using UnityEngine;
namespace NextMind.Examples.Utility
{
///
/// The MouseCursorEnabler is used to automatically hide the cursor after seconds of inactivity.
/// The cursor is displayed back as soon as the mouse moves.
///
public class MouseCursorEnabler : MonoBehaviour
{
///
/// The time of inactivity allowed before the cursor disappear
///
private readonly float visibleTimeAfterlastMove = 4f;
private float timer = 0f;
void Update()
{
if (HasMouseMoved())
{
timer = 0f;
Cursor.visible = true;
}
if (Cursor.visible)
{
timer += Time.deltaTime;
}
if (timer > visibleTimeAfterlastMove)
{
Cursor.visible = false;
}
}
///
/// Check if the mouse is moving this frame.
///
private bool HasMouseMoved()
{
return !Mathf.Approximately(Input.GetAxis("Mouse X"),0) || !Mathf.Approximately(Input.GetAxis("Mouse Y"), 0);
}
}
}