- Home /
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..
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;
}
}
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.
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.
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.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).
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.
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.
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
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.
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.
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.
Your answer
Follow this Question
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