Search Unity

Creating a hotkey to get into "Edit Collider" mode

Discussion in 'Immediate Mode GUI (IMGUI)' started by FeastSC2, Jun 7, 2017.

  1. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    748
    Is there a way to create a hotkey to get into "Edit Collider" mode of a polygon collider 2d?
     
  2. PsyKaw

    PsyKaw

    Joined:
    Aug 16, 2012
    Posts:
    101
    You should look at UnityEditorInternal.EditMode. There are EditMode.ChangeEditMode & EditMode.QuitEditMode.
     
  3. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    748
    Thanks, that just might to be the solution. I don't quite know how to make it work though, I see no info on this on the Unity documentation. Do you know more about this?

    Code (CSharp):
    1. private static void EditCollider()
    2.         {
    3.             var sel = Selection.activeGameObject;
    4.             var col = sel.GetComponent<PolygonCollider2D>();
    5.             if (!col) return;
    6.  
    7.             UnityEditorInternal.EditMode.ChangeEditMode(EditMode.SceneViewEditMode.Collider, , UnityEditor.Editor.CreateEditor(sel));
    8.             Debug.Log("EditMode: " + UnityEditorInternal.EditMode.editMode);
    9.         }
     
  4. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    6,484
    I think you can hook into the Event system to capture current events and keycodes but another way is to make a MenuItem and assign a hotkey chord to it, then Unity will automatically pick it up.

    Code (csharp):
    1. [MenuItem ("Edit/Custom/Edit Collider Mode #_e")] // This is Shift + e
    2. private static void EditCollider()
    3.         {
    4.             var sel = Selection.activeGameObject;
    5.             var col = sel.GetComponent<PolygonCollider2D>();
    6.             if (!col) return;
    7.  
    8.             UnityEditorInternal.EditMode.ChangeEditMode(EditMode.SceneViewEditMode.Collider, , UnityEditor.Editor.CreateEditor(sel));
    9.             Debug.Log("EditMode: " + UnityEditorInternal.EditMode.editMode);
    10.         }
     
  5. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    748
    Yea that's pretty cool but the issue here really is the code I shared. It doesn't work because I'm not sure what to fill this line of code with:
    Code (CSharp):
    1. UnityEditorInternal.EditMode.ChangeEditMode
     
  6. PsyKaw

    PsyKaw

    Joined:
    Aug 16, 2012
    Posts:
    101
    This code works, enjoy!

    Code (csharp):
    1.  
    2. [MenuItem("Edit/Custom/Edit Collider Mode #_e")] // This is Shift + e
    3. private static void EditCollider()
    4. {
    5.     var sel = Selection.activeGameObject;
    6.     var col = sel.GetComponent<Collider2D>();
    7.  
    8.     if (!col)
    9.         return;
    10.  
    11.     if (UnityEditorInternal.EditMode.editMode == EditMode.SceneViewEditMode.Collider)
    12.     {
    13.         UnityEditorInternal.EditMode.ChangeEditMode(UnityEditorInternal.EditMode.SceneViewEditMode.None, new Bounds(), null);
    14.     }
    15.     else
    16.     {
    17.         Type colliderEditorBase = System.Type.GetType("UnityEditor.ColliderEditorBase,UnityEditor.dll");
    18.         Editor[] colliderEditors = Resources.FindObjectsOfTypeAll(colliderEditorBase) as Editor[];
    19.  
    20.         if (colliderEditors == null || colliderEditors.Length <= 0)
    21.             return;
    22.  
    23.         UnityEditorInternal.EditMode.ChangeEditMode(UnityEditorInternal.EditMode.SceneViewEditMode.Collider, col.bounds, colliderEditors[0]);
    24.     }
    25.    
    26.     Debug.Log("EditMode: " + UnityEditorInternal.EditMode.editMode);
    27. }
    28.  
     
    llMarty and FeastSC2 like this.
  7. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    748
    Great! Thank you!
     
  8. llMarty

    llMarty

    Joined:
    Dec 14, 2014
    Posts:
    33
    Great script, but the hotkey is not working in MacOS. However using "%e" works for CMD+E. Also you should also check if sel is null, so it won't error, if there is no object selected.
     
  9. juancarlosthe4th

    juancarlosthe4th

    Joined:
    Jun 14, 2017
    Posts:
    1
    I'm using Unity 2019.3.0f6 and when I step through this script in the debugger it is changing the value of "UnityEditorInternal.EditMode.editMode" but not opening edit mode. Can anyone get this working in a new version of unity?

    Also just double checking, is the expected behavior of the script above the same as clicking on the button in the attached screenshot?
     

    Attached Files:

unityunity