It seemed to be a truly simple problem, but after 3 days of struggling and learning Unity internals I feel a bit hopeless.
##The problem
My desire is to be able to see and edit in Inspector the attributed properties of my MonoBehaviour. If a property is a simple type (int, string), I want to see a default edit control for it. If it’s a complex object, I want to recursively show its properties in a tree-like structure:
I want to keep it generic, so that I can use it with multiple classes without modifying my editor code.
##What I did
-
I started with the popular script ExposePropertiesInInspector, which is basically showing in inspector the properties of simple types marked with ExposeProperties attribute. I added the ExposeProperties to some properties in my ActorBehaviour and ActorData classes.
-
I made empty classes named ExposableMonoBehaviour (for scripts like ActorBehaviour to derive from it) and ExposableObject (for data classes like ActorData to derive from it).
-
I made editors for them. In OnInspectorGUI, the editors draw the default inspector, use reflection to fetch the properties marked with ExposeProperty attribute, and if the property is deriving from ExposableObject, they add indent and recursively create an editor to show the object’s properties beneath.
It does work, but not entirely.
##Problems I encountered
-
Seems like I can’t create an editor for a class not deriving from UnityEngine.Object. I chose ScriptableObject because it seemed most lightweight for me and made my ExposableObject derive from it. I thought I can swallow using it, but then I realised ScriptableObjects can’t be created in unit test context! but I need to use my ExposableObjects alot in testing.
-
Although the serialization and deserialization is working well for ExposableObjects (all the properties are nicely saved when needed and retrieved to previous state after quitting play mode), there is one issue with them — after duplicating a GameObject, the new one shares the same instance of my ExposableObject. So when I change Energy in ActorData of one object, it’s changed also for the other one.
#So…
How it can be helped? Am I doing something wrong? Am I aiming for too much?