Search Unity

Disable built-in menu items, override built-in hotkeys

Discussion in 'Scripting' started by nickfourtimes, Feb 14, 2014.

  1. nickfourtimes

    nickfourtimes

    Joined:
    Oct 13, 2010
    Posts:
    219
    More of a philosophical question, but is it possible to disable/remove some of the built-in editor menus and menu items ("File/" or "Edit/Cut" &c), or override some of the built-in hotkeys (Ctrl-S to save, &c)? Curious how malleable the editor can be.
     
  2. testure

    testure

    Joined:
    Jul 3, 2011
    Posts:
    81
    As far as built in menu items go, you're not going to be able to remove them from the menu- but with some hacky techniques you can definitely intercept the functionality and override it with reflection.

    Overriding hotkeys is infinitely easier, you just need a custom editor or editorwindow and check for input in OnGUI/OnSceneGUI. Just remember that most of unity's hotkeys happen on keydown, not keyup.

    As an example, one of the new hotkeys introduced in 4.3 is the "2" key- it will change your scene view to "2D" mode. To override that, it's as easy as the following code:

    Code (csharp):
    1.  
    2. void OnSceneGUI()
    3. {
    4.     if ( Event.current.type == EventType.keyDown )
    5.     {
    6.         if ( Event.current.isKey  Event.current.keyCode == KeyCode.Alpha2 )
    7.         {
    8.             Debug.Log( "Number 2 was pressed, default unity hotkey is overridden." );
    9.             Event.current.Use();    // if you don't use the event, the default action will still take place.
    10.         }
    11.     }
    12. }
     
  3. DrDecipher

    DrDecipher

    Joined:
    Dec 5, 2012
    Posts:
    54
    Nice post.