- Home /
GUI Texture on Button Swap
I have a button. I want it to switch textures when its clicked and return to normal when let go. Its a GUI Texture. Right now both "Up" and "Down" versions of the texture are in my project folder and not attached to any object. In the Inspector both texture types are set to "GUI".
The primary issue Im having it getting my variables to call for the textures and getting my IF statement to do the swap. This is what I have:
var btnSoldier_Up = GUI_Soldier_Up.Texture(109,96);
var btnSoldier_Down = GUI_Soldier_Down.Texture(109,96);
function OnGUI() {
if (GUI.Button (Rect(0,0,109,96),btnSoldier_Up))
renderer.material.mainTexture = btnSoldier_Down;
}
According to the help document linked below, it looks like this should work. But it's not. Any suggestions? http://unity3d.com/support/documentation/ScriptReference/Texture2D.Texture2D.html http://unity3d.com/support/documentation/Components/class-Texture2D.html
Unfortunately most of the help areas I found on searching were for C#, Im programming in Javascript so it's been a bit confusing for me. Thanks in advance!
Answer by Teriander · Jan 18, 2012 at 08:40 AM
Thanks for replying Danny, I kind of wanted to avoid using GUIStyle because I tried uses it last week for other purposes but had problems. I think if I went back to it, it could solve my button issues but the old problems would return. I feel it could be easier to find a simple button mouseover effect.
I've made some changes to my old code and I think Im really close. But not sure what Im missing. If you can look at this and tell me what you think that would be great.
//Get Component Variables var btnSoldier_Up : GUITexture; var btnSoldier_Down : GUITexture;
function Start () {
// Get Component to display Unit Purchases
btnSoldier_Up = GameObject.Find ("GUI_Soldier_Up").GetComponent (GUITexture);
btnSoldier_Down = GameObject.Find ("GUI_Soldier_Down").GetComponent (GUITexture);
}
function OnGUI() {
//Create Buttons for Unit Purchases.
if (GUI.Button (Rect(0,0,109,96),btnSoldier_Up)) {
btnSoldier_Down.enabled = true;
}else {
btnSoldier_Down.enabled = false;
}
}
The code seems to be accepted but I get this error message:
Assets/Script_SceneManager.js(71,24): BCE0023: No appropriate version of 'UnityEngine.GUI.Button' for the argument list '(UnityEngine.Rect, UnityEngine.GUITexture)' was found.
The error is stating that you're simply using a GUITexture as the texture property of your GUI.Button. You need to change btSoldier_Up to btnSoldier_Up.texture in your GUI.Button if statement
Answer by dannyskim · Jan 18, 2012 at 08:22 AM
It's pretty confusing on what you're actually trying to accomplish here. I think this is because you have certain GUI elements confused on what they actually are, and how to access the properties of them.
If you're trying to change the texture of a
GUI.Button
this is not changed through the renderer.material.mainTexture, seeing as how no GUI element or components that are part of the GUI class actually have a renderer. In fact, a
GUITexture
does not have a renderer attached to it either. It seems like you understand how to access the texture of a GUITexture though, because of your variable declarations with GUI_Soldier_Up and GUI_Soldier_down.
I can also see that you applied a texture to the GUI.Button, but I am not aware of anyway to access that property without using a GUIStyle. If you try to make the GUI.Button a variable and try to access any of its properties like so:
var myButton = GUI.Button( new Rect( 0, 0, 0, 0 ), myTexture );
you'll see that no properties are available to be changed.
So using a GUIStyle, you can do this:
var myStyle : GUIStyle;
if (GUI.Button (Rect(0,0,109,96), "", myStyle ))
// Do Something here
When you declare the GUIStyle, in the inspector you'll see a whole bunch of settings that you can change for different button states. So On Normal, you can set a Texture, On Active, you can set a texture, etc.
These can be accessed through the properties of the GUIStyle that you created:
myStyle.onActive.background = myTexture2D;
myStyle.onNormal.background = myTexture2D_1;
The title of your post is misleading, which states that you want to change the texture property of a GUITexture, which is not a GUI.Button. If this is so, then you can do:
var myGuiElement : GUITexture;
var myTexture2D : Texture2D;
myGuiElement.texture = myTexture2D;
but it seems like you already know how to do this.
One other thing, is I suggest you not use dynamic typing. One, it slows down your game in practically every case, and two, it makes your code harder to read and harder to keep track of. Strictly casting a type to your variables will save you a lot of trouble in the long run and leads to cleaner coding habits, imho. Also, if you plan on deploying to a mobile platform, then iOS doesn't allow dynamic typing for one, and two, using practically any of Unity's GUI implementations is not suggested because of resource hogging by said system. It's only good for menus with no gameplay action, and even then I wouldn't suggest using it.
Answer by Teriander · Jan 18, 2012 at 10:14 AM
Thank you! The button is working without errors now, but the image is not displaying. Im going to have to play around a bit and see why it can't find the graphic. But I think this brings me closer to the solution! Appreciate your support!!
If you happen to notice off hand why the graphic wouldn't display Im all ears :) Till then I'll do some research. Thanks again!
$$anonymous$$aybe you can try
myStyle.active.background = myTexture2D; myStyle.normal.background = myTexture2D_1;
I just tried, the image is displayed.