- Home /
Question by
unity_p1ZEBD9IvIhlOQ · Jan 20, 2020 at 10:49 PM ·
editor-scriptingcustom editorgizmos
Custom gizmo tools
I am starting out making some custom gizmo tools. I first started to put my code in OnSceneGUI which worked fine, but unfortunately the gizmo was only shown when the target GameObject was selected.
[CustomEditor(typeof(Firefly))]
class FireFlyEditor : Editor
{
private int id = -1;
void OnSceneGUI()
{
if (id == -1)
{
id = GUIUtility.GetControlID(target.GetHashCode(), FocusType.Passive);
}
Event e = Event.current;
switch (e.GetTypeForControl(id))
{
case EventType.Layout:
// code ...
break;
case EventType.MouseDown:
// code ...
break;
case EventType.MouseDrag:
// code ...
break;
case EventType.Repaint:
// code ...
break;
}
}
}
This answer suggested another approach, using a static function:
[CustomEditor(typeof(Firefly))]
class FireFlyEditor : Editor
{
private static Dictionary<Transform, int> ids = new Dictionary<Transform, int>();
[DrawGizmo(GizmoType.NotInSelectionHierarchy | GizmoType.InSelectionHierarchy)]
static void Draw(Transform fly, GizmoType gizmoType)
{
if (!ids.ContainsKey(fly) || ids[fly] == -1)
{
ids.Add(fly, GUIUtility.GetControlID(fly.GetHashCode(), FocusType.Passive));
}
int id = ids[fly];
Event e = Event.current;
switch (e.GetTypeForControl(id))
{
case EventType.Layout:
// code ...
break;
case EventType.MouseDown:
// code ...
break;
case EventType.MouseDrag:
// code ...
break;
case EventType.Repaint:
// code ...
break;
}
}
}
Using the second approach, every custom gizmo is drawn, regardless of selection. But unfortunately only the repaint event is triggered, the others (mouse drag, mouse down, layout) are not which makes it not that useful.
Any help is appreciated.
Comment