Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Key binding override when playing in Editor

Discussion in 'Input System' started by MUGIK, May 22, 2021.

  1. MUGIK

    MUGIK

    Joined:
    Jul 2, 2015
    Posts:
    473
    Hi!
    In my game I want to open a pause menu on pressing Escape, and when this happens I want to control the cursor state by myself.
    But as stated in docs:
    In the Editor the cursor is automatically reset when escape is pressed, or on switching applications.

    This means that when I pressing Escape in the editor I'm triggering both my system and unity, which results in hard to catch issues with the cursor state.
    Also, I like that in editor when I'm pressing escape I can see and move my cursor to interact with the editor itself.

    So the solution is simple - use a different button to open pause menu when running in editor. But how to do that?

    Creating one more control scheme sounds like overkill.
    And it's tough to maintain two identical schemes that have only one difference. Also, it requires writing additional code to enable editor-scheme
     
  2. I currently have this on my MenuHandling stuff:
    Code (CSharp):
    1.         private void OnEnable()
    2.         {
    3.             inputActionAsset.Enable();
    4.             var action = inputActionAsset.FindAction("OpenPauseMenu");
    5. #if UNITY_EDITOR
    6.             action.ApplyBindingOverride("<Keyboard>/tab", path: "<Keyboard>/escape");
    7. #endif
    8.             action.performed += TryPauseGame;
    9.         }
    10.  
    Obviously the
    #if UNITY_EDITOR
    stuff is the important. Now, it is a quick and dirty. If you want robust stuff, you can create a second Action in your Asset, like "OpenPauseMenuInEditor" and add some bindings, and then you can override the bindings with all of those inside this #if. So you can keep the "in-editor binding" in the InputActionAsset next to the "real" one. But I will leave that to you, I'm currently good with this dirty one. :D
     
    Last edited by a moderator: May 22, 2021
  3. MUGIK

    MUGIK

    Joined:
    Jul 2, 2015
    Posts:
    473
    Thank you so much!
     
  4. No problem. Obviously if you use the TAB key for anything else, just look up the path of the desired key and replace it. I currently use the TAB in the editor instead of the ESC.