- Home /
How Do I Make GUI ON Off Button with 2 Button States
OK I have a button with 2 states one one button is not lighted 2nd button state is lighted. So when you click the button it swaps out the GUI button image for the lighted one and when clicked again it swaps it back to the un lighted button. Seems simple enough but I can't find a working sample of how this is done anywhere,
Answer by b1gry4n · Nov 30, 2014 at 05:58 PM
if (GUI.Button (Rect (20,40,80,20), "Click")) {
ButtonToggle();
}
set up a boolean. on button click call a function.
private bool isClicked = false;
void ButtonToggle(){
if(isClicked){
isClicked = false;
//swap texture to OFF
}else{
isClicked = true;
//swap texture to ON
}
}
Thanks b1gry4n Does this need a var set up for each button texture?
Yes, you would typically see a variable for both textures.
I'm probably writing it wrong I keep getting errors.
Answer by YoungDeveloper · Nov 30, 2014 at 06:29 PM
Triggering between states is as easy as using simple bool.
private bool state = false;
private void Update(){
if (Input.GetButtonDown("Fire1")){
state = !state;
if(state) Debug.Log("ON");
else Debug.Log("OFF");
}
}
The rest is up to you how you visually represent it. I'd suggest using 4.6 UI if your not familiar with OnGUI().
What I want to do should be old hat I would have thought. All I want to do is make 2 states. One on and one off when clicked it stays one click again button goes off... $$anonymous$$aybe I'm not explaining it right?
You are explaining it right, you don't have basic understanding for scripting, that's all. Did you wrapped my example in class and added to gameobject?
This will start you going. Trigger between states by clicking right mouse button.
using UnityEngine;
public class Test : $$anonymous$$onoBehaviour {
private bool state = false;
private void Update(){
if (Input.GetButtonDown("Fire1")){
state = !state;
if(state) Debug.Log("ON");
else Debug.Log("OFF");
}
}
private void OnGUI(){
if(state){
GUI.Box(new Rect(0,0,100,30), "ON"));
}
}
}
Create c# script called Test, paste the content, add script to gameObject.
Answer by Cherno · Dec 04, 2014 at 04:33 PM
Sounds like you could just use GUI.Toggle.
if(GUI.Toggle(yourRect, yourToggleBool, "buttonName") != yourToggleBool) {
Event.current.Use();
yourToggleBool = !yourToggleBool;
return;
}
You can pass it a custom GUI style of your liking that has the proper button on/off images as it's Toggle style.
Thanks Cherno, How would I make this work with my 2 GUI Texture button states? I would need a var for each texture yes?
O$$anonymous$$ This is what I have as java code right now that changes the button on mouse down and back to normal on mouse up. Which is perfect fro my ($$anonymous$$omentary Buttons) I want to make a code similar to this that creates a (Latching button) i.e. Click once for on, click again for off.
//var levelToLoad : String;
var mouseUpTexture : Texture2D;
var mouseDownTexture : Texture2D;
var beep : AudioClip;
function On$$anonymous$$ouseDown(){
guiTexture.texture = mouseDownTexture;
audio.PlayOneShot(beep);
}
function On$$anonymous$$ouseUp(){
guiTexture.texture = mouseUpTexture;
yield new WaitForSeconds(0.3);
//Do Something
}