Skip to content

Instantly share code, notes, and snippets.

@nobnak
Created February 15, 2016 10:22
Show Gist options
  • Save nobnak/c5a2588f3e5a8c0b798b to your computer and use it in GitHub Desktop.
Save nobnak/c5a2588f3e5a8c0b798b to your computer and use it in GitHub Desktop.
Auto Hide Cursor for Unity
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