Create Texture2D elements in OnGUI Method

Hello!

I´m really confused as I don´t understand the following behavior.
I created a new menuitem for unity editor and call the new window. That window should manage Textures2D elements added by a button.

My First step was simply creating a single Texture2D Object:

    public Texture2D texture2D;
    List<Texture2D> textureList = new List<Texture2D>();

    void OnGUI()
    {
        GUILayout.Label("Textures", EditorStyles.boldLabel);
        texture2D = (Texture2D)EditorGUILayout.ObjectField(texture2D, typeof(Texture2D), true);
    }

The result is as follows. Seems that it´s working fine:
The field is visible and I also can assign a texture to that field:
112800-2.png

So i decided to enhance my project to be more flexible. I added a button that should insert dynamically more Texture2D elements. Here is my enhanced code:

public Texture2D texture2D;
    List<Texture2D> textureList = new List<Texture2D>();

    void OnGUI()
    {
        GUILayout.Label("Textures", EditorStyles.boldLabel);

        if (GUILayout.Button("Add texture", GUILayout.ExpandWidth(false)))
        {
            AddTexture();
        }

        foreach (Texture2D tex in textureList)
        {
            texture2D = (Texture2D)EditorGUILayout.ObjectField(tex, typeof(Texture2D), true);
        }
    }

    void AddTexture()
    {
        textureList.Add((Texture2D)EditorGUILayout.ObjectField("Texture", texture2D, typeof(Texture2D), true));
    }

I can now add more Texture2D elements by clicking a button and adding a Texture2D element to a generic list.
In OnGUI I loop over the list to show the fields on my window.
But I cannot assign a texture anymore:
112801-3.png

What is the problem here?
I´m working on that issue about 2 days now and really don´t know how to fix this.

Appreciate your help.

Thank you

You have several issues here. First of all your adding code makes not much sense:

void AddTexture()
{
    textureList.Add((Texture2D)EditorGUILayout.ObjectField("Texture", texture2D, typeof(Texture2D), true));
}

Using the ObjectField here is pointless. You just want to add a new element to the array which initially should just be null

void AddTexture()
{
    textureList.Add(null);
}

The next thing is you iterate over your list and you pass the foreach loop variable “tex” into the ObjectField. However the return value of ObjectField is the changed value which you store inside the texture2d variable. You need to read and write to the same element in the list. You can’t use a foreach loop for this since a foreach variable is read-only. Use an ordinary for loop like this:

for(int i = 0; i < textureList.Count; i++)
{
    textureList _= (Texture2D)EditorGUILayout.ObjectField(textureList*, typeof(Texture2D), true);*_

}
As you can see we read the current element from the list, pass it into the ObjectField as current value and assign the return value back into the list

Hey Bunny83,

you made my weekend! That works.
Normally I first add a Value/Object (not null, but instantiated) into a list. Then I iterate over it using for-loop etc.
In case of that scenario you gave me a new point of view.
Well, that are my first steps in creating EditorWindow/CustomEditors.

Anyway, Thanks again! :wink: