Skip to content

Instantly share code, notes, and snippets.

@samizzo
Last active June 13, 2018 12:04
Show Gist options
  • Save samizzo/0210a721061056123d399ddd6981d606 to your computer and use it in GitHub Desktop.
Save samizzo/0210a721061056123d399ddd6981d606 to your computer and use it in GitHub Desktop.
A StandaloneInputModule that only shows the mouse cursor if the mouse is active.
using UnityEngine;
using UnityEngine.EventSystems;
/// <summary>
/// A StandaloneInputModule replacement that only shows the mouse cursor if the mouse is active.
/// If keyboard or controller input is detected, the cursor is disabled. If mouse input is
/// detected, the cursor is enabled.
///
/// This also has an optional setting to disable clearing the input focus when clicking on
/// an empty part of the screen.
/// </summary>
public class StandaloneInputModuleEx : StandaloneInputModule
{
[Tooltip("If true, clear the current focus when clicking on an empty part of the screen.")]
[SerializeField]
private bool m_clearFocusOnEmptyClick = true;
#region Private state
private Vector2 m_mousePosition;
private Vector2 m_lastMousePosition;
private bool m_cursorVisible;
#endregion
#region Public interface
public override bool ShouldActivateModule()
{
// The keyboard or controller are active if a button was pressed or an axis changed.
var keyboardIsActive = Input.GetButtonDown(submitButton);
keyboardIsActive |= Input.GetButtonDown(cancelButton);
keyboardIsActive |= !Mathf.Approximately(Input.GetAxisRaw(horizontalAxis), 0.0f);
keyboardIsActive |= !Mathf.Approximately(Input.GetAxisRaw(verticalAxis), 0.0f);
// The mouse is active if the first button went down or it was moved.
m_lastMousePosition = m_mousePosition;
m_mousePosition = Input.mousePosition;
var delta = m_mousePosition - m_lastMousePosition;
var mouseIsActive = delta.sqrMagnitude > 0.0f;
mouseIsActive |= Input.GetMouseButtonDown(0);
if (mouseIsActive)
{
Cursor.visible = true;
}
else if (keyboardIsActive)
{
Cursor.visible = false;
}
return base.ShouldActivateModule();
}
public override void Process()
{
var usedEvent = SendUpdateEventToSelectedObject();
if (eventSystem.sendNavigationEvents)
{
if (!usedEvent)
usedEvent |= SendMoveEventToSelectedObject();
if (!usedEvent)
SendSubmitEventToSelectedObject();
}
if (m_cursorVisible != Cursor.visible)
{
// Cursor visibility has changed.
m_cursorVisible = Cursor.visible;
if (!Cursor.visible)
{
// Cursor is not visible, so clear the current selection to remove any
// mouse highlight. Unfortunately this also clears the selected game
// so we need to restore the selection afterwards.
var selected = eventSystem.currentSelectedGameObject;
ClearSelection();
eventSystem.SetSelectedGameObject(selected);
}
}
if (Cursor.visible)
{
var process = true;
if (!m_clearFocusOnEmptyClick)
{
// If the mouse is not hovering over anything and a mouse button is pressed or released,
// don't process mouse events to avoid clearing the current selection.
var mouseData = GetMousePointerEventData();
var buttonState = mouseData.GetButtonState(PointerEventData.InputButton.Left);
var leftButtonData = buttonState.eventData.buttonData;
var hoverObject = leftButtonData.pointerCurrentRaycast.gameObject;
if (hoverObject == null && (mouseData.AnyPressesThisFrame() || mouseData.AnyReleasesThisFrame()))
process = false;
}
if (process)
ProcessMouseEvent();
}
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment