需求:下面的Toggle是一组,有共同的ToggleGroup,如何获取你当前选择的Toggle.
解决:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ToggleGroupTest : MonoBehaviour
{
//ToggleGroup toggleGroup;
// Use this for initialization
void Start () {
//toggleGroup = transform.GetComponentInChildren<ToggleGroup>();
List<Toggle> toggleList = new List<Toggle>(transform.GetComponentsInChildren<Toggle>());
foreach (Toggle item in toggleList)
{
item.onValueChanged.AddListener(delegate(bool isOn)
{
if (isOn)
{
Debug.Log(item.name + "被选中了");
}
});
}
}
网友评论