I need to map a 1 to N relationship and was trying to make a good interface for it in the inspector, but I havn’t been able to get the serialization to work correctly. It seems that Unity doesn’t want to serialize a List<List> type nor a List<Object> type. Am I thinking correctly that Unity can’t do that or is there some method I am missing?
Here’s what I have:
The class itself:
[System.Serializable]
public class GivePrize : MonoBehaviour {
[HideInInspector]
public List<GameObject> pre;
[HideInInspector]
public List<GameObject[]> result;
[HideInInspector]
public List<int> counts;
[HideInInspector]
public int prereqLength;
The editor for the class:
[CustomEditor(typeof(GivePrize))]
public class GivePrizeEditor : Editor {
public GivePrize myObj;
public void Awake() {
myObj = (GivePrize) target;
}
public override void OnInspectorGUI() {
base.OnInspectorGUI();
if (myObj.pre == null) {
myObj.pre = new List<GameObject>();
}
if (myObj.result == null) {
myObj.result = new List<GameObject[]>();
}
if (myObj.counts == null) {
myObj.counts = new List<int>();
}
myObj.prereqLength = EditorGUILayout.IntField(myObj.prereqLength);
if (GUI.changed) {
while (myObj.pre.Count < myObj.prereqLength)
myObj.pre.Add(null);
while (myObj.result.Count < myObj.prereqLength)
myObj.result.Add(new GameObject[1]);
while (myObj.counts.Count < myObj.prereqLength)
myObj.counts.Add(1);
while (myObj.pre.Count > myObj.prereqLength)
myObj.pre.RemoveAt(myObj.pre.Count-1);
while (myObj.result.Count > myObj.prereqLength)
myObj.result.RemoveAt(myObj.result.Count-1);
while (myObj.counts.Count > myObj.prereqLength)
myObj.counts.RemoveAt(myObj.counts.Count-1);
}
for (int i = 0; i < myObj.pre.Count; i++) {
EditorGUILayout.LabelField("Prereq:");
myObj.pre _= (GameObject)EditorGUILayout.ObjectField((GameObject)myObj.pre*, typeof(UnityEngine.GameObject), true);*_
* EditorGUILayout.LabelField(“Results:”);*
myObj.counts = EditorGUILayout.IntField(myObj.counts*);*
_ GameObject temp = myObj.result*;*_
* if (GUI.changed) {*
_ if (temp.Length < (int)myObj.counts*) {
GameObject[] newArray = new GameObject[(int)myObj.counts];*_
* for (int j = 0; j < temp.Length; j++)*
* newArray[j] = temp[j];*
_ for (int j = temp.Length; j < (int)myObj.counts*; j++)
newArray[j] = null;*_
* temp = newArray;*
_ } else if (temp.Length > (int)myObj.counts*) {
GameObject[] newArray = new GameObject[(int)myObj.counts];*_
_ for (int j = 0; j < (int)myObj.counts*; j++)
newArray[j] = temp[j];*_
* temp = newArray;*
* }*
* }*
* for (int j = 0; j < temp.Length; j++) {*
* temp[j] = (GameObject)EditorGUILayout.ObjectField((GameObject)temp[j], typeof(UnityEngine.GameObject), true);*
* }*
_ myObj.result = temp;
* }*_
* if (GUI.changed) {*
* EditorUtility.SetDirty(myObj);*
* }*
* }*
Any help would be appreciated, thanks.