Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
7
Question by randomuser · Sep 08, 2012 at 12:04 AM · c#guibuttonstoggle

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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().

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LijuDeveloper · Oct 11, 2013 at 11:13 AM 0
Share

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");
             }
         }
     }
 
   }




avatar image AnKOu · Aug 01, 2018 at 03:39 PM 0
Share

http://answers.unity.com/answers/553697/view.html much better answer

avatar image
37

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");
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sdgd · Jan 23, 2014 at 10:46 PM 1
Share

for more info about this:

http://docs.unity3d.com/Documentation/Components/gui-Customization.html

avatar image Rafi-Khan · Sep 10, 2014 at 06:48 AM 0
Share

awesome that worked great for me, now I have a toggle button so I can switch between Accelerometer and Steer handle :)

avatar image arichards · Apr 23, 2019 at 09:29 PM 0
Share

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;
 }
avatar image
0

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";
         }
     }
 }
 



Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ForeignGod · Feb 02, 2017 at 12:16 PM 0
Share

This question is from 2012 and has already been answered. Please stop gravedigging.

avatar image
0

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image randomuser · Sep 08, 2012 at 12:11 AM 0
Share

Yes, but can you do the same to make a toggle look like a button?

avatar image randomuser · Sep 08, 2012 at 12:12 AM 0
Share

or can you control the state of the button (clicked and unclicked)?

avatar image DayyanSisson · Sep 08, 2012 at 12:12 AM 0
Share

Yes you can:

http://docs.unity3d.com/Documentation/ScriptReference/GUI.Toggle.html

avatar image reptilebeats · Sep 08, 2012 at 01:40 AM 0
Share

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; } } }

avatar image reptilebeats · Sep 08, 2012 at 01:41 AM 0
Share

or rather this will just change when u click down

avatar image
0

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

`

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Putting Dictionary/List using foreach as buttons in a scroll view? 3 Answers

My OnGUI() Won't show the Button elements :( 0 Answers

One variable set TRUE when another must be false. TOGGLE 3 Answers

Using an Enum for GUI.Toggle? 1 Answer

Toggle not work in next scene 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges