- Home /
Unity 2018 Editor serializedproperty insert new index using a class reference with InsertArrayElementAtIndex
Over the last couple of days I have been converting my class editor scripts to used the serialized property as using the previous "target" class method would not work when using the "open prefab" functionality.
The challenge I have hit is trying to insert a new array index within the editor. I have a button that runs a script to insert a new element. I am using the InsertArrayElementAtIndex method on the serialized property for my array, but when I try to reference the array element everything is null. On the unity 2018 documentation I found the comment that you need to initialize the array element. Which it the array was a list of strings or integers it would be clear.
However, I cannot find how I initialize my new array element to be a class reference.... I have been banging my head on this for a couple of days... any help would be wonderful, thanks in advance..
// Base Class
using UnityEngine;
.....
public class GameClass : MonoBehaviour
{
[System.Serializable]
public class ClassA
{
[SerializeField]
Public int ValueOfInt;
[SerializeField]
Public string ValueOfString;
}
}
// Editor Class
using UnityEditor;
....
[CustomEditor(typeof(GameClass))]
class GameClassEditor : Editor
{
Protected Void AddNewIndex(SerializedProperty Array)
{
Array.InsertArrayElementAtIndex(Array.arraySize)
// how do you initialize this new element to an instance of GameClass.ClassA??????
}
}