Intercept input

Back
Tags:
Intercept Request
Example of how to override the input, and use the center of screen as the cursor, and Z key as a left mouse button.
Useful for VR.
Usage instructions:
Add this script to map GameObject and start the scene.
Z button will work as the left mouse button.
After publication, the cursor will always be the center of the screen.
InterceptInputExample.cs
/*         INFINITY CODE         */
/*   https://infinity-code.com   */

using UnityEngine;

namespace InfinityCode.OnlineMapsExamples
{
    /// <summary>
    /// Example of how to override the input, and use the center of screen as the cursor, and Z key as a left mouse button.
    /// </summary>
    [AddComponentMenu("Infinity Code/Online Maps/Examples (API Usage)/InterceptInputExample")]
    public class InterceptInputExample : MonoBehaviour
    {
        /// <summary>
        /// Reference to the map control. If not specified, the current instance will be used.
        /// </summary>
        public OnlineMapsControlBase control;
        
        private void Start()
        {
            // If the control is not specified, get the current instance.
            if (control == null) control = OnlineMapsControlBase.instance;
            
            // Intercepts getting the cursor coordinates.
            control.OnGetInputPosition += OnGetInputPosition;

            // Intercepts getting the number of touches.
            control.OnGetTouchCount += OnGetTouchCount;
        }

        private Vector2 OnGetInputPosition()
        {

#if !UNITY_EDITOR
            // On the device returns center of screen.
            return Camera.main.ViewportToScreenPoint(new Vector3(0.5F, 0.5F, 0));
 #else
            // In the editor returns the coordinates of the mouse cursor.
            return Input.mousePosition;
#endif
        }

        private int OnGetTouchCount()
        {
            // If pressed Z, then it will work like the left mouse button.
            return Input.GetKey(KeyCode.Z) ? 1 : 0;
        }
    }
}