如果你想动态创建Button和Toogle 等等一系列控件,需要动态添加事件的如下.
拿button和Toogle抛砖引玉O(∩_∩)O~
using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.Events; public class Test3 : MonoBehaviour { public Button button; public Toggle toggle; void Awake() { //第一种 button.onClick.AddListener(MyOnClick); //第二种 button.onClick.AddListener(new UnityAction(MyOnClick)); //第三种,可以传递参数的 button.onClick.AddListener(delegate { MyOnClick(new GameObject()); }); //Toggle第一种 toggle.onValueChanged.AddListener(MyOnValue); //Toggle第二种 toggle.onValueChanged.AddListener(new UnityAction<bool>(MyOnValue)); } public void MyOnClick() { Debug.Log("单击"); } public void MyOnClick(GameObject meg) { Debug.Log("单击"); } public void MyOnValue(bool isOn) { if (isOn) { Debug.Log("被选中"); } else { Debug.Log("取消选中"); } } }
时间: 2024-10-03 10:18:56