- Home /
On quest for generic editor to allow inspectability and testability of plain data classes
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?
I'm not sure I understand the problem. The text in your problem statement is exactly what Unity does by default:
$$anonymous$$y desire is to be able to see and edit in Inspector the attributed properties of my $$anonymous$$onoBehaviour. 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:
Is the problem that some things are appearing in the inspector that you don't want, or that some things you want to appear do not?
Or I guess is the problem that you want to display properties rather than fields (the latter of which is what Unity supports by default)?
Exactly, I'm talking about properties. I don't want to use public fields when I don't need to.
Answer by Adam-Mechtley · Jan 17, 2018 at 08:59 AM
Is there a reason you cannot do this the recommended way that still works with Unity's serialization system? E.g., simply add a private backing field for your property with SerializeField attribute
Wow, actually I somehow omitted this approach, rushing to use ExposePropertiesInInspector. However, there's one big issue with doing so — changing the values in the editor affects the fields directly, ignoring the properties encapsulating those fields. In some cases this is not acceptable (e.g. when I need to do some additional stuff in property getter/setter). So my problem is not solved, but honestly the approach you proposed should work for most of my use cases. For the record, by browsing through Unity Editor extensions I stumbled upon Odin Inspector, which I think handles easily the case I'm describing. It's not free though.
I have this free package which might help
Your answer
Follow this Question
Related Questions
How should I serialize data that is also editable in the Inspector? 2 Answers
[Solved]Why doesn't my ScriptableObject based asset save using a custom inspector ? 1 Answer
Why doesn't my ScriptableObject save using a custom EditorWindow? 3 Answers
Referencing / linking a .asset / .prefab file in another .asset / .prefab file programmatically. 2 Answers
Unity inspector scripting - scriptable objects - card game 0 Answers