Is it Possible to Assign Scene GameObjects to UnityEvents in a Custom Editor?

Hello ,

I’m currently working on a custom editor in Unity where I need to assign game object from the scene to unity event I’ve created a custom editor window and I’m trying to enable drag-and-drop functionality for game objects into unity event, similar to how it’s done in the standard Inspector.

Here’s a simplified version of what I’m working with

[System.Serializable]
public class PassEvent
{
    public UnityEvent ExecuteOnPass = new UnityEvent();
    public UnityEvent UIEvents = new UnityEvent();
    public List<MICLICDataSetter> ExecuteDataSetter = new List<MICLICDataSetter>();
    public GameObject controlledObject; // Field for GameObject reference
}

[System.Serializable]
public class Window
{
    public int id;
    public WindowType type;
    public Rect rect;
    public PassEvent passEvent = new PassEvent(); // Initialized here
}
void f_PassEvent(SerializedObject serializedObject, SerializedProperty windowsProperty, int windowId)
{
    SerializedProperty windowProperty = GetWindowProperty(windowsProperty, windowId);
    if (windowProperty == null)
    {
        EditorGUILayout.LabelField("PassEvent window not found");
        return;
    }

    SerializedProperty passEventProperty = windowProperty.FindPropertyRelative("passEvent");

    // Display ExecuteOnPass UnityEvent field
    EditorGUILayout.PropertyField(passEventProperty.FindPropertyRelative("ExecuteOnPass"));
    EditorGUILayout.PropertyField(passEventProperty.FindPropertyRelative("UIEvents"));

    // Add ObjectField for GameObject references
    SerializedProperty controlledObjectProperty = passEventProperty.FindPropertyRelative("controlledObject");
    controlledObjectProperty.objectReferenceValue = EditorGUILayout.ObjectField(new GUIContent("Controlled Object"),
                                                                               controlledObjectProperty.objectReferenceValue,
                                                                               typeof(GameObject),
                                                                               true);

    serializedObject.ApplyModifiedProperties();
}

So far, I gather you want to display UnityEvent objects in a custom editor window or inspector, and when you say “assign game object from the scene to unit event” I assume you mean said game object has a script attached that has a function which you want to add as listener to the event. Correct?

If so, I think your editor code may be doing more than necessarily here. When you create the property field for your UnityEvent, it will render into the window the same way it does in any inspector, with a list of available observer actions. It sounds like you’ll just want to press the + button and add your game object there rather than manually creating an extra field in which to drop the game object.

In theory, you could create that extra field, and then you could map the desired method of the attached script to the UnityEvent object using the event’s AddListener() method, but none of that is necessary since that’s what the actions list is for.

Also, UnityEvent uses reflection to expose the observer method’s name for you so that you don’t have to do so manually. It’s the main reason to use UnityEvent rather than any other type of event. If you need that, no need to code it from scratch. If you don’t, you might consider using c# action events instead, since they’re lighter-weight than UnityEvent.