Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Toggle button

Discussion in 'UGUI & TextMesh Pro' started by Devil_Inside, Oct 3, 2015.

  1. Devil_Inside

    Devil_Inside

    Joined:
    Nov 19, 2012
    Posts:
    1,117
    I know there is a Toggle, but it's designed to work as a checkbox, where an additional graphic is either shown or hidden on top of the main button shape. I rather need a way to highlight the actual button shape when it is toggled on, and revert to normal mode when it is turned off.
    It would be really great if another "Toggled" color state is added to the regular Toggle class, so the Toggle could serve both purposes.
    What do you think?
     
  2. jcredible

    jcredible

    Joined:
    Dec 4, 2012
    Posts:
    24
    Yes, I think Unity should add this to their list of standard ui containers.

    That being said, I made a simple one not too long ago. I have a fair amount of ui stuff I would like to share at some point, but for now here's the toggle:

    Code (CSharp):
    1. public class ToggleButton : MonoBehaviour
    2. {
    3.     public Text text;
    4.     public string pressedText;
    5.     public string unpressedText;
    6.  
    7.     public void OnToggle(bool state)
    8.     {
    9.         if (state)
    10.         {
    11.             text.text = pressedText;
    12.         }
    13.         else
    14.         {
    15.             text.text = unpressedText;
    16.         }
    17.     }
    18. }
    I add that class to a standard toggle. Then see the attached for the hierarchy. There's an overlay and a standard background. The overlay is a darker color whereas the background the typical background I want.

    You can see on the attributes, I have the background as the transition image and then the overlay as the graphic. When you click on the "button" it gets darker and also changes the text (calling the OnToggle).

    It's not super exciting but only took me a few mins to create and I think that says something about the awesomeness of Unity!
     

    Attached Files:

    Devil_Inside likes this.
  3. Devil_Inside

    Devil_Inside

    Joined:
    Nov 19, 2012
    Posts:
    1,117
    Thanks!
    That's what I'll probably use for now. What bothers me a bit is that this adds to the overdraw, and my UI is already kind of heavy. I really hope Unity will add a separate color state for the toggle.
     
  4. mafanbong8819

    mafanbong8819

    Joined:
    Feb 2, 2017
    Posts:
    8


    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. public class textBtn : MonoBehaviour {
    6.  
    7.     public Text mytext = null;
    8.     public int counter = 0;
    9.     public void changeText()
    10.     {
    11.         counter++;
    12.         if (counter % 2 == 1) {
    13.             mytext.text = "Pause";
    14.         } else {
    15.             mytext.text = "Start";
    16.         }
    17.     }
    18. }