UnityEvents as Field in UIElements

Hello,
is there a possiblity to get UnityEvents as a Field in UIElements like in the Inspector (see screenshot )?
Or exist a custom property drawer that can expose UnityEvents in UIElements?

7072567--840952--UnityEvent Property Drawer.png

Thanks in advance
Astha

1 Like

For now you have to use IMGUIContainers:

VisualElement IMGUIField(string name)
{
    return new IMGUIContainer(() =>
    {
        serializedObject.Update();
        EditorGUI.BeginChangeCheck();
        EditorGUILayout.PropertyField(serializedObject.FindProperty(name));
        if (EditorGUI.EndChangeCheck())
        {
            serializedObject.ApplyModifiedProperties();
        }
    });
}
3 Likes

@Nexer8 Hey thanks for your answer, but I don’t get it. Needless to say I’ve rarely worked with editor scripting, normally we use Odin Inspector.
What I want to achieve is:
I’ve created a graphview node based dialogue system, which stores the node actions and data as a list in a scriptable object.
Of course the data isn’t an UnityObject, so I can’t use this method since it needs to be an UnityObject. Right now I outsourced the UnityEvent and its parameters in a scriptable object and assign it through an ObjectField…
The probleme here is:
I want an event to remove 5 stones (items) from player, then I create a scriptable object to remove 5 stones from player.
Then I want an event to remove 3 pieces of wood from player, and again I have to create another scriptable object…
So for every “event node” I have to create a scriptable object and that will become out of hand. My thought was to expose the UnityEvent and its parameters directly to the node, but in this case it doesn’t seem to work.
I hope it’s clearer to understand my problem.

And thanks in advance, i really appreciate your help =)

7074553--841369--Node Based Editor 002.png

You need to create a SerializedObject from the ScriptableObject, then do some trickery to find out the array index of the node, create a SerializedProperty from that, then use the IMGUIField method, but instead of a name you pass in the SerializedProperty, which then gets drawn. But in general, the problem you have here is very complex, so see if there is another way to do it.

One workaround could be to dynamically create the ScriptableObejcts using ScriptableObject.CreateInstance(), then use AddObjectToAsset() to add it as part of the graph asset.

1 Like

@Nexer8 again thanks for your help!
Yeah this workaround to dynamically create ScriptableObjects was my thought as well, but would be the last resort, if there isn’t a better solution.
I’ll try your first approach, maybe I get it to work…

Thanks again
Astha

1 Like

User PropertyField instead. This example showing a UnityEvent pooled in a List

               var a = new SerializedObject(signal);
                var start = a.FindProperty("signalList").GetArrayElementAtIndex(someIndex).FindPropertyRelative("action");
              
                var objStart = new PropertyField(start).Width(50, true);
                objStart.BindProperty(start);