Unity判断手势在物品的滑动方向(上下左右)
/************* ** Company: DX **SrtiptName:DragItem ** Auth: CW ** Des: 文本描述 ** Ver.: V1.0.0 *************/ using UnityEngine; using System.Collections; using UnityEngine.EventSystems; using Kernal; namespace Global { public class DragItem : MonoBehaviour { /// <summary> /// 开始触摸的手指的位置 /// </summary> private Vector3 _startTouchPos; private PointerEventData _pointerEventData; /// <summary> /// 拖拽的方向 /// </summary> private E_DragDir _dragDir; void Start() { EventTriggerListener.Get(gameObject).OnMyBeginDrag += OnBeginDrag; EventTriggerListener.Get(gameObject).OnMyDrag += OnDrag; EventTriggerListener.Get(gameObject).OnMyEndDrag += OnEndDrag; } private void OnEndDrag(GameObject go, BaseEventData baseEventData) { _dragDir = E_DragDir.None; } private E_ClickPosType CurretClickType; private void OnDrag(GameObject go, BaseEventData baseEventData) { if (_pointerEventData.position.x > _startTouchPos.x) { Log.Debug("往右边滑动"); _dragDir = E_DragDir.Right; } else if (_pointerEventData.position.x < _startTouchPos.x) { Log.Debug("往左边滑动"); _dragDir = E_DragDir.Left; } else if (_pointerEventData.position.y < _startTouchPos.y) { Log.Debug("往下面滑动"); _dragDir = E_DragDir.Down; } else if (_pointerEventData.position.y > _startTouchPos.y) { Log.Debug("往上面滑动"); _dragDir = E_DragDir.Up; } _startTouchPos = _pointerEventData.position; } private void OnBeginDrag(GameObject go, BaseEventData baseEventData) { _pointerEventData = baseEventData as PointerEventData; _startTouchPos = _pointerEventData.position; } /// <summary> /// 得到滑动的方向 /// </summary> /// <returns></returns> public E_DragDir GetDragDir() { return _dragDir; } } }