Unity flexible control software to hide and disappear mouse

  When I was released before the PC software with unity, for display and mouse never hidden attention, and later to the site once the installer found a mouse stuck in the very influence on the interface showing the effect of (the landlord is doing showrooms software of), so I thought of writing a small script to hide individual control of the mouse and display.

  In unity, we can use the code Cursor.visible = true or false to control displaying and hiding the mouse. I just started to think simple is to use this code, I press a mouse button is displayed, press a hidden button mouse, this mouse is indeed able to control the display and hide, but would like from the user's point of view, this is too silly now, each had to press to press to go, the design is unreasonable. I should be a dynamic program to determine whether the mouse to hide disappear when someone operating a mouse display, a mouse over a period of time when the unmanned operation will automatically disappear, this is reasonable. But how to determine if someone does it, you can determine whether it was operating in accordance with the position of the mouse, the mouse position for a long time when you can not judge judgment unattended operation, the problem has been analyzed clearly be on code

   

using UnityEngine;
using System.Collections;

public class MouseHideControl : MonoBehaviour {

    private Vector3 OldMousePos = new Vector3(0, 0, 0);
    private bool MouseMove = false;
    private float MouseHideTime = -1;
    public float MouseHideTimer = 5;
    // Use this for initialization
    void Start () {
        InvokeRepeating("SearchMouseState", 1f, 0.3f);
    }
    
    // Update is called once per frame
    void Update () {
        if (!MouseMove && MouseHideTime >= 0)
        {
            MouseHideTime += Time.deltaTime;
            if (MouseHideTime >= MouseHideTimer)
            {
                Cursor.visible = false;
                MouseHideTime = -1;
            }
        }
    }

    void SearchMouseState()
    {
        if (Input.mousePosition != OldMousePos)
        {
            OldMousePos = Input.mousePosition;
            Cursor.visible = true;
            MouseMove = true;
            MouseHideTime = 0;
        }
        else
        {
            MouseMove = false;
        }
    }
}

        Long code is not necessarily a good taste, to follow the single responsibility, a complete code for a feature.

Guess you like

Origin www.cnblogs.com/linkshow/p/12068379.html