- Home /
image in the button
hi, I'm following the book unity beginners guide by examples, and I'm having problems showing an image in a button, this is the code:
if(GUILayout.Button(Resources.Load(card.img), GUILayout.Width(cardDIM))) {
Debug.Log(card.img);
}
there's no error in the game, the card.img is a string that tell me the name "empty", the same name in the project panel of an image 100 x 100.
I don't know why this don't work, i've followed the book carefully
I'm having the same problem. To clarify:
Card c = aGrid[i][j]; if (GUILayout.Button(Resources.Load(c.cardImage), GUILayout.Width(cardWidth))) { Debug.Log(c.cardImage); }
cardImage is a string that is the resources name.
Even typing in "robot" (The name of the picture in Resources folder) directly does not work, since Resources.Load seems to be returning an Object ins$$anonymous$$d of a Texture.
Answer by clunk47 · Dec 04, 2013 at 05:18 AM
I'm going to give a fresh example. Create a folder called Resources in your Assets folder (project pane). For this example, just use a .png file and name it "Image", place it directly into your Resources folder. Now we'll create GUIContent and use it for the GUI Button. I've attached an example project .zip in case you have some issues with the following script:
#pragma strict
var image : Texture2D;
var content : GUIContent;
function Start()
{
image = Resources.Load("Image");
content.image = image;
}
function OnGUI()
{
GUI.Button(Rect(0, 0, 128, 128), content);
}
This works to some extent. If you simply want to use your texture as the button itself and not use built in background:
#pragma strict
var image : Texture2D;
function Start()
{
image = Resources.Load("Image") as Texture2D;
}
function OnGUI()
{
GUI.skin.button.normal.background = image;
GUI.skin.button.hover.background = image;
GUI.skin.button.active.background = image;
GUI.Button(Rect(0, 0, 128, 128), "");
}
You could of course use a GUIStyle to do this in the inspector, you could also use different images for each button state.
Thanks!! This was exactly that I was looking for: hiding the fancy border from the GUI Texture Button to show only the texture.
Answer by efge · Mar 19, 2011 at 10:54 PM
This function needs the name of the asset (or the asset path) as a string:
Resources.Load("card.img")
Edit:
var img : Texture;
if(GUILayout.Button(img, ...))
card is an object that I've created and img is the variable string,
card.img is set as "image", the name of the image.
I've tryed with Resources.Load("image"), but this doesn't work either
Answer by Uzquiano · Mar 19, 2011 at 11:19 PM
Hi,
You should just write in "" the name of the file you placed in the Resources folder inside your project, take away the extension. See the example:
if(GUILayout.Button(Resources.Load("card"));
card is an object that I've created and img is the variable string, card.img is set as "image", the name of the image. I've tryed with Resources.Load("image"), but this doesn't work either
Sorry, I didn't get your point. So, are you trying to show a string as an image?
Resources.Load() is used for loading asset which are placed in the Resources folder... I am totally lost, I don't get what you are trying, sorry
Hei, have you seen this in the reference? http://unity3d.com/support/documentation/ScriptReference/Resources.Load.html
The Description says: Loads an asset stored at path in a Resources folder.
Answer by $$anonymous$$ · Nov 16, 2012 at 11:54 PM
You need to explicitly state that the resource you are loading is a texture. Looks like Resource.Load(); returns a texture, but GUILayout.Button(); is not sure whether it is a texture, a string or something else.
if ( GUILayout.Button( (Texture)Resources.Load("robot"), GUILayout.Width(cardW) ) )
This worked for me. For C#. Not sure if casting is done the same way in JavaScript.
This works for me.. i am new so cant vote it up because dont have 15+ reputation :(
Answer by Parithi · Dec 04, 2013 at 05:02 AM
Just create a folder named "Resources" in your Assets folder -> Assets/Resources and place your robot.jpg inside the Resources folder. And Bam! Your code works...