using System.Collections.Generic;
using System.Linq;
using UnityEngine.UI;
namespace MyFrameworkPure
{
public static class ToggleGroupExtensions
{
private static System.Reflection.FieldInfo _toggleListMember;
///
/// 获取组中的所有Toggle
///
///
///
public static IList GetToggles(this ToggleGroup grp)
{
if (_toggleListMember == null)
{
_toggleListMember = typeof(ToggleGroup).GetField("m_Toggles", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
if (_toggleListMember == null)
throw new System.Exception("UnityEngine.UI.ToggleGroup source code must have changed in latest version and is no longer compatible with this version of code.");
}
return _toggleListMember.GetValue(grp) as IList;
}
///
/// 组中所有Toggle数量
///
///
///
public static int Count(this ToggleGroup grp)
{
return GetToggles(grp).Count;
}
///
/// 通过索引值获取Toggle
///
///
///
///
public static Toggle Get(this ToggleGroup grp, int index)
{
return GetToggles(grp)[index];
}
public static void SetActiveToggleByIndex(this ToggleGroup grp, int index)
{
IList toggles = GetToggles(grp);
toggles = toggles.OrderBy(x => x.transform.GetSiblingIndex()).ToList();
grp.SetAllTogglesOff();
toggles[index].isOn = true;
}
public static void SetActiveToggleByName(this ToggleGroup grp, string name)
{
IList toggles = GetToggles(grp);
grp.SetAllTogglesOff();
toggles.First(x => x.name == name).isOn = true;
}
public static int GetActiveIndex(this ToggleGroup grp)
{
Toggle activeToggle = grp.ActiveToggles().FirstOrDefault();
if (!activeToggle)
return -1;
List toggles = grp.GetToggles().OrderBy(x => x.transform.GetSiblingIndex()).ToList();
return toggles.IndexOf(activeToggle);
}
}
}