uGUI Graphical Toggle Button

Hey all,

I’m trying to port over my old GUI to the new system. I have a button that toggles graphically between ON state and OFF state each time you press it.

I tried doing it with the Toggle UI, but it only toggles the button’s visibility (no image swap available). The Button UI on the other hand, reverts back to its original state once you release the mouse button.

Is there an UI component that does graphical toggle, besides me doing function calls and change the image by script? Did I miss something?

Thanks!

There is no such component now and it won’t be available in 4.6 final release.
All you can do now is to use Toggle with “Checkmark” image which covers “Background” image (names are from default Toggle created with Unity menu).

1 Like

nope, doesn’t seem like you missed anything.

1 Like

It’s something we will be adding, but it won’t be ready for version one. Once the source is available you should be able to see how we do things and add your own.

1 Like

I see! Thanks for the replies.

Yeah! It would be very nice if unity developers added this in Toggle component or offered another solution. I often need do elements similar this(image below). But in every UI(dfgui, ngui, here) i invent something new. Now I use the crutch: I do function so called “toggleChangedHandler”. it runs always when toggle state changed…It simply sets backgrounds alpha of toggle to 0 when toggle changed to active(now Toggle.isOn),and returns background alpha to 255 when toggle changed to inactive.

1757898--111308--extention_toggle.PNG

I had the same problem, so I wrote a little class that would work around this. It simply disables a graphic when the toggle is enabled. You can use the “checkmark” from the standard toggle as the on state, and just create another image as the off state. Attach this script to the toggle and assign the image you want to hide to the script.

using UnityEngine;
using UnityEngine.UI;

[RequireComponent(typeof(Toggle))]
public class ToggleHide : MonoBehaviour {
    public Graphic graphic;

    void Start() {
        GetComponent<Toggle>().onValueChanged.AddListener((value) => {
            OnValueChanged(value);
        });
    }

    void OnValueChanged(bool value) {
        if (graphic != null) {
            graphic.enabled = !value;
        }
    }
}
4 Likes

@vatara I modified your script, so you can place it on a button, and then drag any GameObject to the inspector.
Thanks!

using UnityEngine;
using UnityEngine.UI;

[RequireComponent(typeof(Button))]
public class ToggleElement : MonoBehaviour {
    private bool visible = false; //show or hide at start
    public GameObject go;

    void Start() {
        GetComponent<Button>().onClick.AddListener(() => {
            OnClick();
        });
        OnClick(); //set initial state
    }

    void OnClick() {
        visible = !visible;
        go.SetActive(!visible);
    }
}
1 Like

Any word on this becoming a standard feature, yet?

Its something that is used VERY often. Seems really bizarre that it isn’t there. Guess i’ll go back to scripting the entire GUI.

I would like an update from Unity concerning this as well. It seems like a strange oversight to leave out something this basic from the standard UI functionality. Or is it so basic that everyone is expected to write their functionality for this on their own?

I’ve tried writing this:

/// <summary>

/// A Toggle that also sets a gameobject active when the toggle is on, and inactive when the toggle is off.

/// </summary>
public class SettingsToggle : Toggle

And it works fine, but requires a lot of hacking around Unity because a lot of the methods and properties are not defined as virtual; so I need to use the non-type safe reflection method described here c# - How do I use reflection to invoke a private method? - Stack Overflow

In order to call Set(). I also have to switch the Unity editor to debug mode in order to swap out the toggles I already have for the new SettingsToggle, because Unity saw fit to remove that feature from the regular editor in version 5.

You also have to build a whole new Editor, or find out how to override the ToggleEditor (which I haven’t been able to find yet) It seems indeed that UnityEngine.UI is just not built for extending; I’m going to switch to the component listener approach above, even though it is less elegant than what should be possible, if this were a mature GUI library:

protected override void Set(bool value)
{
    myAddedFeature.SetEnabled(value);
    base.Set(value);
}