Skip to content

Instantly share code, notes, and snippets.

@benthroop
Created February 5, 2023 20:56
Show Gist options
  • Save benthroop/fb721d0007e907dc6bbcc9faf4197f5f to your computer and use it in GitHub Desktop.
Save benthroop/fb721d0007e907dc6bbcc9faf4197f5f to your computer and use it in GitHub Desktop.
Fix a Unity annoyance with mouse cursor and play mode button.
/*
This fixes an issue you'd encounter if...
1. You hit the play button with the mouse cursor.
2. You lock/hide the mouse cursor.
3. You click a mouse button (presumably right or middle mouse) to show the mouse cursor again.
In this case, you will find that the mouse cursor is still on the play button, and the click which unhides the cursor
will press the play button, thus stopping the editor. This happened to me most often when I would use a gamepad after
hitting play, but would want to edit something via the mouse. Not a common issue, but once you accidentally stop your
game several times, a fix becomes desirable.
Ben Throop
2/5/2023
*/
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.InputSystem;
[InitializeOnLoad]
public static class OMGMoveTheCursorOnPlayMode
{
static OMGMoveTheCursorOnPlayMode()
{
EditorApplication.playModeStateChanged += LogPlayModeState;
}
//Only works with New Input System
private static void LogPlayModeState(PlayModeStateChange state)
{
Mouse.current.WarpCursorPosition(new Vector2(0,0));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment