Unity判断手势在物体的滑动是顺时针还是逆时针

方法一:
/************* ** Company: DX **SrtiptName:JudgeClockwise ** Auth: CW ** Des: 判断是否顺时针旋转 ** Ver.: V1.0.0 *************/ using UnityEngine; using System.Collections; using Kernal; using UnityEngine.EventSystems; namespace Global { public class JudgeClockwiseRotate : MonoBehaviour { /// /// 手指开始触摸的位置 /// private Vector3 _startTouchPos; private PointerEventData _pointerEventData; /// /// 手指触摸的区域 /// private E_ClickPosType CurretClickType; /// /// 圆的左边的点 /// public Transform Left; /// /// 圆的右边的点 /// public Transform Right; /// ///圆上面的点 /// public Transform Up; /// /// 圆的下面的点 /// public Transform Down; /// /// 圆的中心点 /// public Transform Center; /// /// 是否是顺时针旋转 /// private bool _isClockwiseRotate = false; void Start() { EventTriggerListener.Get(gameObject).OnMyBeginDrag += OnBeginDrag; EventTriggerListener.Get(gameObject).OnMyDrag += OnDrag; } private void OnDrag(GameObject go, BaseEventData baseEventData) { _pointerEventData = baseEventData as PointerEventData; //得到手指的位置在圆的哪个部分(左上,坐下,右上,右下) CurretClickType = UnityHelper.ChangePosType(_pointerEventData.position, Center.position, Left.position, Up.position, Right.position , Down.position); switch (CurretClickType) { case E_ClickPosType.TopLeft: if (_pointerEventData.position.x > _startTouchPos.x && _pointerEventData.position.y > _startTouchPos.y) { _isClockwiseRotate = true; Log.Debug("顺时针"); } else if (_pointerEventData.position.x <= _startTouchPos.x && _pointerEventData.position.y <= _startTouchPos.y) { _isClockwiseRotate = false; Log.Debug("逆时针"); } break; case E_ClickPosType.TopRight: if (_pointerEventData.position.x > _startTouchPos.x && _pointerEventData.position.y < _startTouchPos.y) { _isClockwiseRotate = true; Log.Debug("顺时针"); } else if (_pointerEventData.position.x < _startTouchPos.x && _pointerEventData.position.y > _startTouchPos.y) { _isClockwiseRotate = false; Log.Debug("逆时针"); } break; case E_ClickPosType.DownLeft: if (_pointerEventData.position.x < _startTouchPos.x && _pointerEventData.position.y > _startTouchPos.y) { _isClockwiseRotate = true; Log.Debug("顺时针"); } else if (_pointerEventData.position.x >= _startTouchPos.x && _pointerEventData.position.y < _startTouchPos.y) { _isClockwiseRotate = false; Log.Debug("逆时针"); } break; case E_ClickPosType.DownRight: if (_pointerEventData.position.x < _startTouchPos.x && _pointerEventData.position.y < _startTouchPos.y) { _isClockwiseRotate = true; Log.Debug("顺时针"); } else if (_pointerEventData.position.x > _startTouchPos.x && _pointerEventData.position.y > _startTouchPos.y) { _isClockwiseRotate = false; Log.Debug("逆时针"); } break; } _startTouchPos = _pointerEventData.position; } private void OnBeginDrag(GameObject go, BaseEventData baseEventData) { PointerEventData pointerEventData = baseEventData as PointerEventData; _startTouchPos = pointerEventData.position; } /// /// 是否是顺时针旋转 /// /// public bool GetResult() { return _isClockwiseRotate; } } }

UnityHelper函数:

 /// 
    /// 判断点击位置在哪一个区域
    /// 
    public static  E_ClickPosType ChangePosType(Vector2 scenePos,Vector3 centerPos,Vector3 leftPos,Vector3 UpPos,Vector3 rightPos,Vector3 downPos)
    {
        
        Camera mainCamera= GameObject.FindGameObjectWithTag(Tags.Tag_MainCamera).GetComponent();
        Vector3 pos= mainCamera.ScreenToWorldPoint(scenePos);

        E_ClickPosType clickType = E_ClickPosType.None;
        if (pos.x >= centerPos.x && pos.x <= rightPos.x && pos.y >= centerPos.y && pos.y <= UpPos.y)
        {
            clickType = E_ClickPosType.TopRight;
        }
        else if (pos.x < centerPos.x && pos.x > leftPos.x && pos.y > centerPos.y && pos.y < UpPos.y)
        {
            clickType = E_ClickPosType.TopLeft;
        }
        else if (pos.x <= centerPos.x && pos.x >= leftPos.x && pos.y <= centerPos.y && pos.y >= downPos.y)
        {
            clickType = E_ClickPosType.DownLeft;
        }
        else if (pos.x > centerPos.x && pos.x < rightPos.x && pos.y < centerPos.y && pos.y > downPos.y)
        {
            clickType = E_ClickPosType.DownRight;
        }
        return clickType;
    }

 
    

 

 
   

 

转载于:https://www.cnblogs.com/weiqiangwaideshijie/p/9284463.html

你可能感兴趣的:(Unity判断手势在物体的滑动是顺时针还是逆时针)