Unity3D Button组管理(给按钮的onclick事件“传递参数”)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

// 利用委托来进行按钮组的管理
public class test07 : MonoBehaviour {
// 脚本挂在一个Canvas上,其子物体上有两个Button:Button 喝 Button(1)
delegate void argument(string str);
void Start () {
argument arg = new argument(test);
arg = arg + test1;
for (int i = 0; i < gameObject.GetComponentsInChildren<Button>().Length; i++)
{
Button btn = gameObject.GetComponentsInChildren<Button>()[i];
btn.onClick.RemoveAllListeners();
btn.onClick.AddListener(() =>
{
if(arg != null){
arg(btn.name);
}
});
}
}

void onclicked(){

}
public void test(string str){
switch(str){
case "Button":
print("button");
break;
case "Button(1)":
print("button");
break;
default: print("nothing");
break;
}
}
public void test1(string str)
{
print(str);
}
}

猜你喜欢

转载自www.cnblogs.com/BXLH/p/10437424.html