60 lines
1.5 KiB
C#
Raw Normal View History

2025-05-18 01:04:31 +08:00
// ***********************************************************************
// Assembly : Unity
// Author : Kimch
// Created : 2018-03-29
//
// Last Modified By :
// Last Modified On :
// ***********************************************************************
// <copyright file= "KUIToggleGroup" company="Kunpo"></copyright>
// <summary></summary>
// ***********************************************************************
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
[RequireComponent(typeof(ToggleGroup))]
public class KUIToggleGroup : MonoBehaviour
{
public class ToggleEvent : UnityEvent<int>
{
public ToggleEvent()
{
}
}
public readonly ToggleEvent onToggleSelected = new ToggleEvent();
private Toggle[] _toggles;
public Toggle[] toggles => _toggles;
private void OnToggleValueChanged(bool value)
{
if (value)
{
for (int i = 0; i < _toggles.Length; i++)
{
if (_toggles[i].isOn)
{
onToggleSelected.Invoke(i);
break;
}
}
}
}
#region MonoBehaviour
private void Awake()
{
_toggles = GetComponentsInChildren<Toggle>(true);
foreach (var toggle in _toggles)
{
toggle.onValueChanged.AddListener(OnToggleValueChanged);
}
}
#endregion
}