Created
February 15, 2016 10:22
-
-
Save nobnak/c5a2588f3e5a8c0b798b to your computer and use it in GitHub Desktop.
Auto Hide Cursor for Unity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
namespace nobnak.Blending { | |
public class AutoHideCursor : MonoBehaviour { | |
public float hideAfterSeconds = 3f; | |
public float thresholdInPixels = 3f; | |
float _lastTime; | |
Vector3 _lastMousePos; | |
void Start () { | |
_lastTime = Time.timeSinceLevelLoad; | |
_lastMousePos = Input.mousePosition; | |
} | |
void Update () { | |
var dx = Input.mousePosition - _lastMousePos; | |
var move = (dx.sqrMagnitude > (thresholdInPixels * thresholdInPixels)); | |
_lastMousePos = Input.mousePosition; | |
if (move) | |
_lastTime = Time.timeSinceLevelLoad; | |
Cursor.visible = (Time.timeSinceLevelLoad - _lastTime) < hideAfterSeconds; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment