Question: With the following CustomInspector setup, the user can edit a UnityEvent. However once the inspected object is deselected in the hierarchy then reselected, the changes made by the user have been saved the ScriptableObject but are reset in the inspector. I need the changes to persist within the inspector.
I am going to use modified “Intractable” code from the comments because I think it best represents my setup:
Intractable.cs:
[Serializable]
public class Interactable : MonoBehaviour
{
[SerializeField]
public InteractableScriptableObject mySO;
}
InteractableScriptableObject:
[Serializable]
public class InteractableScriptableObject : ScriptableObject
{
public List<InteractableCustomType1> customType1List;
}
InteractableCustomType1:
[Serializable]
public class InteractableCustomType1 : IComparable<InteractableCustomType1>
{
public int myInt;
public List<InteractableCustomType2> myCustomType2List = new List<InteractableCustomType2>();
public InteractableCustomType1(int customInt, List<InteractableCustomType2> customType2List)
{
myCustomType2List = customType2List;
myInt = customInt;
}
//Required by IComparable.
public int CompareTo(InteractableCustomType1 other)
{
if (other == null)
{
return 1;
}
return 0;
}
}
InteractableCustomType2:
[Serializable]
public class InteractableCustomType2 : IComparable<InteractableCustomType2>
{
public string myString;
public UnityEvent myUnityEvent;
public InteractableCustomType2(string customString, UnityEvent customEvent)
{
myString = customString;
myUnityEvent = customEvent;
}
//Required by IComparable.
public int CompareTo(InteractableCustomType2 other)
{
if (other == null)
{
return 1;
}
return 0;
}
}
InteractableInspector:
[CustomEditor(typeof(Interactable))]
public class InteractableInspector : Editor
{
public override void OnInspectorGUI()
{
Interactable targetInteractable = (Interactable)target;
if(targetInteractable != null)
{
InteractableScriptableObject SO = targetInteractable.mySO;
if (GUILayout.Button("Create ScriptableObject"))
{
targetInteractable.mySO = CreateInstance<InteractableScriptableObject>();
}
if (SO.customType1List != null)
{
if (GUILayout.Button("Add a customType1"))
{
SO.customType1List.Add(new InteractableCustomType1(0, new List<InteractableCustomType2>()));
}
if (SO.customType1List.Count > 0)
{
for (int x = SO.customType1List.Count - 1; x >= 0; x--)
{
InteractableCustomType1 customType1 = SO.customType1List[x];//So it is easier to type out.
if (GUILayout.Button("Add a customType2"))
{
customType1.myCustomType2List.Add(new InteractableCustomType2("string", null));
}
if (customType1.myCustomType2List.Count > 0)
{
for (int y = customType1.myCustomType2List.Count - 1; y >= 0; y--)
{
SerializedObject serializedObject = new SerializedObject(targetInteractable.mySO);
SerializedProperty myCustomType1 = serializedObject.FindProperty("customType1List");
SerializedProperty customType1Element = myCustomType1.GetArrayElementAtIndex(x);
SerializedProperty targetCustomType2 = customType1Element.FindPropertyRelative("myCustomType2List").GetArrayElementAtIndex(y);
SerializedProperty myUnityEvent = targetCustomType2.FindPropertyRelative("myUnityEvent");
EditorGUILayout.PropertyField(myUnityEvent);
if (GUI.changed == true)
{
serializedObject.ApplyModifiedProperties();
}
}
}
}
}
}
else
{
SO.customType1List = new List<InteractableCustomType1>();
}
}
}
}
The code I’m working with is so complex that I think my previous examples were confusing, from here with the new example; what would I change to make the changes made to the UnityEvent persist so the properties that were applied to the ScriptableObject are displayed again on the UnityEvent when reselected in the hierarchy.