- Home /
Serializing UnityEvents housed in generic class from Custom Inspector
basically I have this issue, the Example class has its event and I want to display all the events in the List in the ExampleMono inspector. The issue arises because Example can't be turned into a SerializedObject and thus I can't get the serialized property for Update1D. It also can't be a scriptable object because I want the list to be dynamic and I don't want to keep deleting and adding objects to the project.
public class Example //just holds Events and refrences
{
[Serializable]
public class UnityEventFloat : UnityEvent<float> { }
public UnityEventFloat Update1D = new UnityEventFloat();
}
public class ExampleMono : Monobehavior //designed to hold a list of the Example class
{
List<Example> examples;
}
[CustomEditor(typeof(ExampleMono))]
public class ExampleMonoInpector : Editor
{
override OnInspectorGUI()
{
SerializedObject mapObject = new SerializedObject(examples[0]);
SerializedProperty tempProp = mapObject.FindProperty("Update1D");
EditorGUILayout.PropertyField(tempProp);
}
}
Answer by Cassos · Apr 28, 2020 at 08:56 PM
Damn why can't people just formulate simple questions -.- I need more info, what is in the example class.
Could help you, just wrote a custom editor with serialized events too.
the Example class is really just a collection of UnityEvents, and the $$anonymous$$ono is just there to hold a list of them. there is a lot more to it but all of that works great, the issue is that you need to use the PropertyField to show UnityEvents and the Example class doesn't inherit from UnityEngine.Object so SerializedObject(examples[0]) can't work. trust me the rest of the script isn't relevant to the issue.
Yes I know, as I said I had my own trouble with that sh*t serializable in custom inspector system provided by Unity on my own. Did cost me 1 week of my lifetime.
So here is what you have to do:
SerializedProperty examples;
private void OnEnable()
{
examples = this.serializedObject.FindProperty("examples"); // Finds your list of example events
}
public override void OnInspectorGUI()
{
FallOnCollision t = (FallOnCollision)target; // Just example for a script, used an old one for this so don't worry, just important for the values
serializedObject.Update(); // You have to do that before you do the next step to get the current values
for (int i = 0; i < t.examples.Length; i++) // ALWAYS USE ARRAYS FOR SERIALIZED STUFF CAUSE UNITY WILL $$anonymous$$ETE LISTS - Don't know why, just do it that way!
{
SerializedProperty _e = examples.GetArrayElementAtIndex(i); // Gets the event out of the list of examples
EditorGUILayout.PropertyField(_e); // Serialize the Event
}
serializedObject.Apply$$anonymous$$odifiedProperties(); // Does what it says. Applies the modified event values! ^^
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612232733im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Values changed with Editor Script don't get saved 1 Answer
Undo.RecordObject is too slow on large arrays, alternatives? 1 Answer
How to change settings on editor using Enum with an array? 0 Answers
Detect Play mode about to be entered 3 Answers
Bug with displaying components in Inspector after hideFlags.NotEditable? 1 Answer