I was searching for a way to decouple the editor window GUI from the logic in it. I also wanted to incorporate the built in Unity serialization for data persistence in the editor window after pressing play/some other assembly reload.
During my search I tried different approaches. I wanted to build modular components I could plug into the editor window, each component representing an action taken in the editor window. It was clear to me that the logic and visual implementations of the action would have to be separate.
At first I tried having 3 different classes: one for the logic, a second for the visual representation and a third as the “glue”/controller between them. Having the glue/controller know what are it’s corresponding visual and logic related classes. Sort of an MVC…but problems arose:
Unity doesn’t serialize delegates/events
I had a problem linking any events from the visual side/class to other parts because Unity doesn’t serialize delegates/events and this information is lost after an assembly reload. So for example the click of a button in the editor window would stop being connected after I pressed play in the editor (the delegate/event invocation list would be empty).
Unity doesn’t serialize System.Type
Looking for a solution I thought I could use reflection instead of delegates/events to connect between say: a button click on the visual side to the controller/”glue”, but again as I found out, Unity doesn’t serialize “System.Type” either – so the whole idea of reflection went flying out of the window.
Finally I decided to use only 2 classes (the logic and the visual side) of an action and rely on generics. So first I would create the logic class and then create a visual representation in the form of a visual class (One thing to remember is that Unity has a problem serializing generics…So you need to have specific implementation of generic classes in order to serialize them – This mean you need to serialize a derived class of the generic class).
In the process I was also trying to implement my own Undo/Redo system for actions taken in the editor window. Here is a short video of what I came up with:
I am making this available to the community at my github repository.
It’s a fully working project but it also still a work in progress:
I haven’t implemented persistence for the system if the window is closed/Editor layout reset. I am guessing it would have to be done using an asset or using a custom serialization method.
Feel free to download or use what you need.
This project is highly documented in code with a separate “How to use” guide as well.
Any comments/questions and suggestions are welcome.
Nims