SerializedObject obj = new SerializedObject(myComponent); SerializedProperty myArray = obj.FindProperty("myArray"); // Insert a new element at index 2 myArray.InsertArrayElementAtIndex(2); obj.ApplyModifiedProperties();
using UnityEditorInternal; SerializedObject obj = new SerializedObject(myObject); SerializedProperty myArray = obj.FindProperty("myArray"); // Get the ReorderableList for the array property ReorderableList list = new ReorderableList(obj, myArray); list.Insert(list.index, new MyClass()); obj.ApplyModifiedProperties();This code uses the `UnityEditorInternal` namespace to create a `ReorderableList` for the `myArray` serialized property. Then, the `Insert` method is called on the list object to insert a new element at the current index position with a new instance of the `MyClass` type. Finally, the property changes are applied using `ApplyModifiedProperties()`. The package library for these examples is the `UnityEditor` package, which is part of the Unity Editor.