Пример #1
0
    void OnGUI()
    {
        var           nativeTouchScale = InputManager.instance.nativeTouchScale;
        StringBuilder output           = new StringBuilder();

        switch (currentInput)
        {
        case 1:
            output.AppendLine("<b>Input:</b> Native Touch");
            break;

        case 2:
            output.AppendLine("<b>Input:</b> Unity Touch");
            break;

        case 3:
            output.AppendLine("<b>Input:</b> Mouse");
            break;

        default:
            output.AppendLine("<b>Input:</b> Undefined (Mouse / Unity Touch)");
            break;
        }

        output.AppendLine("<b>Supports Touch Radius:</b> " + InputManager.instance.supportsTouchRadius);

        switch (InputManager.instance.forceTouchState)
        {
        case ForceTouchState.Available:
            output.AppendLine("<b>Force Touch State:</b> Available");
            break;

        case ForceTouchState.Unknown:
            output.AppendLine("<b>Force Touch State:</b> Unknown");
            break;

        case ForceTouchState.IncompatibleOS:
            output.AppendLine("<b>Force Touch State:</b> Incompatible OS");
            break;

        case ForceTouchState.Unavailable:
            output.AppendLine("<b>Force Touch State:</b> Unavailable");
            break;
        }

        output.AppendLine("<b>Callback Message:</b> " + callbackMessage);


        if (InputManager.instance.touches.Count > 0)
        {
            output.AppendLine("<b>Touches:</b>");
            for (int i = 0; i < InputManager.instance.touches.Count; i++)
            {
                output.AppendLine(InputManager.instance.touches [i].ToString());
            }
        }
        else
        {
            output.AppendLine("<b>Touches:</b> No touches available");
        }



        GUI.skin.label.alignment = TextAnchor.UpperLeft;
        GUI.color = Color.black;
        GUI.Label(new Rect(10 * nativeTouchScale, 10 * nativeTouchScale, Screen.width - 20 * nativeTouchScale, Screen.height - 20 * nativeTouchScale), "<size=" + 16 * nativeTouchScale + ">" + output.ToString() + "</size>");



        GUI.color = Color.white;

        if (ForceTouchPlugin.IsTracking())
        {
            if (GUI.Button(new Rect(10 * nativeTouchScale, Screen.height - ((10 + 40) * nativeTouchScale), Screen.width / 2 - 15 * nativeTouchScale, 40 * nativeTouchScale), "<size=" + 16 * nativeTouchScale + ">Switch Unity Input</size>"))
            {
                ForceTouchPlugin.StopTracking();
            }
        }
        else
        {
            if (GUI.Button(new Rect(10 * nativeTouchScale, Screen.height - ((10 + 40) * nativeTouchScale), Screen.width / 2 - 15 * nativeTouchScale, 40 * nativeTouchScale), "<size=" + 16 * nativeTouchScale + ">Switch Native Input</size>"))
            {
                //it would be best to only start tracking if forceTouchState is Available
                ForceTouchPlugin.StartTracking();
            }
        }

        if (GUI.Button(new Rect(Screen.width / 2 + 5 * nativeTouchScale, Screen.height - ((10 + 40) * nativeTouchScale), Screen.width / 2 - 15 * nativeTouchScale, 40 * nativeTouchScale), "<size=" + 16 * nativeTouchScale + ">Request FT State</size>"))
        {
            InputManager.instance.forceTouchState = ForceTouchPlugin.GetForceTouchState();
        }


        if (callbackAdded)
        {
            if (GUI.Button(new Rect(10 * nativeTouchScale, Screen.height - (100 * nativeTouchScale), Screen.width - 20 * nativeTouchScale, 40 * nativeTouchScale), "<size=" + 16 * nativeTouchScale + ">Remove FT State Change Callback</size>"))
            {
                callbackMessage = "No message received";
                InputManager.instance.RemoveCallback();
                callbackAdded = false;
            }
        }
        else
        {
            if (GUI.Button(new Rect(10 * nativeTouchScale, Screen.height - (100 * nativeTouchScale), Screen.width - 20 * nativeTouchScale, 40 * nativeTouchScale), "<size=" + 16 * nativeTouchScale + ">Add FT State Change Callback</size>"))
            {
                callbackMessage = "No message received";
                InputManager.instance.AddCallback();
                callbackAdded = true;
            }
        }
    }
Пример #2
0
    /*
     * Refresh touch data.
     */
    public void Update()
    {
        if (GetComponent <StatusUI>())        //this is only required for StatusUI.
        {
            GetComponent <StatusUI>().currentInput = -1;
        }

        if (ForceTouchPlugin.IsTracking())         //this will give false for all platforms except iOS. On iOS it will give true if StartTracking was called.
        {
            /*
             * If you don't need to handle touch state, you can simply replace the code bellow with the following code:
             * touches.Clear();
             *
             * touches.AddRange(nativeTouches);
             *
             * if (GetComponent<StatusUI>()) //this is only required for StatusUI.
             *      GetComponent<StatusUI>().currentInput = 1;
             */

            UpdateNativeTouches();

            var nativeTouches = ForceTouchPlugin.GetNativeTouches();
            for (var i = 0; i < nativeTouches.Count; i++)
            {
                HandleNativeInput(nativeTouches[i]);
            }

            if (GetComponent <StatusUI>())            //this is only required for StatusUI.
            {
                GetComponent <StatusUI>().currentInput = 1;
            }
        }
        else if (Input.touchCount > 0)         //if native touch isn't available fallback to unity touch
        {
            touches.Clear();

            for (int i = 0; i < Input.touchCount; i++)
            {
                var t = Input.touches [i];
                HandleInput(t.fingerId, t.phase, t.position, t.deltaPosition);
            }

            if (GetComponent <StatusUI>())            //this is only required for StatusUI.
            {
                GetComponent <StatusUI>().currentInput = 2;
            }
        }
        else         //if unity touch is unavailable fallback to mouse input
        {
            touches.Clear();

            var mousePos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
            if (Input.GetMouseButtonDown(0))
            {
                HandleInput(mouseId, TouchPhase.Began, mousePos, Vector2.zero);
            }
            else if (Input.GetMouseButtonUp(0))
            {
                HandleInput(mouseId, TouchPhase.Ended, mousePos, mousePos - lastMousePos);
            }
            else if (Input.GetMouseButton(0))
            {
                var delta = mousePos - lastMousePos;
                if (delta != Vector2.zero)
                {
                    HandleInput(mouseId, TouchPhase.Moved, mousePos, delta);
                }
                else
                {
                    HandleInput(mouseId, TouchPhase.Stationary, mousePos, delta);
                }
            }
            else
            {
                return;
            }

            lastMousePos = Input.mousePosition;

            if (GetComponent <StatusUI>())            //this is only required for StatusUI.
            {
                GetComponent <StatusUI>().currentInput = 3;
            }
        }
    }