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 orkungo · Oct 03, 2017 at 02:40 PM · guigui-button

How to change texture of individual GUI.Button ?

Hi,

I have a texture array and I iterate a for loop through set of elements to generate a button with the image from texture array. My goal is to change an individual button's texture that was generated with the loop.

Following is my scenario-drawing. I only want the single Button1's texture to be changed however I do something wrong.. alt text alt text

Here is my code that generates Button1s and I call it at the OnGUI(), whenever I click on Button1 all Button1s change the texture :

 public int k = 0;
 
 rectBut.center = new Vector2(scrWidth / 18f+i*scrWidth/3.5f, backButPos.y/ 1.5f);
 for(int i = 0; i<size; i++)
 {
 
     if(GUI.Button(rectBut, textures[k], guiSkin.customStyles[0]))
         {
             if (k >= textures.Count ()-1)
                 k = 0;
             else
                 k += 1;
             
         }
 }

untitled-2.png (20.6 kB)
untitled-2.png (20.6 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

2 Replies

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

Answer by CybernetHacker14 · Oct 05, 2017 at 07:13 AM

It may be a longshot but I will try to explain what can be done. This functionality is useful if you use Unity UI components instead of Scripting GUI.

  1. Create a folder called 'Resources' in your Assets, then put all your textures in the Resources>(Create New)Textures folder. All those textures should be in the form of Sprites, as buttons need sprites, not raw texture files to work.

  2. As I work in C#, I know this functionality only but you can find on net how to port various function to UnityScript. Create a new script, then create an array of sprites named thumbnails, and then fill it up with those textures by using thumbnails = Resources.LoadAll<Sprites> ("Textures"). The "Textures" field is basically the path where you have stored the files inside the 'Resources' folder.

  3. Create a UI panel using Unity UI components (I have notices that you are using Scripting GUI, but trust me, this will sort many of your UI problems), attach a Layout Group Component to it (horizontal or vertical or both) and a Content Size Fitter component to it. Set it's values as per your choice (it's all iterative development).

  4. Create a Button prefab, or in your case, one of those drawings, with the big Rectangle and buttons below it. Set it's properties as per your choice.

  5. In your script, get the button prefab in a public gameObject variable, get the UI panel as an another gameObject component, and inside a for loop, Instantiate the button prefab and set it as the child of the Unity UI panel. The UI panel acts a container for these buttons and with the Layout Group component and Content Size Fitter component, all these buttons are sorted and arranged in a geometrical way.

  6. In that same for loop, after a button in instantiated and set as child of the UI panel, access the Button1 of the button prefab, then access it's Image component by (buttonPrefab).GetComponent.sprite and assign it the value of the thumbnails array. (pathToButton1OfButtonPrefab).GetComponent<Image>.sprite = thumbnails[i], this will then assign the particular sprite at the array index of thumbnails to the image component of button1.

Which texture to apply in which sequence can be controlled by numbering the texture files in the 'Resources' folder. For further references and understanding, check out this video Dynamic Level Selection Tutorial

Comment
Add comment · Show 3 · 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 orkungo · Oct 05, 2017 at 07:21 AM 0
Share

Thank you very much. You are right, I was using Unity GUI Scripting. Your explanation seems more robust and intuitive since Unity is also moving towards building GUI with components.

avatar image orkungo · Oct 05, 2017 at 07:26 AM 0
Share

Just a quick question for the Textures. Do you see any potential problem if I load the Textures from Asset Bundles? Because I am building the GUI dynamically and data driven that is being fetched from FTP as Asset Bundles. Bundles hold textures as well so maybe I would implement a pooling mechanism.

avatar image CybernetHacker14 orkungo · Oct 05, 2017 at 08:37 AM 0
Share

Asset Bundles actually decompress and unpack themselves in your Asset Folder, but to include outside resources, like sprites, gameObjects and other things via a script, there is no option but to use the 'Resources' folder, because inside Unity libraries, the Resources.(somecodehere) means that it will check for the specified thing inside the 'Resources' folder. If you want to use textures from Asset Bundles, try importing them, then using a file explorer, locate those assets and move it into the 'Resources' folder. BTW you are welcome for your earlier reply.

avatar image
0

Answer by orkungo · Oct 04, 2017 at 06:18 AM

Any ideas?

Comment
Add comment · 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

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

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

Is it possible to detect which GUIStyleState is used? 2 Answers

Simple Drop down list 1 Answer

How do I make a button "enabled" using GUI.Button()? 1 Answer

What do I add to this script, so if you clicked the button ex. 30 times, a gui.box will appear? 1 Answer

Making a GUI button appear after pressing another GUI button? C# 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