Ejemplo n.º 1
0
    void Update()
    {
        Input.multiTouchEnabled = multitouch;

        // update old touches
        foreach (Touch t in Input.touches)
        {
            // see if we already have a TouchWrapper for this particular Touch.
            VCTouchWrapper tw = null;
            foreach (VCTouchWrapper touchWrapper in touches)
            {
                if (touchWrapper.fingerId == t.fingerId)
                {
                    tw = touchWrapper;
                    break;
                }
            }

            if (tw == null)
            {
                // this is a new touch, add it to our active list

                // find an available TouchWrapper to use.
                VCTouchWrapper availableTw = null;
                foreach (VCTouchWrapper touchWrapper in touches)
                {
                    if (touchWrapper.fingerId == -1)
                    {
                        availableTw = touchWrapper;
                        break;
                    }
                }

                if (availableTw == null)
                {
                    Debug.LogWarning("Cannot find an available TouchWrapper to assign Touch info from!  Consider increasing VCTouchController.kMaxTouches.");
                }
                else
                {
                    availableTw.Set(t);
                }
            }
            else
            // touch we're already tracking, update it.
            {
                tw.visited = true;

                tw.deltaPosition = t.position - tw.position;
                tw.position      = t.position;
                tw.phase         = t.phase;
            }
        }

#if UNITY_EDITOR
        // use mouse input to emulate a touch.
        if (Input.GetMouseButtonDown(0))
        {
            VCTouchWrapper availableTw = null;
            foreach (VCTouchWrapper touchWrapper in touches)
            {
                if (touchWrapper.fingerId == -1)
                {
                    availableTw = touchWrapper;
                    break;
                }
            }

            if (availableTw == null)
            {
                // we're out of touches, can't emulate.
                Debug.LogWarning("Cannot find an available TouchWrapper to assign Mouse Emulated Touch info from!  Consider increasing VCTouchController.kMaxTouches.");
            }
            else
            {
                // populate the TouchWrapper with mouse input data.
                availableTw.phase           = TouchPhase.Began;
                availableTw.fingerId        = kEmulatedTouchFingerId;
                availableTw.deltaPosition.x = 0.0f;
                availableTw.deltaPosition.y = 0.0f;
                availableTw.position.x      = Input.mousePosition.x;
                availableTw.position.y      = Input.mousePosition.y;
                availableTw.visited         = true;
            }
        }
        else if (Input.GetMouseButton(0))
        {
            // find the emulated TouchWrapper
            VCTouchWrapper emulatedTw = null;
            foreach (VCTouchWrapper touchWrapper in touches)
            {
                if (touchWrapper.fingerId == kEmulatedTouchFingerId)
                {
                    emulatedTw = touchWrapper;
                    break;
                }
            }

            // update it
            emulatedTw.phase           = TouchPhase.Moved;
            emulatedTw.deltaPosition.x = Input.mousePosition.x - emulatedTw.position.x;
            emulatedTw.deltaPosition.y = Input.mousePosition.y - emulatedTw.position.y;
            emulatedTw.position.x      = Input.mousePosition.x;
            emulatedTw.position.y      = Input.mousePosition.y;
            emulatedTw.visited         = true;
        }
#endif

        // reset any previously active, but no longer active touch
        foreach (VCTouchWrapper tw in touches)
        {
            if (!tw.visited)
            {
                tw.Reset();
            }
            else
            {
                tw.visited = false;                 // ready to cull next frame
            }
        }

        // invalidate the ActiveTouches cache so it updates next time the user wants it.
        _activeTouchesCacheNeedsUpdate = true;
    }
Ejemplo n.º 2
0
    void Update()
    {
#if (UNITY_IPHONE && !UNITY_EDITOR) || (UNITY_ANDROID && !UNITY_EDITOR)
        Input.multiTouchEnabled = multitouch;

        // update old touches
        foreach (Touch t in Input.touches)
        {
            VCTouchWrapper tw = touches.FirstOrDefault(x => x.fingerId == t.fingerId);
            if (tw == null)
            {
                // this is a new touch, add it to our active list
                touches.FirstOrDefault(x => x.fingerId == -1).Set(t);
            }
            else
            // touch we're already tracking, update it.
            {
                tw.visited = true;

#if UNITY_ANDROID
                // test for error movement
                if (ignoreMultitouchErrorMovement && Input.touchCount > 1)
                {
                    Vector2 d = tw.position - t.position;
                    if (d.sqrMagnitude > multiTouchErrorSqrMagnitudeMax)
                    {
                        continue;
                    }
                }
#endif
                tw.deltaPosition = t.position - tw.position;
                tw.position      = t.position;
                tw.phase         = t.phase;
            }
        }

        // reset any previously active, but no longer active touch
        foreach (VCTouchWrapper tw in touches)
        {
            if (!tw.visited)
            {
                tw.Reset();
            }
            else
            {
                tw.visited = false;                 // ready to cull next frame
            }
        }
#else
        // use mouse input to emulate a touch.
        if (Input.GetMouseButtonDown(0))
        {
            touches[0].phase           = TouchPhase.Began;
            touches[0].fingerId        = 1;
            touches[0].deltaPosition.x = 0.0f;
            touches[0].deltaPosition.y = 0.0f;
            touches[0].position.x      = Input.mousePosition.x;
            touches[0].position.y      = Input.mousePosition.y;
        }
        else if (Input.GetMouseButton(0))
        {
            touches[0].phase           = TouchPhase.Moved;
            touches[0].deltaPosition.x = Input.mousePosition.x - touches[0].position.x;
            touches[0].deltaPosition.y = Input.mousePosition.y - touches[0].position.y;
            touches[0].position.x      = Input.mousePosition.x;
            touches[0].position.y      = Input.mousePosition.y;
        }
        else
        {
            touches[0].Reset();
        }
#endif

        _activeTouchesCache = null;
    }