Is there a way to create a hotkey to get into “Edit Collider” mode of a polygon collider 2d?
You should look at UnityEditorInternal.EditMode. There are EditMode.ChangeEditMode & EditMode.QuitEditMode.
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?
private static void EditCollider()
{
var sel = Selection.activeGameObject;
var col = sel.GetComponent<PolygonCollider2D>();
if (!col) return;
UnityEditorInternal.EditMode.ChangeEditMode(EditMode.SceneViewEditMode.Collider, , UnityEditor.Editor.CreateEditor(sel));
Debug.Log("EditMode: " + UnityEditorInternal.EditMode.editMode);
}
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.
[MenuItem ("Edit/Custom/Edit Collider Mode #_e")] // This is Shift + e
private static void EditCollider()
{
var sel = Selection.activeGameObject;
var col = sel.GetComponent<PolygonCollider2D>();
if (!col) return;
UnityEditorInternal.EditMode.ChangeEditMode(EditMode.SceneViewEditMode.Collider, , UnityEditor.Editor.CreateEditor(sel));
Debug.Log("EditMode: " + UnityEditorInternal.EditMode.editMode);
}
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: UnityEditorInternal.EditMode.ChangeEditMode
This code works, enjoy!
[MenuItem("Edit/Custom/Edit Collider Mode #_e")] // This is Shift + e
private static void EditCollider()
{
var sel = Selection.activeGameObject;
var col = sel.GetComponent<Collider2D>();
if (!col)
return;
if (UnityEditorInternal.EditMode.editMode == EditMode.SceneViewEditMode.Collider)
{
UnityEditorInternal.EditMode.ChangeEditMode(UnityEditorInternal.EditMode.SceneViewEditMode.None, new Bounds(), null);
}
else
{
Type colliderEditorBase = System.Type.GetType("UnityEditor.ColliderEditorBase,UnityEditor.dll");
Editor[] colliderEditors = Resources.FindObjectsOfTypeAll(colliderEditorBase) as Editor[];
if (colliderEditors == null || colliderEditors.Length <= 0)
return;
UnityEditorInternal.EditMode.ChangeEditMode(UnityEditorInternal.EditMode.SceneViewEditMode.Collider, col.bounds, colliderEditors[0]);
}
Debug.Log("EditMode: " + UnityEditorInternal.EditMode.editMode);
}
Great! Thank you!
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.
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?
Try this:
[MenuItem("Tools/Edit Polygon Collider")]
static void edit_polygon_collider() {
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (var assembly in assemblies) {
if (assembly.GetType("UnityEditor.PolygonCollider2DTool") != null) {
UnityEditor.EditorTools.EditorTools.SetActiveTool(assembly.GetType("UnityEditor.PolygonCollider2DTool"));
}
}
}
In settings, bind this hot function to any shortcut you want.
in u2022 SetActiveTool is now using ToolManager instead of EditorTools :
using UnityEditor.EditorTools;
(...)
ToolManager.SetActiveTool(type);
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 ![]()
You can get the type of the active tool via:
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”
[MenuItem("DoomTurtle/Tools/Edit Collider Mode #_e")] // This is Shift + e
static void EditCollider()
{
var EditorTypes = typeof(EditorTool).Assembly.GetTypes();
var SelObj = Selection.activeGameObject;
if (SelObj)
{
var BoxToolType = EditorTypes.FirstOrDefault(x => x.FullName == "UnityEditor.BoxPrimitiveColliderTool");
var SphereToolType = EditorTypes.FirstOrDefault(x => x.FullName == "UnityEditor.SphereColliderTool");
if (SelObj.GetComponent<BoxCollider>())
{
UnityEditor.EditorTools.ToolManager.SetActiveTool(BoxToolType);
}
else if (SelObj.GetComponent<SphereCollider>())
{
UnityEditor.EditorTools.ToolManager.SetActiveTool(SphereToolType);
}
}
//Debug.LogError("Active tool type: " + UnityEditor.EditorTools.ToolManager.activeToolType);
}
