DrawGizmo - Accessing Editor script in DrawGizmo
I have been making a custom editor for a hexagon grid, and also a custom gizmo for that grid using the [DrawGizmo] attribute.
It currently works basically like this:
I've got the HexGrid class, that contains all the functionality for storing and communicating with the hex grid:
public class HexGrid : MonoBehaviour { public float HexRadius; public Color GizmoColor; public int GizmoDrawRange; // Rest of class... }
And then I've got the HexGridEditor class that also contains the HexGridGizmo method, that uses the [DrawGizmo] attribute:
[CustomEditor(typeof(HexGrid))] public class HexGridEditor : Editor { // Variables, references, functions... [DrawGizmo(GizmoType.NotInSelectionHierarchy | GizmoType.InSelectionHierarchy | GizmoType.Pickable)] static void HexGridGizmo(HexGrid hexGrid, GizmoType gizmoType) { Color color = hexGrid.GizmoColor; int DrawRange = hexGrid.GizmoDrawRange; // Rest of gizmo functionality... } }
This works fine, but it's not how I want it organized. I ideally want all editor and gizmo functionality to be contained in the HexGridEditor class, and have the HexGrid class only contain runtime functionality. So to be more precise, I want the GizmoColor and GizmoDrawRange fields to be contained in the editor class, but I can't get that to work.
If I just put the GizmoColor and GizmoDrawRange fields in the HexGridEditor class I can't reference them from the HexGridGizmo function, because it's a static function. And it has to be static according to the script reference.
[CustomEditor(typeof(HexGrid))] public class HexGridEditor : Editor { public Color GizmoColor; public int GizmoDrawRange; [DrawGizmo(GizmoType.NotInSelectionHierarchy | GizmoType.InSelectionHierarchy | GizmoType.Pickable)] static void HexGridGizmo(HexGrid hexGrid, GizmoType gizmoType) { Color color = GizmoColor; int DrawRange = GizmoDrawRange; // DOES NOT WORK: // Can't reference GizmoColor and GizmoDrawRange from static method. } }
The renderer function takes as its first argument the object for which the gizmo is being drawn, and as we saw in my current setup (first two code snippets) that lets me reference that objects fields, so what if I use the HexGridEditor class as that argument instead of the HexGrid class? That doesn't work either and throws a strange (and seemingly undocumented) error: classID != -1.
[CustomEditor(typeof(HexGrid))] public class HexGridEditor : Editor { public Color GizmoColor; public int GizmoDrawRange; [DrawGizmo(GizmoType.NotInSelectionHierarchy | GizmoType.InSelectionHierarchy | GizmoType.Pickable)] static void HexGridGizmo(HexGridEditor hexGridEditor, GizmoType gizmoType) { Color color = HexGridEditor.GizmoColor; int DrawRange = HexGridEditor.GizmoDrawRange; // THROWS ERROR (in Unity): classID != -1. } }
So how do I solve this in a tidy way? Is there some way to pass the HexGridEditor class into the gizmo function? Some other way, where editor and runtime functionality is neatly separated?
Much thanks in advance!
Answer by unityAston · Feb 25, 2016 at 02:43 AM
I got help via reddit from /user/Sarkahn on /r/Unity3D:
Whew, that's a pretty huge wall of text for such a simple problem! You're really overcomplicating it - passing the Editor to the DrawGizmos doesn't really make any sense.
If I'm understanding this right you have two editor-specific values ( GizmoColor and DrawRange) that you want to be moved out of the runtime class and into the editor class.
If you want editor-specific data to live only in your editor class just stick them in your editor class. You say you want to use them for DrawGizmos in your editor class and DrawGizmos is static, so any variables it uses need to be static. So make your variables static.
If you need your variables to instance specific ( so different hex grids have different colors and draw ranges) and you absolutely must keep them out of your hex-grid class then the simplest solution is EditorPrefs.
TLDR: Make your drawRange and gizmoColor variables static.
Your answer
Follow this Question
Related Questions
Property Drawer ArgumentException 1 Answer
Good practices for storing readonly data 0 Answers
How to properly import mesh C# Editor 1 Answer
Why am I getting this strange behaviour? 1 Answer
Custom Editors - Array of Custom Classes 2 Answers