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

 /*************
** 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
    {

        /// <summary>
        /// 手指开始触摸的位置
        /// </summary>
        private Vector3 _startTouchPos;
        private  PointerEventData _pointerEventData;
        /// <summary>
        /// 手指触摸的区域
        /// </summary>
        private E_ClickPosType CurretClickType;
        /// <summary>
        /// 圆的左边的点
        /// </summary>
        public Transform Left;
        /// <summary>
        /// 圆的右边的点
        /// </summary>
        public Transform Right;
        /// <summary>
        ///圆上面的点
        /// </summary>
        public Transform Up;
        /// <summary>
        /// 圆的下面的点
        /// </summary>
        public Transform Down;
        /// <summary>
        /// 圆的中心点
        /// </summary>
        public Transform Center;
        /// <summary>
        /// 是否是顺时针旋转
        /// </summary>
        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;
        }
        /// <summary>
        /// 是否是顺时针旋转
        /// </summary>
        /// <returns></returns>
        public bool GetResult()
        {
            return _isClockwiseRotate;
        }
    }
}

UnityHelper函数:

 /// <summary>
    /// 判断点击位置在哪一个区域
    /// </summary>
    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<Camera>();
        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;
    }

猜你喜欢

转载自www.cnblogs.com/weiqiangwaideshijie/p/9284463.html