- Home /
GUI.Button Texture Swap?
Is there an way to use the on press functionality of a GUI.Button to change the texture assigned to it. Something to the effect of,
public Texture2D Texture1 public Texutre2D Texture2
void OnGUI() {
if (GUI.Button (new Rect (50,410,60,60), Texture1)) {
//Swap to Texture2
}
Answer by ThumbStorm · Aug 11, 2010 at 08:19 PM
first make a main texture "textureButton" then assign that texture " Texture1" in the awake function. Use the 3rd texture "Texture2" when you want to swap it
var Texture1 : Texture; var Texutre2 : Texture; var TextureButton: Texture;
function Awake() { TextureButton = Texture1; }
void OnGUI() {
if (GUI.Button (new Rect (50,410,60,60), TextureButton)) {
TextureButton = Texture2; //Swap to Texture2
}
Answer by siberman · Jan 25, 2014 at 01:15 AM
I think this is a more elegant solution. If you set a bool with the button you can use it to determine the texture in line, e.g.
if( GUI.Button( myRect, myBool ? texture1:texture2 ) )
{
myBool = !myBool;
}
This should swap the texture each time the button is pressed.
Answer by DarkSlash · May 20, 2014 at 04:45 PM
I have the same problem on this post http://answers.unity3d.com/questions/710962/change-guibutton-texture-on-runtime.html but the texture is not changing! I also tried to apply the elegant solution but it hasn't work! Any clue?
Your answer
Follow this Question
Related Questions
Make a Button out of a Textured Plane 2 Answers
Buttons and images 1 Answer
GUI Button Texture only showing as small 1 Answer
GUI buttons look fine on full screen but distorted otherwise? 0 Answers
Change the texture of a GUI BUTTON 1 Answer