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:
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:
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