5

I have GameObject array field in my CustomEditor class derived from the UnityEngine.Editor. I need to be able to display (draw) and give user ability to modify that array.

Just like how Unity's Inspector doing this for Serializable fields of objects derived from the ScriptableObject. E.g. displaying materials array in the inspector: .

5 Answers 5

11

Refer to yours editor object as to the SerializedObject and then find any required property, draw it, and apply modification:

public class MyEditorWindow : EditorWindow
{
    [MenuItem("Window/My Editor Window")]
    public static void ShowWindow()
    {
        GetWindow<MyEditorWindow>();
    }

    public string[] Strings = { "Larry", "Curly", "Moe" };

    void OnGUI()
    {
        // "target" can be any class derived from ScriptableObject 
        // (could be EditorWindow, MonoBehaviour, etc)
        ScriptableObject target = this;
        SerializedObject so = new SerializedObject(target);
        SerializedProperty stringsProperty = so.FindProperty("Strings");

        EditorGUILayout.PropertyField(stringsProperty, true); // True means show children
        so.ApplyModifiedProperties(); // Remember to apply modified properties
    }
}

Original answer here.

Sign up to request clarification or add additional context in comments.

3 Comments

When I change the values in the editor window the Values do not change... I think it's a problem with the so.ApplyModifiedProperties(); line but I need help solving this
Ok, I got the solution to this, I will edit it in the answer but basically you need to declare the variables on OnEnable() and use the so.Update() function
Couldn't edit it so posted another answer
1

I managed to do it with a for loop and an extra int:

using UnityEditor;
using UnityEngine;

public class RockSpawner : EditorWindow
{
    int rockCollectionSize = 0;
    GameObject[] rockCollection;

    [MenuItem("Tools/Rock Spawner")]
    public static void ShowWindow()
    {
        GetWindow(typeof(RockSpawner));
    }

    void OnGUI()
    {
        rockCollectionSize = EditorGUILayout.IntField("Rock collection size", rockCollectionSize);
        if (rockCollection != null && rockCollectionSize != rockCollection.Length)
            rockCollection = new GameObject[rockCollectionSize];
        for (int i = 0; i < rockCollectionSize; i++)
        {
            rockCollection[i] = EditorGUILayout.ObjectField("Rock " + i.ToString(), rockCollection[i], typeof(GameObject), false) as GameObject;
        }
    }
}

Comments

1

The top answer didn't work well for me because the properties did not update, there for I moved the declaration lines to the OnEnable function you do not want to declare them over and over again) and used to so.Update() to update the changed variables.

using UnityEngine;
using UnityEditor;

public class MyEditorWindow : EditorWindow
{
    [MenuItem("Window/My Editor Window")]
    public static void ShowWindow()
    {
        GetWindow<MyEditorWindow>();
    }

    public string[] Strings = { "Larry", "Curly", "Moe" };
    SerializedObject so;

    private void OnEnable()
    {
        ScriptableObject target = this;
        so = new SerializedObject(target);
    }

    void OnGUI()
    {
        // "target" can be any class derived from ScriptableObject 
        // (could be EditorWindow, MonoBehaviour, etc)
        so.Update();
        SerializedProperty stringsProperty = so.FindProperty("Strings");

        EditorGUILayout.PropertyField(stringsProperty, true); // True means show children
        so.ApplyModifiedProperties(); // Remember to apply modified properties
    }
}

Links:

Base for my answer

SerializedObject.Update

Comments

-2

public GameObject[] yourGameObjects;

Then in the inspector set your size, and fields should open up.

5 Comments

i mean EditorWindow class. Which allows you to create a custom window in the IDE.
@ИльяМашин I don't understand what you mean. This and the answer supplied by Fribu will both show variables like the ones in the image you have supplied.
link This class for creating custom window in editor mode. I try add field for array of gameobject.
@ИльяМашин That link you have supplied seems to be complete over kill for something as simple as a public array of GameObject's
That link i added for your understanding what i want to create. I don't need public array in inspector. I need to add array in EditorWindow object. As EditorGUILayout.ObjectField but Array field
-2

add a public array to your script

public GameObject[] myObjects

1 Comment

This workin in playmode. I mean Editor scripting and EditorWindow.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.