IndieGame/client/Assets/Ether/Scripts/Module/Extension/UGUI/Toggle/ToggleGroupEx.cs

69 lines
1.2 KiB
C#
Raw Permalink Normal View History

using System;
using System.Collections;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using UnityEngine;
namespace Ether
{
public class ToggleGroupEx : MonoBehaviour
{
private List<ToggleEx> toggles = new List<ToggleEx>();
[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;
}
/// <summary>
/// 值改变事件
/// </summary>
public ActionEvent<int> OnValueChange { get; } = new ActionEvent<int>();
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();
}
}
}