- Home /
Make a Button Behave Like a Toggle
Hello! I am working with the unity Gui and have run into a interesting problem. I want to make a button behave like a toggle or make a toggle appear like a button. I know about GUI Skins, but can't see any way to change the appearance of a toggle appear that drastically.
Thanks.
Answer by randomuser · Mar 19, 2013 at 03:55 AM
I ended up adding new skins for each state of the button and not using a toggle becuase it was harder to edit to look like a button. I had a function that decided what skin the button should have before it was drawn in onGUI().
This is a simple code for alternative toggle button, here i using GUI Button
using UnityEngine;
using System.Collections;
public class SimpleAlternativeToggleButton : $$anonymous$$onoBehaviour
{
public bool boolToogleButton;
public GUISkin $$anonymous$$yGUISkin;
void OnGUI()
{
if (boolToogleButton == false)
{
if(GUI.Button (new Rect(Screen.width*0.7f,Screen.height*0.3f,Screen.width *0.02f,Screen.width *0.02f),"",$$anonymous$$yGUISkin.customStyles[1] ) ) // $$anonymous$$yGUISkin.customStyles[1] is unselected button image
{
boolToogleButton = true;
Debug.Log ("Tick On");
}
}
else
{
if(GUI.Button (new Rect(Screen.width*0.7f,Screen.height*0.3f,Screen.width *0.02f,Screen.width *0.02f),"",$$anonymous$$yGUISkin.customStyles[2] )) // $$anonymous$$yGUISkin.customStyles[2] is selected button image
{
boolToogleButton = false;
Debug.Log ("Tick Off");
}
}
}
}
http://answers.unity.com/answers/553697/view.html much better answer
Answer by FredericRP · Oct 11, 2013 at 09:54 AM
It's a very old question, but if anyone search for the answer, there's a simple way to do this.
Use a GUI.Toggle and set its style as "Button" :
bool pressed = true;
OnGUI() {
pressed = GUILayout.Toggle(pressed, "Toggle me !", "Button");
}
for more info about this:
http://docs.unity3d.com/Documentation/Components/gui-Customization.html
awesome that worked great for me, now I have a toggle button so I can switch between Accelerometer and Steer handle :)
Old, but helpful post! Since this is a top Google result, I thought I'd add this small caveat I ran into for a different use case.
@FredericRP 's answer works like a toggle, which answers the question. However, toggles evaluate to true every OnGUI call in which they are down.
In my case, I wanted a button that would evaluate to true only when it was pressed, like a normal button--but which looked like this button-styled checkbox, so I could indicate a checked/unchecked (up/down) state.
Here is the code, where justPressed
indicates the Toggle was clicked in that frame.
bool checked = true;
OnGUI() {
var justPressed = GUILayout.Toggle(checked, "Press me!", "Button") != checked;
}
Answer by mafanbong8819 · Feb 02, 2017 at 10:27 AM
You can use if statement then with modulus (%), for example, if(counter%2==0), this is toggle button. If you have three conditions, you can use if(counter%3==0) , four conditions you can use if(counter%4==0)
so every time the button is clicking, counter ++,
Here, I recommend you a simple video for toggle button using UI button, video tutorial
example script is as below, every simple and easy to understand.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class textBtn : MonoBehaviour {
public Text mytext = null;
public int counter = 0;
public void changeText()
{
counter++;
if (counter % 2 == 1) {
mytext.text = "Pause";
} else {
mytext.text = "Start";
}
}
}
This question is from 2012 and has already been answered. Please stop gravedigging.
Answer by DayyanSisson · Sep 08, 2012 at 12:08 AM
Look at the documentation on GUI Buttons. You can apply a texture to the button to make it appear how you like.
Yes, but can you do the same to make a toggle look like a button?
or can you control the state of the button (clicked and unclicked)?
simple code for clicked and unclicked use one boolean and when pressed check what the boolean is and then change the boolean. so something like
var onOff : boolean = false;
function Update { if(Input.Get$$anonymous$$ouseButtonDown(0)) { if(onOff == false) { //do something onOff = true; } else if(onOff == true) { //do something onOff = false; } } }
Answer by Zaffer · Sep 08, 2012 at 02:49 AM
Hi Randmuser,
Here is a similar example from Sue Blackman's book, "Beginning 3D Game Development with Unity", page 468. She makes a GUI object look like a box, but behave like a label by creating a custom GUI style that looks like a box control and then adding it to a label control using the label's style parameter. Here's the code:
`var boxStyleLabel : GUIStyle; // make a label that looks like a box`
now change this line:
GUI.Box (Rect (Screen.width/2 - 250, Screen.height - 37, 500,35), " This is the text string for a Box Control ");
to this:
GUI.Label (Rect (Screen.width/2 - 250, Screen.height - 37, 500,35), "This is the text string for a fake Box Control", boxStyleLabel);
Maybe you can do something similar with your buttons and toggles.
Zaffer
`
Your answer
Follow this Question
Related Questions
Toggle GuiTexture 1 Answer
How do I make a custom font for a GUI button? C# 1 Answer
Distribute terrain in zones 3 Answers
Do Something ONLY when all Toggles are On or Off 4 Answers
Problem with rect and touch 1 Answer