using System; using System.Collections; using System.Collections.Generic; using Sirenix.OdinInspector; using UnityEngine; namespace Ether { public class ToggleGroupEx : MonoBehaviour { private List toggles = new List(); [ShowInInspector] private int selectIndex = 0; public int SelectIndex { set { selectIndex = value; if (selectIndex >= 0 && selectIndex < toggles.Count) { toggles[value].IsOn = true; } else { Debug.LogError("SelectIndex超出索引范围"); } } get => selectIndex; } /// /// 值改变事件 /// public ActionEvent OnValueChange { get; } = new ActionEvent(); public void AddToggle(ToggleEx toggle) { toggle.IsOn = false; toggles.Add(toggle); } public void SetToggleSelected(ToggleEx selectedToggle) { for (int i = 0; i < toggles.Count; i++) { ToggleEx toggle = toggles[i]; if (toggle != selectedToggle) { toggle.IsOn = false; } else { selectIndex = i; } } OnValueChange.Invoke(selectIndex); } private void OnDestroy() { toggles.Clear(); } } }