• 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
Question by Robs · Aug 29, 2014 at 09:39 AM · uibutton4.6

How do I create a button that toggles with the 4.6 UI?

I've been setting up my UI with the new UI released with the 4.6 beta and I can't seem to figure out a way to create a button that toggles. There is a button that you can press, and a check box, but no button that toggles.

I am essentially trying to have a button that I can press once and change the shape of my player, and then press it again to return the player to it's original shape. I also would like the graphic to toggle as well.

Am I missing something simple, or are toggle buttons not supported?

Comment

People who like this

0 Show 0
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

6 Replies

  • Sort: 
avatar image
Best Answer

Answer by meintgames · Sep 09, 2014 at 07:04 PM

Indeed it's easy to create one, but there actually is a Toggle button in 4.6 and even a ToggleGroup for radio buttons.

I used it in 4.60b17 and it's called UnityEngine.UI.Toggle, but I can't find any documentation about it

Comment
stevethorne

People who like this

1 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 StianC · Oct 10, 2014 at 08:18 PM 0
Share

At the top of the script you can always add

 using UnityEngine.UI;

That way you can just use the Toggle object type. Example:

 public Toggle someToggle;

avatar image pixelone · Nov 22, 2014 at 10:25 PM 0
Share

Thank you this was very helpful

avatar image chrissung · Jan 24, 2015 at 05:10 PM 0
Share

Worth noting (at least to me) that since ToggleGroup functions as component on a GameObject, one way to add is to use the Inspector, e.g. Add Component > UI > ToggleGroup

avatar image

Answer by denisb · Oct 11, 2014 at 03:56 AM

There is toggle in unity 4.6, with the new UI system, really easy to use. Just create a canvas right click on hierarchy then UI->Canvas Then right click on Canvas and UI->Toggle

if you want a toggle group, you can create an empty gameobject and add toggle group component to it, then for each toggle you created that you want inside that group, just link the group you just created on your toggles.

Comment
KitCoo
yashpal
sotirosn

People who like this

3 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 sotirosn · Feb 14, 2016 at 08:14 PM 0
Share

When you add the toggle button it looks like a checkbox with a label on its right. To make it more button like you can change the text label to an image which overlays the background and checkmark. Then make the checkmark image the same as the background image but with a different color. I think the background image is something like UISprite. Then when you click the activate the toggle the colored background will display on top of the unchecked background but under the image label.

avatar image

Answer by rageingnonsense · May 04, 2015 at 04:33 PM

This question was answered a long while ago, but it is the top result on google when searching for toggle buttons, so I am adding my solution here.

I had need for a color change, not a graphic change. the standard toggle does not handle this properly. Use the following overridden class in place of the standard Toggle if you want the capability to toggle the color state:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class ToggleButton : Toggle {
     public override void OnPointerClick(UnityEngine.EventSystems.PointerEventData eventData) {
         base.OnPointerClick(eventData);
 
         // override the color such that the toggle state of the button is obvious
         // by its color. 
         if (isOn) {
             image.color = this.colors.pressedColor;            
         } else {
             image.color = this.colors.normalColor;           
         }
     }
 }

Now the pressed color will be persistent if the toggle is on. Your highlight color should match the normal color if you do not want the last selected button to have a different color.

Comment
paranoidray
roddles

People who like this

2 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 Xtro · Jul 03, 2015 at 02:26 PM 0
Share

if you override pointer click event, you can't handle programatic isOn changes.

avatar image

Answer by Xtro · Jun 26, 2015 at 09:55 PM

This is better implementation and handles the programmatic isOn changes too.

 using System;
 using UnityEngine;
 using UnityEngine.UI;
 
 class ToggleButton : Toggle
 {
     #region Inspector
     // ReSharper disable InconsistentNaming
     Sprite normalSprite;
     // ReSharper restore InconsistentNaming
     #endregion
 
     protected override void Start()
     {
         base.Start();
 
         normalSprite = ((Image)targetGraphic).sprite;
         onValueChanged.AddListener(value =>
         {
             switch (transition)
             {
             case Transition.ColorTint: image.color = isOn ? colors.pressedColor : colors.normalColor; break;
             case Transition.SpriteSwap: image.sprite = isOn ? spriteState.pressedSprite : normalSprite; break;
             default: throw new NotImplementedException();
             }
         });
     }
 }

Comment
Heigl
Dave-Carlile

People who like this

2 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 Heigl · Jul 03, 2015 at 01:42 PM 1
Share

I lost the selection of Toggle-Buttons with SpriteSwap transition. This solved it! Thank you very very much

avatar image

Answer by HarshadK · Aug 29, 2014 at 09:42 AM

As far as I know, there is no toggle button as you want in new Unity 4.6 beta.

But you can easily create one if you want.

Have two Buttons placed at the same position so that one button will overlap another. Now write a script that will switch between these two buttons when any of that button is clicked. You can set different sprites for each button to show different states. And you can also perform the action of switching player's look when one button is clicked.

Comment
stevethorne

People who like this

1 Show 0 · 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
  • 1
  • 2
  • ›

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

9 People are following this question.

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

Related Questions

[4.6 UI] Button onSelect 1 Answer

Unity UI Button states 1 Answer

[4.6 b20] How to make a hold button for the new UI 4 Answers

Can't change UI Button Listeners 1 Answer

[4.6 UI] Possible to reference parameter of OnClick persistent event? 0 Answers


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