Hello!
So, I’ve written a custom inspector for a custom object called “ProcedureHelper”. ProcedureHelper is a custom class (implements MonoBehavior) that contains a list of "AbstractProcedure"s, which is an interface I’ve implemented in several classes. I can add several AbstractProcedures from the custom inspector, and the inspector will list them all one by one.
However, as soon as a recompile, the inspector data disappears. A Debug.Log reveals that the list contained goes from full to null.
Is there any known cause to why this happens? What can I do to keep this data? Thank you!
Here is the Custom Editor
using UnityEngine;
using UnityEditor;
using System;
using System.Collections;
using System.Collections.Generic;
[CustomEditor(typeof(ProcedureHelper))]
public class ProcedureHelperEditor : Editor
{
ProcedureType typeToAdd = ProcedureType.Default;
public override void OnInspectorGUI()
{
// Variables
ProcedureHelper controller = target as ProcedureHelper;
// Set our controls like normal
EditorGUIUtility.LookLikeControls();
DrawDefaultInspector ();
// Add Button
EditorGUILayout.BeginHorizontal();
typeToAdd = (ProcedureType)EditorGUILayout.EnumPopup("Loop Type:", typeToAdd);
if (GUILayout.Button("Add Procedure"))
{
controller.AddProcedure(typeToAdd);
}
EditorGUILayout.EndHorizontal();
// Begin Showing Procedures
if (controller.procedures != null)
for (int i = 0; i < controller.procedures.Count; i++)
{
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField(controller.procedures*.type.ToString());*
-
EditorGUILayout.EndHorizontal();*
-
}*
-
// If we've changed the gui, set it to dirty*
-
if (GUI.changed == true)*
-
{*
-
EditorUtility.SetDirty (target);*
-
}*
-
}*
}
Here is the class you’re editing
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[System.Serializable]
public class ProcedureHelper : MonoBehaviour
{
-
[HideInInspector]*
-
private List m_procedures;*
-
public List procedures {*
-
get { return this.m_procedures; } *
-
}*
-
public void AddProcedure(ProcedureType type)*
-
{*
-
if (m_procedures == null)*
-
m_procedures = new List<AbstractProcedure>();*
-
switch (type)*
-
{*
-
case ProcedureType.Default:*
-
break;*
-
case ProcedureType.GoToLevel:*
-
m_procedures.Add(new GoToLevelProcedure());*
-
break;*
-
}*
-
}*
}
Here is the interface
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public interface AbstractProcedure
{
- ProcedureType type*
- {*
-
get;*
- }*
- void Run(GameObject obj);*
}
public enum ProcedureType { Default, GoToLevel, ShowObjects, HideObjects };
Here is an Implementation of the Interface
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[System.Serializable]
public class GoToLevelProcedure : System.Object, AbstractProcedure
{
- public ProcedureType type { get { return ProcedureType.GoToLevel; } }*
- public Levels_t LevelToLoad = Levels_t.NONE;*
- public string GoToChapter = “”;*
}