Hi there,
I am having problems with data persistence after pressing play in the editor for my editor window.
To demonstrate the problem in a clear way - copy and paste and dump this script into an editor folder:
using UnityEngine;
using UnityEditor;
using System;
public class SerTest : EditorWindow
{
private BaseView _baseView;
[MenuItem("SerializationTest/SerTest")]
static void DoIt()
{
GetWindow<SerTest>();
}
void OnEnable()
{
if (_baseView == null)
{
_baseView = new BaseView();
Debug.Log("New BaseView created");
}
}
void OnGUI()
{
_baseView.OnGUI();
}
}
[Serializable]
class BaseView
{
private ViewUnit[] _array;
private CompoundClass<ViewUnit> _compoundClass;
public BaseView() //Constructor
{
_array = new ViewUnit[3];
for (int i = 0; i < _array.Length; i++)
{
_array[i] = new ViewUnit();
}
_compoundClass = new CompoundClass<ViewUnit>(3);
}
public void OnGUI()
{
GUILayout.Space(15);
GUILayout.Label("Simple Array");
for (int i = 0; i < _array.Length; i++)
{
_array[i].OnGUI();
}
GUILayout.Space(15);
GUILayout.Label("Compound Class");
for (int i = 0; i < _compoundClass.AnArray.Length; i++)
{
_compoundClass.AnArray[i].OnGUI();
}
}
}
[Serializable]
class ViewUnit
{
private int _someInt;
public void OnGUI()
{
_someInt = EditorGUILayout.IntField("Some Int", _someInt);
}
}
[Serializable]
class CompoundClass<T> where T : new()
{
private int _someInt;
public T[] AnArray;
public CompoundClass(int capacity)
{
AnArray = new T[capacity];
for (int i = 0; i < AnArray.Length; i++)
{
AnArray[i] = new T();
}
}
}
So there is a ViewUnit which I am displaying in 2 fashions:
Once as a simple array of ViewUnits, and in the 2nd instance as an array which is part of a class.
If you try this sample out and press play you will see that in the simple array - data is preserved after play is pressed.
But all data is lost in the 2nd case.
How can I solve this? Help will be very much appreciated.