- Home /
Custom Editor script to show non-native class in inspector
So I have a class that is not extending anything (apart from System.Object of course) and therefore variables of that type don't show up in the inspector. I tried writing a CustomPropertyDrawer, but it still doesn't show the variable. This is my code:
using UnityEditor;
using UnityEngine;
[CustomPropertyDrawer(typeof(AScriptableObjectClassThatShouldDisplayTheVariableInItsInspector))]
public class ShowVariable : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
base.OnGUI(position, property, label);
EditorGUI.BeginProperty(position, label, property);
Rect rect = new Rect(position.x, position.y, 50, position.height);
EditorGUI.PropertyField(rect, property.FindPropertyRelative("nameOfVariable"));
EditorGUI.EndProperty();
}
}
This is the class that contains a variable of the class that should be displayed:
[System.Serializable]
public class Syntax : ScriptableObject
{
public GameLevelFunction nameOfVariable;
}
And this is basically how my class that should be displayed in the inspector looks like:
[System.Serializable]
public abstract class GameLevelFunction
{
//literally two methods
}
I'm pretty sure the problem is with it being an abstract class, because Unity should serialize non-mono classes by default (if they have system.serializable attribute)
Answer by Bunny83 · Oct 21, 2019 at 02:54 PM
I've answered this question now so many time ^^. The last time just a couple of hours ago. So I won't go into much details. The issue it that Unity's serialization system does not support polymorphism for custom serializable classes. Such classes are not referenced since they don't have a referencable ID or something. They behave just like structs. Pure data containers. No type information is stored in the serialized data.
Unity can only serialize references for assets. And that's either MonoBehaviour derived classes or ScriptableObject derived classes. However both need to be stored as asset in your project. So if you want inheritance you might want to use ScriptableObjects.
If you have about an hour I really recommend watching this video about ScriptableObjects
Oh wow, I can't believe I didn't get notified about this. I kinda forgot what I wanted to do back then and have abandoned the project by now anyways. So, uhh, thanks for the answer and sorry for the late reply.