- Home /
Serialising subclass variables in inspector
Is it possible to serialise the variables in a subclass in the inspector (In list)?
At the moment I have a sub class, a base class, and a "class manager" that has a list of type baseclass. In runtime it adds classes of type subclass into the list of baseclass.
But when the classes are added to the list and updates the inspector, it looks like the inspector only shows the baseclass variables, and none of the subclass' variables.
For the most part it looks like the list supports subclasses, and I can call subclass functions and whatnot, but the inspector still doesn't serialise the subclass variables?
Is there a fix for this? Or should I find a different solution for having a list of different subclasses?
BaseClass.cs :
using UnityEngine;
using System.Collections;
[System.SerializableAttribute]
public class BaseClass {
public string name;
public int baseInt;
public float baseFloat;
public virtual void TestFunction(float flt)
{
baseFloat -= flt;
Debug.Log("TestFunction baseclass called.");
}
}
SubClass.cs
using UnityEngine;
using System.Collections;
[System.Serializable]
public class SubClass : BaseClass {
[SerializeField]
public float subFloat;
public override void TestFunction(float flt) {
base.TestFunction(flt);
Debug.Log(subFloat);
}
}
ClassManager.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ClassManager : MonoBehaviour {
public List<BaseClass> classList = new List<BaseClass>();
BaseClass currentClass;
public void Button() {
BaseClass wow = new SubClass();
AddClassToList(wow);
currentClass = classList[0];
}
public void OtherButton() {
currentClass.TestFunction(0.5f);
}
public void AddClassToList(BaseClass bs) {
classList.Add(bs);
print(bs.GetType());
}
public void RemoveClassFromList(BaseClass bs) {
classList.Remove(bs);
}
}
Just looked up this post and the last code chunk might just do the trick. If not, the whole post looks like very interesting for you:
http://answers.unity3d.com/questions/51615/do-custom-inspectors-support-inheritance.html
The question you've linked is not relevant here. The question was about $$anonymous$$onoBehaviour derived classes and their custom inspectors. Custom inspectors can't change how things are serialized, only how they are displayed in the inspector. The code WhyDoIDoIt posted is no longer relevant as Unity now supports writing a custom inspector for the base class and have it apply to derived classes as well. That's why the accepted answer is the accepted answer ^^.
Answer by Bunny83 · Oct 21, 2016 at 11:24 PM
No, Unity doesn't support inheritance for custom serializable classes. Only classes derived from UnityEngine.Object support inheritance when it comes to serialization in Unity. The only classes you can actually use as base class are MonoBehaviour (which always have to be attached to a gameobject) or ScriptableObject.
Unity serializes custom serializable classes based on the variable type. So no matter what actual instance the variable references it will always be serialized as base class.
Update:
Since Unity 2019.3 there's an update:
Kernel: Introducing new serialization attribute: SerializeReference
Unity now has the SerializeReference attribute which adds some new features to the serialization system and allows custom serializable classes to be serialized "as reference". However those references are still local to the housing object. So they are serialized alongside with the housing object. Though such fields now support inheritance and the type of the instance is no longer determined by the field type but the actual type.
This adds makes it more versatile, but at the same time the inspector will no longer create those classes for you since even "null" is now supported. As I mentioned above those references can't span across multiple components or scriptable objects. Those can only reference other custom serializable classes that Unity's serialization system supports.
As you can read in the documentation such fields now also support interfaces or abstract types (since they are no longer bound to their field type)
Answer by Ruzihm · Mar 10, 2021 at 06:51 AM
The above answer is out of date with the introduction of the SerializeReference decorator. Adding this decorator to the above code will now produce the desired outcome:
[SerializeReference] public List classList = new List();
Bless you! You have no idea how much pain you just spared me.
Your answer
Follow this Question
Related Questions
*Really* need help on Serialisation, stuck on this for over a week now, deadline looming 1 Answer
Serialize variables of elements in a list ? 0 Answers
Difference between assigning a value in inspector and with a custom editor script 1 Answer
A node in a childnode? 1 Answer
How do I make child classes in a list editable from the Inspector? 1 Answer