- Home /
Question by
ManaAura · Jul 24, 2017 at 04:04 PM ·
listserializationdatabasemonobehaviourcustom inspector
List of custom classes in MonoBehaviour's Data gets lost after Re-Opening Unity!!!
I have a trouble with List of custom classes in MonoBehaviour missing data after I reopen unity... I have tried set it to dirty, but it didn't work....
I have a list of custom class, in the monobehaviour. Then i have an editor code of it that Add and remove that list with button clicks. The list works perfectly fine in run-time and editor time. HOWEVER, once i save and close the project, then re-open it.. the LIST BECOMES EMPTY
/////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using UnityEngine;
public class MYTEST : MonoBehaviour
{
[SerializeField]
public List<test> tList;
[Serializable]
public class test
{
public int t;
}
}
///////////////////////////////////////////////////////////////////
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(MYTEST)), CanEditMultipleObjects]
public class MYTESTEDITOR : Editor
{
private MYTEST myTest;
private SerializedObject sb;
public void OnEnable()
{
myTest = (MYTEST)target;
sb = new SerializedObject(target);
}
public override void OnInspectorGUI()
{
sb.Update();
DrawDefaultInspector();
EditorUtility.SetDirty(myTest);
if (GUILayout.Button("ADD"))
{
if (myTest.tList == null)
{
myTest.tList = new List<MYTEST.test>();
}
myTest.tList.Add(new MYTEST.test());
}
if (GUILayout.Button("RMV"))
{
myTest.tList.RemoveAt(myTest.tList.Count - 1);
}
sb.ApplyModifiedProperties();
}
}
Comment