Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by damtre · Mar 11, 2018 at 08:20 PM · texture2donguiobjectfield

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

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

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

3.png (5.4 kB)
2.png (3.3 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image damtre · Mar 11, 2018 at 11:41 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

79 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges