コード例 #1
0
 private void Update()
 {
     if (Input.touchCount == 2)
     {
         Touch   touch     = Input.touches[0];
         Touch   touch2    = Input.touches[1];
         Vector2 position  = touch.position;
         Vector2 position2 = touch2.position;
         Vector2 vector    = position - lastTouchPos1;
         Vector2 vector2   = position2 - lastTouchPos2;
         if (firstTouch)
         {
             firstTouch   = false;
             initPos1     = position;
             initPos2     = position2;
             initGradient = (position - position2).normalized;
             float x = position.x - position2.x;
             float y = position.y - position2.y;
             prevAngle = IT_Gesture.VectorToAngle(new Vector2(x, y));
         }
         if (touch.phase == TouchPhase.Moved && touch2.phase == TouchPhase.Moved)
         {
             float num = Vector2.Dot(vector, vector2);
             if (num < 0.5f)
             {
                 Vector2 normalized  = (position - initPos1).normalized;
                 Vector2 normalized2 = (position2 - initPos2).normalized;
                 float   num2        = Vector2.Dot(normalized, initGradient);
                 float   num3        = Vector2.Dot(normalized2, initGradient);
                 if (num2 < 0.7f && num3 < 0.7f)
                 {
                     float x2      = position.x - position2.x;
                     float y2      = position.y - position2.y;
                     float current = IT_Gesture.VectorToAngle(new Vector2(x2, y2));
                     float num4    = Mathf.DeltaAngle(current, prevAngle);
                     if (rotationSmoothing == _SmoothMethod.None)
                     {
                         RotateInfo rI = new RotateInfo(num4, position, position2);
                         IT_Gesture.Rotate(rI);
                     }
                     else
                     {
                         if (Mathf.Abs(num4) > 0f)
                         {
                             AddRotVal(num4);
                         }
                         float averageValue = GetAverageValue();
                         if (averageValue != 0f)
                         {
                             RotateInfo rI2 = new RotateInfo(averageValue, position, position2);
                             IT_Gesture.Rotate(rI2);
                         }
                     }
                     prevAngle = current;
                 }
                 else
                 {
                     Vector2 vector3 = position - position2;
                     float   num5    = (position - vector - (position2 - vector2)).magnitude - vector3.magnitude;
                     if (Mathf.Abs(num5) > 0.5f)
                     {
                         PinchInfo pI = new PinchInfo(num5, position, position2);
                         IT_Gesture.Pinch(pI);
                     }
                 }
             }
         }
         lastTouchPos1 = position;
         lastTouchPos2 = position2;
     }
     else if (!firstTouch)
     {
         firstTouch = true;
         if (rotationSmoothing != 0)
         {
             ClearRotVal();
         }
     }
 }
コード例 #2
0
    void Update()
    {
        if (Input.touchCount == 2)
        {
            Touch touch1 = Input.touches[0];
            Touch touch2 = Input.touches[1];

            Vector2 pos1 = touch1.position;
            Vector2 pos2 = touch2.position;

            Vector2 delta1 = pos1 - lastTouchPos1;
            Vector2 delta2 = pos2 - lastTouchPos2;


            if (firstTouch)
            {
                firstTouch = false;

                //for rotate
                initPos1     = pos1;
                initPos2     = pos2;
                initGradient = (pos1 - pos2).normalized;

                float curX = pos1.x - pos2.x;
                float curY = pos1.y - pos2.y;
                prevAngle = IT_Gesture.VectorToAngle(new Vector2(curX, curY));
            }


            if (touch1.phase == TouchPhase.Moved && touch2.phase == TouchPhase.Moved)
            {
                float dot = Vector2.Dot(delta1, delta2);
                if (dot < .5f)
                {
                    Vector2 grad1 = (pos1 - initPos1).normalized;
                    Vector2 grad2 = (pos2 - initPos2).normalized;

                    float dot1 = Vector2.Dot(grad1, initGradient);
                    float dot2 = Vector2.Dot(grad2, initGradient);

                    //rotate
                    if (dot1 < 0.7f && dot2 < 0.7f)
                    {
                        float curX     = pos1.x - pos2.x;
                        float curY     = pos1.y - pos2.y;
                        float curAngle = IT_Gesture.VectorToAngle(new Vector2(curX, curY));
                        float val      = Mathf.DeltaAngle(curAngle, prevAngle);

                        if (rotationSmoothing == _SmoothMethod.None)
                        {
                            RotateInfo rotateInfo = new RotateInfo(val, pos1, pos2);
                            IT_Gesture.Rotate(rotateInfo);
                        }
                        else
                        {
                            if (Mathf.Abs(val) > 0)
                            {
                                AddRotVal(val);
                            }

                            float valueAvg = GetAverageValue();
                            if (valueAvg != 0)
                            {
                                RotateInfo rotateInfo = new RotateInfo(valueAvg, pos1, pos2);
                                IT_Gesture.Rotate(rotateInfo);
                            }
                        }

                        prevAngle = curAngle;
                    }
                    //pinch
                    else
                    {
                        Vector2 curDist  = pos1 - pos2;
                        Vector2 prevDist = (pos1 - delta1) - (pos2 - delta2);
                        float   pinch    = prevDist.magnitude - curDist.magnitude;

                        if (Mathf.Abs(pinch) > 0.5f)
                        {
                            PinchInfo pinchInfo = new PinchInfo(pinch, pos1, pos2);
                            IT_Gesture.Pinch(pinchInfo);
                        }
                    }
                }
            }

            lastTouchPos1 = pos1;
            lastTouchPos2 = pos2;
        }
        else
        {
            if (!firstTouch)
            {
                firstTouch = true;
                if (rotationSmoothing != _SmoothMethod.None)
                {
                    ClearRotVal();
                }
            }
        }
    }