- Home /
Custom Editors for classes that derive from system.object
Is there a way to override Unity's editor / inspector for standard classes deriving from System.Object? It appears to only work on classes deriving from MonoBehaviour.
For example, below, I'd like to hide or show 'exponent' based on the type selected in the editor. Obviously it would still serialize both values, but this is for editor display (and more complex cases).
[System.Serializable] public sealed class NormalizedCurve{
public enum Type{Linear, Exponential}
[SerializeField]
Type type;
[SerializeField]
float exponent;
}
Answer by Dreamora · Dec 19, 2010 at 06:17 AM
I don't think so but you can derive from Object (technically UnityEngine.Object) which forms the base of all UnityEngine classes
Can you do that? For some reason I think I got errors when trying to inherit from anything $$anonymous$$onoBehavior inherits from (including UnityEngine.Object).
I suppose I could derive from ScriptableObject (since there are actually constructors for that). Still, I'd like to show the properties of this object in-line, without having to select the scriptableObject instance.
Answer by Dreamora · Dec 27, 2010 at 05:29 AM
To answer Comment and concerns:
You only get problems by having stuff thats already "beyond" UnityEngine.Object normally (ie scriptableobject and its extends Component, Behaviour and MonoBehaviour) and even there only when you try to use New instead of AddXXX
As you marked it as serializable it will normally always show on the editor, just be aware of one major problem: As its serializable and not initialized by unity upon scene start (as scriptableobject, component and behaviours are) its treated similar to a struct, ie its always there basically, already in the editor even if not setup yet and out of my experience that then will lead to serious trouble (i've had totally undebuggable phantom issues due to a an own tree alike datastructure where the classes were serializable so I could see them in the editor)
I highly recommend to not mark it serializable if its directly exposed on the editor. Either make it serializable and write an EditorWindow that allows you to access it or make it serializable and mark its field on the monobehaviour in question with [HideInInspector]
If you need editor exposure, use ScriptableObject or SerializableObject instead (give SeriazableObject / SerializableProperty a look in the docs - its a bit scarse but you seem to be aware of the basics in relation to serialization :))