コード例 #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);
        }
コード例 #2
0
        private void InputUpdateHandler()
        {
            //点击返回
            if (Input.GetKeyDown(KeyCode.Escape))
            {
                EscapeBtnClick();
            }

            if (_isInit && IsEnable)
            {
                if (_currentPlatform == PLATFORM_ANDROID || _currentPlatform == PLATFORM_IOS)
                {
                    //是否点击到UI界面
                    if (TouchCount > 0)
                    {
                        _touchResult = GetTouchMoveDirection(_touchResult);
                    }
                    else
                    {
                        _touchResult = TouchDirection.None;
                    }
                }
                else if (_currentPlatform == PLATFORM_WINDOWEDITOR || _currentPlatform == PLATFORM_IOSEDITOR)
                {
                    var wheel = Input.GetAxis("Mouse ScrollWheel");
                    FingerZoomCallback?.Invoke(wheel);

                    if (Input.GetMouseButtonDown(0))
                    {
                        //点击UI检测
                        if (EventSystem.current.IsPointerOverGameObject())
                        {
                            _touchResult |= TouchDirection.OnUI;
                        }
                        IsTouchedOnUICallBack?.Invoke(CurrentIsOnUI);

                        if (_config.TouchDetectUI && CurrentIsOnUI)
                        {
                            return;
                        }

                        //按压计时
                        CalculateTimeByPressStart(Input.mousePosition);
                        //输出开始点击事件
                        TouchBeganCallback?.Invoke(Input.mousePosition);
                    }

                    if (Input.GetButton("Fire1"))
                    {
                        if (_config.TouchDetectUI && CurrentIsOnUI)
                        {
                            return;
                        }

                        if (_recordedPCPos == Vector3.zero)
                        {
                            _pressTimeCount = 0f;
                            _recordedPCPos  = Input.mousePosition;
                            return;
                        }
                        if (_recordedPCPos != Input.mousePosition)
                        {
                            _pressTimeCount = 0f;
                            _recordedPCPos  = Input.mousePosition;

                            TouchMoveCallback?.Invoke(Input.mousePosition);
                        }
                        else
                        {
                            _pressTimeCount += Time.deltaTime;
                            if (_pressTimeCount >= _config.TouchStationaryTime)
                            {
                                TouchStationaryCallback?.Invoke(Input.mousePosition);
                            }
                        }
                    }

                    if (Input.GetMouseButtonUp(0))
                    {
                        _recordedPCPos = Vector2.zero;

                        if (_config.TouchDetectUI && CurrentIsOnUI)
                        {
                            _touchResult = TouchDirection.None; return;
                        }
                        TouchEndCallback?.Invoke(Input.mousePosition);
                        CalculateTimeByPressOver(Input.mousePosition);

                        _touchResult = TouchDirection.None;
                    }
                }
            }
        }