Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Disable cursor unlocking when pressing ESC

Discussion in 'Editor & General Support' started by illinar, Jun 22, 2020.

  1. illinar


    Apr 6, 2011
    Currently if I press ESC the cursor unlocks until I click on the game window again. This is absolutely disruptive for the game testing. How do I avoid that behavior??
  2. dgoyette


    Jul 1, 2016
    I'm pretty sure there's nothing you can really do. Hitting escape in the editor while in Play Mode will make the mouse cursor visible again. If this is a deal breaker:
    • This will only be an issue in the editor, so you can try testing in a build, where this behavior won't occur.
    • You can pick a different key other than Escape which you use while in the editor, or map an additional key besides Escape to that behavior.
    I'm awaiting the day when we can simply use the Escape key (or Alt keys, for that matter) in Unity without side effects.
  3. Joe-Censored


    Mar 26, 2013
    illinar and PraetorBlue like this.
  4. illinar


    Apr 6, 2011
    Yes, I'm using [ ~ ] key now. But I think the ability to disable this behavior should exist in Unity.
    Marc-Saubion and Denis-535 like this.
  5. Yes, I'm saying for years that we need to be able to redefine all of the shortcuts, not just the ones that have menu item. This includes the ESC when escaping the game view in the editor.
  6. WildcardMoo


    Aug 28, 2020
    I am honestly baffled reading this. The controls in my game are bindable and not hardcoded (as in any game, I hope), with a default key of Esc set for certain actions like pause or exiting menus. I can't just hard code a different key for it in case we're running in the editor. So I show the mouse cursor when entering a menu, and then lock it again when exiting, only for it to reappear because Unity is like "Oh, Escape has been pressed, happy days".

    The fact that Unity ties a completely unnecessary functionality to one of the most commonly used keys, that will be only used in the editor, with no way to disable it or bind it to a different key is simply insane... Who thought that would be a good idea?
  7. Why not? We have the
    for stuff like this.
    If you use the new InputSystem, it's even more easy.
    Code (CSharp):
    1. #if UNITY_EDITOR
    2.         private const string INPUT_ORIG_ESC_KEY = "<Keyboard>/escape";
    3.         private const string INPUT_TEMP_TAB_KEY = "<Keyboard>/tab";
    4. #endif
    5. // [...]
    6. #if UNITY_EDITOR
    7.     private void Awake()
    8.     {
    9.         Input.MainMenu.Back.ApplyBindingOverride(INPUT_TEMP_TAB_KEY, path: INPUT_ORIG_ESC_KEY);
    10.     }
    11. #endif
    Obviously doesn't matter how you get the
    , you can override its binding temporarily.