Exemple #1
0
    private void MobileInputDetect()
    {
        //?PC上无法模拟Input.Touch
        Input.simulateMouseWithTouches = true;//设置touch=mouse,但mouse!=touch

        int touchCount = Input.touchCount;

        if (touchCount > 0)
        {
            miniMapController.RefreshFlag = true;
        }

        if (touchCount == 1)
        {
            //TODO:实现单指移动,避免单指触摸的位置是puzzle或者ui按钮
            if (scaleMode)
            {
                DetectCurrentPos();
                moveMode  = false;
                scaleMode = false;
                return;
            }
            Touch touch = Input.GetTouch(0);
            if (moveMode && touch.phase == TouchPhase.Moved)
            {
                //单指移动模式时,记录每一帧的手指位移,应用至playField的位移
                curPos = touch.position;
                Vector3 moveVec = Camera.main.ScreenToWorldPoint(curPos) - Camera.main.ScreenToWorldPoint(lastPos);
                playFieldTrans.position += moveVec;


                DetectCurrentPos();


                Vector3 offset = playFieldTrans.position - playFieldOriginPos;
                miniMapController.RefreshOrangeOutline(offset, scaleRatio);

                lastPos = curPos;
            }

            else if (touch.phase == TouchPhase.Began)
            {
                PointerEventData pointerEventData = new PointerEventData(EventSystem.current);
                pointerEventData.position = touch.position;
                List <RaycastResult> raycastResultsList = new List <RaycastResult>();
                EventSystem.current.RaycastAll(pointerEventData, raycastResultsList);
                if (raycastResultsList.Count > 0)
                {
                    Debug.Log("raycastHit obj:" + raycastResultsList[0].gameObject.name);
                    string rayhitObjName = raycastResultsList[0].gameObject.name;
                    if (rayhitObjName == "BG" || rayhitObjName.Contains("BlankGrid") || rayhitObjName.Contains("FixGrid"))
                    {
                        //如果第一次按下时触摸到的是BG或空格子或固定格子,就开始单指移动模式
                        moveMode = true;
                        lastPos  = touch.position;
                    }
                }
            }

            else if (touch.phase == TouchPhase.Ended)
            {
                // DetectCurrentPos();

                moveMode  = false;
                scaleMode = false;
            }
        }
        else if (touchCount == 2)
        {
            //TODO:实现双指缩放,双指开始的位置需要是非UI以及puzzle
            Touch touch1 = Input.GetTouch(0);
            Touch touch2 = Input.GetTouch(1);
            if (moveMode)
            {
                DetectCurrentPos();
                moveMode  = false;
                scaleMode = false;
                return;
            }

            if (scaleMode)
            {
                //双指模式时,记录每一帧的手指位移,应用至playField的缩放

                //TODO:双指距离增加时,scale相应增加,减少时scale相应减少
                twoFingersCurDistance = Vector3.Magnitude(touch1.position - touch2.position);
                float deltaDistance = twoFingersCurDistance - twoFingersLastDistance;

                scaleRatio += deltaDistance * scaleDeltaSpeed * Time.deltaTime;
                Debug.Log("ScaleRatio:" + scaleRatio);
                if (scaleRatio > maxScaleRatio)
                {
                    scaleRatio = maxScaleRatio;
                }
                else if (scaleRatio < 1)
                {
                    scaleRatio = 1;
                }
                playFieldTrans.localScale = Vector3.one * scaleRatio;

                Vector3 newPos = new Vector3(scalePoint.x - centerToScalePointVec.x * (scaleRatio / lastScaleRatio), scalePoint.y - centerToScalePointVec.y * (scaleRatio / lastScaleRatio), playFieldTrans.position.z);
                playFieldTrans.position = newPos;

                // if (scaleRatio > 1)
                // {
                DetectCurrentPos();
                // }

                Vector3 offset = playFieldTrans.position - playFieldOriginPos;
                miniMapController.RefreshOrangeOutline(offset, scaleRatio);

                twoFingersLastDistance = twoFingersCurDistance;
            }

            else if ((touch1.phase == TouchPhase.Began && touch2.phase == TouchPhase.Began) ||
                     (touch1.phase == TouchPhase.Began && touch2.phase == TouchPhase.Moved) ||
                     (touch1.phase == TouchPhase.Moved && touch2.phase == TouchPhase.Began) ||
                     (touch1.phase == TouchPhase.Began && touch2.phase == TouchPhase.Stationary) ||
                     (touch1.phase == TouchPhase.Stationary && touch2.phase == TouchPhase.Began))
            {
                PointerEventData pointerEventData1 = new PointerEventData(EventSystem.current);
                pointerEventData1.position = touch1.position;
                PointerEventData pointerEventData2 = new PointerEventData(EventSystem.current);
                pointerEventData2.position = touch2.position;
                List <RaycastResult> raycastResultsList1 = new List <RaycastResult>();
                EventSystem.current.RaycastAll(pointerEventData1, raycastResultsList1);
                List <RaycastResult> raycastResultsList2 = new List <RaycastResult>();
                EventSystem.current.RaycastAll(pointerEventData2, raycastResultsList2);

                if (raycastResultsList1.Count > 0 && raycastResultsList2.Count > 0)
                {
                    Debug.Log("raycastHit1 obj:" + raycastResultsList1[0].gameObject.name);
                    Debug.Log("raycastHit2 obj:" + raycastResultsList2[0].gameObject.name);

                    string rayhitObjName1 = raycastResultsList1[0].gameObject.name;
                    string rayhitObjName2 = raycastResultsList2[0].gameObject.name;

                    if ((rayhitObjName1 == "BG" || rayhitObjName1.Contains("BlankGrid") || rayhitObjName1.Contains("FixGrid") || rayhitObjName1.Contains("SettlePuzzle")) &&
                        (rayhitObjName2 == "BG" || rayhitObjName2.Contains("BlankGrid") || rayhitObjName2.Contains("FixGrid") || rayhitObjName2.Contains("SettlePuzzle")))
                    {
                        //如果双指hit的是以上这些,就开始双指缩放模式
                        //TODO:确定一个缩放点,确定初始的双指距离
                        Vector3 touchCenterPos = (touch1.position + touch2.position) / 2;

                        scaleMode      = true;
                        scalePoint     = Camera.main.ScreenToWorldPoint(touchCenterPos);
                        lastScaleRatio = scaleRatio;

                        twoFingersCurDistance  = Vector3.Magnitude(touch1.position - touch2.position);
                        twoFingersLastDistance = twoFingersCurDistance;


                        centerToScalePointVec = new Vector2(scalePoint.x - playFieldTrans.position.x, scalePoint.y - playFieldTrans.position.y);
                    }
                }
            }

            else if (touch1.phase == TouchPhase.Ended && touch2.phase == TouchPhase.Ended)
            {
                DetectCurrentPos();
                moveMode  = false;
                scaleMode = false;
            }
        }
    }