Custom Editor Inspector member class with inheritance, values resets after starting Play Mode
Hello, let's say I have following classes:
, Manager class which has class A as member:[System.Serializable] public class A { public float a; }
[System.Serializable] public class B : A { public float b; }
[System.Serializable] public class C : A { public float c; }
using UnityEngine;
public class Manager : MonoBehaviour {
public enum Mode { B, C };
public Mode mode = Mode.B;
[SerializeField]
public A classA;
}
and custom inspector for Manager class:
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(Manager))]
public class ManagerEditor : Editor {
private string[] tabNames = { "B", "C" };
Manager myTarget;
private void setClass() {
if(myTarget.mode == Manager.Mode.B) {
if (myTarget.classA.GetType() != typeof(B)) {
myTarget.classA = new B();
}
displayB();
} else if (myTarget.mode == Manager.Mode.C) {
if(myTarget.classA.GetType() != typeof(C)) {
myTarget.classA = new C();
}
displayC();
}
}
private void displayC() {
myTarget.classA.a = EditorGUILayout.FloatField("a", myTarget.classA.a);
(myTarget.classA as C).c = EditorGUILayout.FloatField("c", (myTarget.classA as C).c);
}
private void displayB() {
myTarget.classA.a = EditorGUILayout.FloatField("a", myTarget.classA.a);
(myTarget.classA as B).b = EditorGUILayout.FloatField("b", (myTarget.classA as B).b);
}
public override void OnInspectorGUI() {
myTarget = target as Manager;
myTarget.mode = (Manager.Mode)GUILayout.Toolbar((int)myTarget.mode, tabNames);
setClass();
}
}
Everything seams to be fine, but when I set some values in inspector in edit mode, for example mode C, and a = 1, c = 2:
as soon as I press Play, every value resets:
Is there any way to prevent that and keep values from edit mode in play mode or I am doing something wrong?
Answer by Bunny83 · Jan 07, 2019 at 12:00 AM
What you're doing wrong is that Unity's serialization system does not support inheritance for custom serializable classes. You may want to read the section
When might the serializer behave unexpectedly?
carefully.
Note that custom editor only provide a custom visualization of the serialized data. It can't change the way the serializer works. Custom serializable classes are serialized "inline". The type of the instance is not saved at all, only the values. They actually behave like structs. When the class is deserialized the type of the field is used. So no matter what object you store inside the "classA" field of your Manager, after deserialization it will be an instance of "A" since that's the type of the field.
If you need polymorphism / inheritance you have to use ScriptableObjects or MonoBehaviours, they support inheritance since those are seperately serialized objects. ScriptableObjects are meant to be saved as asset in the project. MonoBehaviours and ScriptableObject instances can be referenced by other serialized objects.
Thank you very much for your aswer. I managed to solve this problem with ScriptableObjects as you advised me. Unfortunately because of this I cannot use Toolbar to automaticly chose mode and set options, but I have to create ScriptableObject assets and than attach them to my script. But maybe thats good because I can save many configurations and switch them any time. Do you know maybe some other more convinient way to automaticly create somehow scriptableobject so user don't have to create that every time?
Your answer
Follow this Question
Related Questions
Custom Editor List with child classes 1 Answer
Is the glitch fixed? 0 Answers
How to reduce editor refresh time? 0 Answers
How to create a drop down menu in editor inspector 0 Answers