So I have a button, which I want to enable/disable based on the state of a ToggleGroup. For example imagine it’s a toggle Group which can have no single toggle selected. The corresponding button should only be active, when the toggleGroup has a toggle selected.
Any ideas on how to do this, short of attaching the button to all toggles or to do this during update…
Well, the ToggleGroup does have an “ActiveToggles” enumeration to show which toggles are on.
Also there is now a “AnyTogglesSelected” bool to indicate if any toggles bound the group are active.
So either of those should suit your needs. If you need it wired, then you could hook an event up to the “AnyTogglesSelected” property when it changes.
How can I hook something into that property? Does it expose any events?
Ok, just checked the source for the ToggleGroup and they haven’t given us many options:
https://bitbucket.org/Unity-Technologies/ui/src/b411f84ded13340515b6a48f1bfd2ea59d5348b8/UnityEngine.UI/UI/Core/ToggleGroup.cs?at=5.3&fileviewer=file-view-default
None of the fields are overridable or overloadable in anyway, which leaves you two options:
1: have a script that checks the state of the ActiveToggles property (or better the Count of ActiveToggles).
2: Copy the source and make your own Toggle group, exposing an event when any of the tracked toggles change.
3: A modified version of the Toggle script and ToggleGroup, to notify the toggle group when it changes and then cause an event to fire on the togglegroup when it changes.
Option 3 would be my preference (might even add it to the UI Extensions project) as it’s cleaner and avoids having to iterate through the Toggles list every update, which isn’t that performant.
If you pick a direction, let me know. Currently working on the next update to UI Extensions, so happy to squeeze this in but it’ll still be a few days before I can publish an updated asset (but you could grab the classes from source)
Eh, I just hooked onto the buttons, and then basically mirrored the toggle state there, now using the toggles only for visuals. It’s ugly, but works.
Right Scratch that, slight alteration. Seems toggle does indeed have an event that tells the togglegroup it has changed.
So you only need a modified version of the ToggleGroup 
yeah, buttons can be used, but as you state, it’s overkill and fugly. Almost finished a version that should work 
Ok, that was slightly more work than anticipated. But I have a version of the Toggle and Toggle group that produces the effect you were looking for.
Still needs a EditorMenuOption to create it. But if you create an extensions toggle group, then create your Normal UI Toggles and , swap out the script and set the Target Graphic (background) and Graphic (checkbox) it works fine
Added both events for when the group changes and also when a toggle state changes.
Might do more with it later.
let me know if you want it and I’ll push it up to source as is for now.
Sounds cool. Send over. Not sure if I’ll have time to check it out today, but I’ll happily give it a look sometime.
I think it would also be a cool addition if you could quickly access the one toggle that is on (if any).
Ok, will add that to the new control now and check it in
Although you can already access all the toggles that are on through the “ActiveToggles” IEnumerable
Uploaded to the 5.3 branch in the utilities folder
Hm. I thought ActiveToggles was all the registered Toggles, not the one that is active, because it returns an IEnumerable, where by definition, only on Toggle can be ON.
Nope, it reveals only toggles that are in an ON state in the group, as you can see in the code:
public IEnumerable<Toggle> ActiveToggles()
{
return m_Toggles.Where(x => x.isOn);
}
Basic linq expression to return items from the enumeration, in this case where x.isOn == true.
Which is weird, since there can ever only be one ON.
Not necessarily. You can start with them all ON in the current setup and them turn them off.
However, you can’t unselect the last one, which kind of breaks that pattern. Might improve this since it’s now in the extensions, so allow for more flexible working 
If you ask me, the functionality should be as follows:
The toggle group has the following fields:
public int minActiveElements = 1;
public int maxActiveElements = 1;
public enum ToggleMode { RadioButton, RadioToggle, LimitedToggle }
Then the following is the intended behavior:
If you click an inactive element you activate it. You can have at most maxActiveElements active.
If you click an active element, depending on the mode, you deactivate it. You have to have at least minActiveElements active.
The interesting part is what happens when these bounds are hit, which is where the ToggleMode comes into play:
In RadioButton it behaves as the current Toggle does: You can’t manually deactivate elements, you can only click on other elements to turn the others off. Here, if you hit maxActiveElements, the one that was the selected longest ago will be deselected automatically. With the current defaults this generates the same behavior as the original toggle.
in RadioToggle it behaves as RadioButton, but you can manually deactivate elements. If you deactivate one and go beneath minActiveElements, the first inactive Element in the list will be activated.
in LimitedToggle you can manually activate and deactivate elements, however once you hit the max of elements, all other elements will be non-interactable, to prevent the user from going over the max. Once the minimum is hit, all active elements will be non-interactable.
Or, you make the boundsBehavior (RadioBehavior, HardLimit) and canDeselect (bool) and canSelect (bool) into separate fields for more control on the side of the user.
You could also then add an enum for how the toggleGroup begins play: A random selection (min or max), the first items in the list (min or max), nothing selected or no change to the scene settings
With that you could have a system where 5 things are selected, and you can only deactivate until you have 2 remaining, for example.
Will add it to the list. Fancy logging that as a proposal issue on the BitBucket site with those details
and I’ll add them, (or you can re-work it and submit a PR
)
Hi, could you please send a link where I could find this ToggleGroup modification with event firing? I have a lot of toggles in one toggle group, and it’s looks a little weird to add change event for every toggle button to be able to track overall toggle group state change.
I was looking it in 5.3 branch in the utilities folder, but it only contains Pool classes.
Here as ExtensionsToggle and ExtensionsToggleGroup
https://bitbucket.org/ddreaper/unity-ui-extensions/src/13e291bef4ff636f49f34b4a949cc0fc7d8edd9d/Scripts/Utilities/?at=develop_5.3
I’m guessing you were looking in the Unity UI source and not the UI Extensions source