Search Unity

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

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:
    978
    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:
    102
    You should look at UnityEditorInternal.EditMode. There are EditMode.ChangeEditMode & EditMode.QuitEditMode.
     
  3. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    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:
    7,442
    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:
    978
    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:
    102
    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:
    978
    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:

  10. Vitalya_dev_3

    Vitalya_dev_3

    Joined:
    Oct 16, 2016
    Posts:
    1
    Try this:
    Code (CSharp):
    1. [MenuItem("Tools/Edit Polygon Collider")]
    2.     static void edit_polygon_collider() {
    3.         var assemblies = AppDomain.CurrentDomain.GetAssemblies();
    4.         foreach (var assembly in assemblies) {
    5.             if (assembly.GetType("UnityEditor.PolygonCollider2DTool") != null) {
    6.                 UnityEditor.EditorTools.EditorTools.SetActiveTool(assembly.GetType("UnityEditor.PolygonCollider2DTool"));
    7.             }
    8.         }
    9.     }
    In settings, bind this hot function to any shortcut you want.
     
    EversorDvL likes this.
  11. ABerlemont

    ABerlemont

    Joined:
    Sep 27, 2012
    Posts:
    67
    in u2022 SetActiveTool is now using ToolManager instead of EditorTools :
    Code (CSharp):
    1.  
    2.     using UnityEditor.EditorTools;
    3.     (...)
    4.     ToolManager.SetActiveTool(type);
    5.  
    Any idea on how to do the same to edit Spline of SpriteShapeController ?
    I searched in the assemblies for various types (like PolygonCollider2DTool) that would be compatible but couldn't find the right one to feed to the ToolManager.

    Thanks :)
     
    ramonster1986 likes this.
  12. DTAli

    DTAli

    Joined:
    Jan 22, 2016
    Posts:
    52
    You can get the type of the active tool via:

    Code (CSharp):
    1. Debug.LogError("Active tool type: " + UnityEditor.EditorTools.ToolManager.activeToolType);
    Put that in an editor function with a shortcut, select your object, enable the tool, then log the name of it.

    That's how I was able to find the tool names for sphere and box colliders: "UnityEditor.BoxPrimitiveColliderTool" and "UnityEditor.SphereColliderTool"

    Code (csharp):
    1.  
    2. [MenuItem("DoomTurtle/Tools/Edit Collider Mode #_e")] // This is Shift + e
    3. static void EditCollider()
    4. {
    5.     var EditorTypes = typeof(EditorTool).Assembly.GetTypes();
    6.  
    7.     var SelObj = Selection.activeGameObject;
    8.     if (SelObj)
    9.     {
    10.         var BoxToolType = EditorTypes.FirstOrDefault(x => x.FullName == "UnityEditor.BoxPrimitiveColliderTool");
    11.         var SphereToolType = EditorTypes.FirstOrDefault(x => x.FullName == "UnityEditor.SphereColliderTool");
    12.  
    13.         if (SelObj.GetComponent<BoxCollider>())
    14.         {
    15.             UnityEditor.EditorTools.ToolManager.SetActiveTool(BoxToolType);
    16.         }
    17.         else if (SelObj.GetComponent<SphereCollider>())
    18.         {
    19.             UnityEditor.EditorTools.ToolManager.SetActiveTool(SphereToolType);
    20.         }
    21.     }
    22.  
    23.     //Debug.LogError("Active tool type: " + UnityEditor.EditorTools.ToolManager.activeToolType);
    24. }
    25.