- Home /
Show images on buttons from an example
Following a Robot Repair example from "Unity 3.x Game Development by Example" book. I have reached a point where I should see whole randomized and pictured layout. (Page 169)
Worked hard and finally solved my issue myself :). For anyone else who might follow the book and run into similar issue this is what I suggest:
1)
Assets/Scripts/sc_GameScript.js(x,x): BCE0048: Type 'Object' does not support slicing.
Under #pragma strict card:Object casting gives you error. So make card:Card to get values from arrays correctly. I dunno why book suggest to do that. Maybe they don't use #pragma strict.
(Perhaps somebody else with more experience can give the reason?)
2)
I completely rewrote the whole card dealing and bulding blocks and saw where was my error. It all stood begind one "/" symbol xD. How typical! Inside Resources.Load I wrtoe "Images/Robots" instead of "Images/Robots*/*"
The important thing here was also that I didn't have the original resources of that example, so I did everything myself and realized I didn't have Resources folder created inside the project tab. When using Resources.Function_name you need to have Resources folder created! I missed that.
Instead of loading image inside Button function, I completely moved it into Card class's constructor function. That way resource isn't loaded on every frame, but instead on Card instance creation!
Relevant parts of the code:
class Card extends System.Object //Create the class
{
var img:Texture; //Create a texture variable
function Card(imgName:String) //The constructor is the very first function that gets called, automatically
{
//This is where I load the image! String of the image name is still the argument.
img = Resources.Load("Images/Robots/" + imgName);
}
}
[...]
function BuildGrid()
{
var kaart:Card;
[...]
for (k=0; k<rows; k++)
{
[..]
for(l=0; l<cols; l++)
{
kaart = aGrid[k][l];
//Here I point to the class variable to get the texture!
if( GUILayout.Button(kaart.str_img) )//, GUILayout.Width(cardW)) )
{
Debug.Log("4. Clicked on " + k + ";" + l);
}
}
[..]
}
[...]
}
Answer by Crypton · Jul 25, 2012 at 09:08 PM
Hope this works.
I solved my issue and stated the solution in the first post.
Your answer
Follow this Question
Related Questions
Creating a component from a button click 1 Answer
Change GUI.Button Texture on runtime 1 Answer
gui.button not display text and texture 2 Answers
GUI Texture on Button Swap 3 Answers
Show mouse click feedback on textured 2d plane button 2 Answers