I'm trying to build a custom editor for a ScriptableObject. Everything works fine with standard properties (string, enum)
But I'm trying to also add a List/Array, which contains items that all inherit from one blueprint class based on ScriptableObjct.
The idea is that I add elements of various different classes, and then when iterating through the List, I draw a Custom Editor depending on each entry's type.
But no matter what I try, it only works as long as the current Editor Session is running. Upon restart, the content of the List/Array is empty. (or rather, it still contains the previous amount of entries, but they're all Null)
- I've tried setting every altered object Dirty (i.e. the List as soon as a new element has been added.)
- I've also tried setting the added elements Dirty, but I never get the prompt that there's unsaved changes in the Editor.
- I've tried altering the Array with SerializedProperty from the CumstomEditor, and I've used the
serializedObject.Update();
drawEditor();
serializedObject.ApplyModifiedProperties();
- framing in the CustomEditor's OnInspectorGUI()
Here's an overview of the structure (abbreviated):
The Main Class
[CreateAssetMenu(fileName = "Basic Action"), Serializable]
public class BasicAction : ScriptableObject
{
[HideInInspector]
public List<BaseEffect> _effectStack;
}
The Custom Editor
[CustomEditor(typeof(BasicAction))]
public class BasicActionEditor : Editor{
BasicAction _basicAction;
private void OnEnable() {
_basicAction = target as BasicAction;
}
public override void OnInspectorGUI()
{
serializedObject.Update();
base.OnInspectorGUI();
DrawEditor();
serializedObject.ApplyModifiedProperties();
if (GUI.changed)
{
EditorUtility.SetDirty(_basicAction);
//EditorSceneManager.MarkSceneDirty(_basicAction.gameObject.scene);
}
}
private void DrawEditor(){
//A Button that adds a new element to the list
if (GUILayout.Button("+", GUILayout.Width(40))){
InheritedEffect newItem = InheritedEffect.CreateInstance<InheritedEffect>();
_basicAction._effectStack.Add(newItem);
}
//This is for debugging: If there's entries in the List, log their type
if (this._basicAction._effectStack.Count > 0){
for (int i = 0; i < _basicAction._effectStack.Count; i++){
Debug.Log(this._basicAction._effectStack[i].GetType());
}
}
}
}
The Base ScriptableObject class (very abbreviated)
[Serializable]
public class BasicEffect : ScriptableObject
{
public string title = "Basic Effect";
}
And the inherited class
[Serializable]
public class InheritedEffect: BasicEffect
{
public int inheritedProperty;
}
What happens: When I add a new entry of the InheritedEffect, it's being added to the List, and then successfully read and stored and whatnot. But once I restart the Editor, every value, both belonging to BasicEffect and InheritedEffect is gone. In fact, the items stored in the List are null. The Scriptable Object JSON of the main class (BasicAction) in the file system also shows the array to be empty, although 2 elements had been added:
_effectStack: []
I hope that someone can point my nose on where it went wrong, because I've been cracking my brain over this for 2 days now. :) Thanks for any help in advance, and if any additional information is needed, please feel free to ask, this is my first post here!