也可以添加事件的UI都可以这样添加事件监听
using UnityEngine;
using UnityEngine.UI;
public class Test : MonoBehaviour
{
private Toggle toggle;
void Awake()
{
toggle = toggleObj.GetComponent<Toggle>();
}
void Start()
{
//button1.onClick.RemoveAllListeners();//清楚之前的监听,以免冲突
//lambda表达式转换为委托类型
toggle.onValueChanged.AddListener((bool value) => OnToggleClick(toggle, value));
}
public void OnToggleClick(Toggle toggle, bool value)
{
Debug.Log("toggle change " + (value ? "On" : "Off"));
}
}