Esempio n. 1
0
        //获取一次滑动行为
        public TouchDirection GetTouchMoveDirection(TouchDirection dirResult)
        {
            if (TouchCount == 1)
            {
                //实时检测触碰到UI
                if (_currentEventSys.IsPointerOverGameObject(Input.touches[0].fingerId))
                {
                    dirResult |= TouchDirection.OnUI;

                    IsTouchedOnUICallBack?.Invoke(CurrentIsOnUI);
                }

                if (Input.touches[0].phase != TouchPhase.Canceled)
                {
                    Vector2 inputPos = Input.touches[0].position;
                    switch (Input.touches[0].phase)
                    {
                    case TouchPhase.Began:
                        if (_config.TouchDetectUI && CurrentIsOnUI)
                        {
                            return(dirResult);
                        }

                        _touchBeginPos = inputPos;
                        TouchBeganCallback?.Invoke(inputPos);
                        break;

                    case TouchPhase.Ended:
                        _touchEndPos = inputPos;
                        if (_config.TouchDetectUI && CurrentIsOnUI)
                        {
                            return(dirResult = TouchDirection.None);
                        }

                        TouchEndCallback?.Invoke(inputPos);
                        if (Vector2.Distance(_touchBeginPos, _touchEndPos) < MOVE_MIX_DISTANCE)
                        {
                            return(dirResult);
                        }
                        float offSetX = _touchEndPos.x - _touchBeginPos.x;
                        float offSetY = _touchEndPos.y - _touchBeginPos.y;
                        float angle   = Mathf.Atan2(offSetY, offSetX) * Mathf.Rad2Deg;
                        //顺时针偏移
                        angle += _clockWiseDegree;
                        if (angle > 180)
                        {
                            angle = 360 - angle;
                        }
                        else if (angle < -180)
                        {
                            angle += 360;
                        }
                        if (angle <= 45f && angle > -45f)
                        {
                            dirResult |= TouchDirection.Right;
                        }
                        else if (angle > 45f && angle <= 135f)
                        {
                            dirResult |= TouchDirection.Up;
                        }
                        else if ((angle > 135f && angle < 180f) || (angle > -180f && angle <= -135f))
                        {
                            dirResult |= TouchDirection.Left;
                        }
                        else if (angle > -135f && angle <= -45f)
                        {
                            dirResult |= TouchDirection.Down;
                        }
                        break;

                    case TouchPhase.Moved:
                        if (_config.TouchDetectUI && CurrentIsOnUI)
                        {
                            return(dirResult);
                        }
                        TouchMoveCallback?.Invoke(inputPos);
                        break;

                    case TouchPhase.Stationary:
                        if (_config.TouchDetectUI && CurrentIsOnUI)
                        {
                            return(dirResult);
                        }
                        TouchStationaryCallback?.Invoke(inputPos);
                        break;
                    }
                }
                else
                {
                    dirResult = TouchDirection.None;
                }
                return(dirResult);
            }
            else if (TouchCount > 1)
            {
                if (Input.GetTouch(0).phase == TouchPhase.Began)
                {
                    _oldPosition1 = Input.GetTouch(0).position;
                }
                if (Input.GetTouch(1).phase == TouchPhase.Began)
                {
                    _oldPosition2 = Input.GetTouch(1).position;
                }

                if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved)
                {
                    //计算出当前两点触摸点的位置
                    var tempPosition1 = Input.GetTouch(0).position;
                    var tempPosition2 = Input.GetTouch(1).position;

                    FingerRotateCallBack?.Invoke(LMath.CalcIncludedAngle2D(_oldPosition2 - _oldPosition1, tempPosition2 - tempPosition1));

                    //缩放数据
                    float currentTouchDistance = Vector3.Distance(tempPosition1, tempPosition2);
                    float lastTouchDistance    = Vector3.Distance(_oldPosition1, _oldPosition2);

                    FingerZoomCallback?.Invoke((lastTouchDistance - currentTouchDistance) * Time.deltaTime);

                    //备份上一次触摸点的位置,用于对比
                    _oldPosition1 = tempPosition1;
                    _oldPosition2 = tempPosition2;
                }
            }
            return(dirResult = TouchDirection.None);
        }