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 OnlineMaps;
using UnityEngine;

namespace 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(Utils.ExampleMenuPath + "InterceptInputExample")]
    public class InterceptInputExample : MonoBehaviour
    {
        /// <summary>
        /// Reference to the map control. If not specified, the current instance will be used.
        /// </summary>
        public ControlBase control;
        
        private void Start()
        {
            // If the control is not specified, get the current instance.
            if (!control && !(control = ControlBase.instance))
            {
                Debug.LogError("Control not found");
                return;
            }
            
            // Intercepts getting the cursor coordinates.
            InputManager.OnGetInputPosition += OnGetInputPosition;

            // Intercepts getting the number of touches.
            InputManager.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 InputManager.mousePosition;
#endif
        }

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