Skip to content

Instantly share code, notes, and snippets.

@jmbeach
Created November 25, 2015 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jmbeach/78c3e46669db89628fce to your computer and use it in GitHub Desktop.
Save jmbeach/78c3e46669db89628fce to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEngine.UI;
using System.Linq;
public class BetterToggleGroup : ToggleGroup {
public delegate void ChangedEventHandler(Toggle newActive);
public event ChangedEventHandler OnChange;
public void Start() {
foreach (Transform transformToggle in gameObject.transform) {
var toggle = transformToggle.gameObject.GetComponent<Toggle>();
toggle.onValueChanged.AddListener((isSelected) => {
if (!isSelected) {
return;
}
var activeToggle = Active();
DoOnChange(activeToggle);
});
}
}
public Toggle Active() {
return ActiveToggles().FirstOrDefault();
}
protected virtual void DoOnChange(Toggle newactive)
{
var handler = OnChange;
if (handler != null) handler(newactive);
}
}
@elenzil
Copy link

elenzil commented Mar 5, 2019

minor changes here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment