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. Dismiss Notice

ToggleButton control

Discussion in 'UGUI & TextMesh Pro' started by Mathieu-Girard, Dec 12, 2014.

  1. Mathieu-Girard

    Mathieu-Girard

    Joined:
    Sep 25, 2014
    Posts:
    13
    Hi all,

    I was wondering if anyone else had a need of a ToggleButton control: something which looks like the current UI.Button, but behaves like a toggle and stays armed when its boolean state is set to true.

    uGUI offers a UI.Toggle control, but it is basically a checkbox control with a graphical marker which is on or off. What I am looking for is something which behaves as a Tab to navigate between subpanels.

    I have looked into the code, and it seems that the background disarming when releasing the mouse button is coded at the "Selectable" level. So if I want to have a button with a toggle behaviour, I have to rewrite the code and inspector with something parallel to Selectable.

    Has any one else found another solution?

    Thanks!
     
  2. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,181
    Hi, we have plans to add a toggle button. Until then you can 'mimic' the functionality with a single toggle and a 'selected' image that looks like the button is pressed.
     
  3. Mathieu-Girard

    Mathieu-Girard

    Joined:
    Sep 25, 2014
    Posts:
    13
    Great news, thanks!
     
  4. yudixiaok

    yudixiaok

    Joined:
    Aug 29, 2014
    Posts:
    17
    HI @Tim C
    Do you plan to add ListView to new UI. If so, I Hope that the ListView can support group by key word and we can add separator to it
     
  5. mafanbong8819

    mafanbong8819

    Joined:
    Feb 2, 2017
    Posts:
    8
    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.

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