KITASENJU DESIGN BLOG

memo, html, javascript, unity

draggable UI from side to side

for smartphone and pc.

左右にドラッグできるUI タッチまたはマウスダウンで左右にドラッグするスクリプトスマホ、PC対応

めんどくさいので、もうつくりたくない・・

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace HOGE
{
    
    public class AboutContainer : MonoBehaviour
    {

        //マウスダウンした移動量分左右に動かす

        [SerializeField] private RectTransform _container;
        [SerializeField] private float _width=3000;
        private Vector3 _containerPoint = Vector3.zero;

        private Vector2 _downPoint = Vector2.zero;
        private Vector2 _currentPoint = Vector2.zero;
        private bool _isDown=false;


        void Awake(){

        }

        private void _onMouseDown(Vector2 downPoint){
            
            _isDown=true;
            _downPoint = downPoint;//Input.mousePosition;
            _containerPoint = _container.localPosition;
            _currentPoint = downPoint;//Input.mousePosition;
        }

        private void _onMouseUp(){
            _isDown=false;            
        }

        private void _onMouseMove(Vector2 mousePos){
            _currentPoint = mousePos;
        }

        void Update(){

            
            if(Input.GetMouseButtonDown(0)){
                _onMouseDown(Input.mousePosition);
            }
            if(Input.GetMouseButtonUp(0)){
                _onMouseUp();
            }

            
            if(Input.touchCount >= 1){
                var touch = Input.touches[0];
                if (touch.phase == TouchPhase.Began){
                    //タッチ開始
                    _onMouseDown(touch.position);
                }
                if(touch.phase == TouchPhase.Moved){
                    //_downPoint = touch.position;
                    //_onMouseMove( touch.position );
                }
                if (touch.phase == TouchPhase.Ended){
                    //_downPoint = touch.position;
                    _onMouseUp();
                }
            }


            if(_isDown){
                
                if(Input.touchCount >= 1){
                    var tt = Input.touches[0];
                    _onMouseMove( tt.position );
                }else{
                    _onMouseMove( Input.mousePosition );
                }           
                    
                
                var d = _currentPoint - _downPoint;
                _container.localPosition = _containerPoint + new Vector3(d.x,0,0);

                //制限
                if(_container.localPosition.x>0){
                    var pos = _container.localPosition;
                    pos.x=0;
                    _container.localPosition=pos;
                }
                if(_container.localPosition.x<-_width){
                    var pos = _container.localPosition;
                    pos.x=-_width;
                    _container.localPosition=pos;                    
                }

            }else{


            }


        }


    }

}
"FOOTER"