- Home /
How Can I Display UnityEvent in Custom Inspector When it is a Property of a Properties Property? (C#)
So I have a custom inspector for a Monobehavior, this Monobehavior has a property that is a ScriptableObject class, the ScriptableObject class has a property that is a List of a custom Type, this custom Type has a property of a list of another custom Type, that Type has a UnityEvent property.
I want the UnityEvent to show up in the CustomInspector, I can access the ScriptableObject classes property, but am not sure how to access the properties of the list it contains.
(To my knowledge UnityEvents can only be displayed by EditorGUILayout.PropertyField, with the UnityEvent being the serialized property.)
So what I have now is something like this:
OnInspectorGUI()
{
MyBaseClass base = (MyBaseClass)target;
SerializedObject serializedObject = new SerializedObject(base.myScriptableObjectProperty);
SerializedProperty myCustomListProperty1 = serializedObject.FindProperty("customPropertyName");
//Access myCustomListProperty1's List
//Get myCustomListProperty2
//Get the UnityEvent property from that list
EditorGUILayout.PropertyField(myUnityEventProperty);
}
I'm new to using properties like this, any help is appreciated :)
Answer by Adam-Mechtley · Mar 15, 2017 at 11:41 AM
If you just want to draw everything, you can of course do EditorGUILayout.PropertyField(myCustomListProperty, true);
If you need to find a specific child property, use SerializedProperty.FindRelative()
. If you need to get an element for a list property, use SerializedProperty.GetArrayElementAtIndex()
.
@Adam-$$anonymous$$echtley .GetArrayElementIndex() is what I was looking for!
Also I happened to find another post you answered on: https://answers.unity3d.com/questions/1270631/get-unityevent-reference-from-serializedproperty.html
I want to be able to get the UnityEvent back out of the property, or at least have any changes made by the user to the UnityEvent persist after the target object is deselected or a copy exists.
$$anonymous$$aybe this is a separate question but it may be useful to other users who are starting from where I did.
I'm trying to use your solution from the other post, but it doesn't seem to work in my case =/
SerializedProperty persistentCalls = myUnityEventProperty.FindPropertyRelative("m_PersistentCalls.m_Calls");
for (int i = 0; i < persistentCalls.arraySize; ++i)
{
persistentCalls.GetArrayElementAtIndex(i).FindPropertyRelative("m_CallState").intValue = (int)UnityEventCallState.RuntimeOnly;
}
Yeah if you could post another question with specific details of what you're trying to do I will take a look :)