You need to create a serialized object to update and apply, also how are you setting the values? I would also suggest using begin and end change check rather than applymodifiedproperies every call.
For example;
public class MyClass : MonoBehaviour
{
public int MyInt;
}
using UnityEditor;
[CustomEditor(typeof(MyClass))]
public class MyClassEditor : Editor
{
private SerializedObject _myClass;
private void OnEnable()
{
_myClass = new SerializedObject(target);
}
public override void OnInspectorGUI()
{
_myClass.Update();
EditorGUI.BeginChangeCheck();
var myInt = _myClass.FindProperty("MyInt");
EditorGUILayout.PropertyField(myInt);
if (EditorGUI.EndChangeCheck())
{
//This is where undo is created and values are saved
_myClass.ApplyModifiedProperties();
}
}
}
I was doing like this but then I cought many errors with arrays
Properties-arrays are null
[Serializable]
public class ChildClass1 : ParentClass
{
public string str;
public override void SomeMethod()
{
Debug.Log("some interactions");
}
}
[Serializable]
public class ChildClass2 : ParentClass
{
public int integer;
public override void SomeMethod()
{
Debug.Log("some another interactions");
}
}
[Serializable]
public abstract class ParentClass
{
protected GameObject gameObject;
public abstract void SomeMethod();
}
public class MyClass : MonoBehaviour
{
public ParentClass[] array = new ParentClass[10];
}
[CustomEditor(typeof(MyClass))]
public class MyClassEditor : Editor
{
private SerializedObject _object;
private void OnEnable()
{
_object = new SerializedObject(target);
}
public override void OnInspectorGUI()
{
serializedObject.Update();
var a = _object.FindProperty("array");
Debug.Log(a.isArray); // NullReferenceException: Object reference not set to an instance of an object
EditorUtility.SetDirty(target);
serializedObject.ApplyModifiedProperties();
}
Thanks, it have worked for me.
But how i can insert to array something different from ParentClass? _object.FindProperty("array").InsertArrayElementAtIndex(0)
InsertArrayElementAtIndex() will add a instance of ParentClass, not a instance of ChildClass1 or ChildClass2