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?
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).
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.
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.
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;
}
}
}
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?
/// <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
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: