Hey!
I have the following class:
public class myClass: EditorWindow
And in it I have tons of ‘EditorGUILayout’ code and it is taking up too much space.
I have created a namespace called ‘myNameSpace’ with several helper functions defined in it that I can easily call in ‘myClass’ by typing:
HelperClass.HelperFunction();
Now I would like to put all of the messy and long ‘EditorGUILayout’ code in a separate script/class and then I want to just be able to call a function like ‘drawGUIStuff();’ in my ‘myClass’ script but I can’t figure it out.
With all the previous helper functions, they all derived from MonoBehaviour and I could just define functions in them and then call them inside of MyClass (MyClass is ‘using’ the myNameSpace namespace). But now since it’s editor code it needs to derive from Editor and I’m stuck.
If anyone could point me in the right way, that would be awesome!
Alex
PS:
I have hundreds and hundreds of lines with just things like ‘EditorGUILayout.Label’ and I want to make it simpler. If you think I’m not handling this right and just moving the code to another script is not the way things should be done, please let me know!
First off, having hundreds of lines in a single editor file is fine. its code that your not releasing with your game anyway. if your Editor script is a hot mess, it doesn’t matter too much if it looks fine in unity, so long as it works and its fast enough.
If its something that you think you’ll constantly be iterating on, or be a tool you plan to release to the asset store (at which point you should be iterating tons on it anyway). Then it may be helpful that you break it up into multiple classes. Take the Inspector Editor window for example. even if its just showing an empty gameobject and its transform, its easily using a dozen classes (at least) to render everything in that window.
As for using a helper class, just do what I do. Wrap them inside an extensions class. for me I created an interface (IEditorGUILayoutExtentions for example) and made all my extensions tie to the interface (not to an Editor or EditorWindow). Then in said editor classes I could either make the class implement the interface, or more commonly have the interface as a null field(typically named guiHelper or guiLayoutHelper).
I primarily did it for code sugar reasons, so lines like
EditorGUI.BeginChangeCheck();
serializableProperty.intValue = EditorGUILayout.IntField(serializableProperty.intValue);
if(EditorGUI.EndChangeCheck())
{
serializableProperty.serializedObject.ApplyModifiedProperties();
}
could be something like
guiLayoutHelper.IntField(serializableProperty);
however another side benefit is that it gives padding against any possible changes the unity team may do on said classes. I find it highly unlikely that they’ll change it massively, especially in the near future. but if they ever do. I have one place to go to fix all my scripts if such a change broke them.