- Home /
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:
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
Answer by Bunny83 · Mar 11, 2018 at 09:37 PM
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[i] = (Texture2D)EditorGUILayout.ObjectField(textureList[i], 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! ;)
Your answer
![](https://koobas.hobune.stream/wayback/20220612154358im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
My OnGUI() Won't show the Button elements :( 0 Answers
Make a PropertyField() for a Texture2D SerializedProperty looking like an ObjectField() 2 Answers
Better performance? GUITextures or Texture2D drawn in function OnGUI? 0 Answers
move texture 2d with Time.deltatime 3 Answers
"null texture passed" for any texture2D other than the 2 originals. 1 Answer