Get object from EditorGUILayout.PropertyField

Hi,

I’m making a plugin-manager extension to the Unity Editor. I need an ObjectField to drag-and drop folders in, and so far this has worked

public override void OnInspectorGUI()
    {
        serializedObject.Update();
        //...
        if (Event.current.commandName == "ObjectSelectorUpdated")
        {
            currentObject = EditorGUIUtility.GetObjectPickerObject();
        }

        currentObject = EditorGUILayout.ObjectField("Object", currentObject, typeof(Object), true) as DefaultAsset;
        //...
        serializedObject.ApplyModifiedProperties();
    }

Yet, I need this to be a re-sizable list of ObjectFields, and not just one.

Using

EditorGUILayout.PropertyField(folderProp, new GUIContent("Plugin 1"), true);
        //"folderprop" is a serialized property of "Object[]".

gives me my re-sizable list in the inspector, but I don’t know how to retrieve what Object(s) were put into the editor because the method returns a bool and doesn’t trigger an Event with a command name! Aside from that, the documentation doesn’t give an example. Does anyone know how to use this?

Thanks in advance;).

I solved my problem. The object was actually going to the variable in the script my editor script was a type of (the variable the editor script was getting its serialized property from), not the editor script itself. I was able to get an instance of that script using GameObject.FindObjectOfType<MyScript>(); //or FindObjectsOfType. Mine only appears once. inside of my editor script and call a function inside of it that uses that value.

if folderProp is an array or List then you can access elements using:

SerializedProperty myElement = folderProp.GetArrayElementAtIndex(index)
MyScript obj = myElement.objectReference as MyScript

Could I use SerializedProperty.arraySize in a foreach loop with that?

I also assume you mean this. If so, that is super useful! I found a working solution though.

Thanks;).

Yes and yes

1 Like