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);
}
}
最新文章
- 集成学习之Adaboost算法原理小结
- sublime
- WCF实现客户端自动更新
- Android SDK Manager 更新代理配置 ,蛋碎了
- thinkphp 模板中赋值
- Sublime text3 JS语法检测工具安装及使用
- hdu Diophantus of Alexandria(素数的筛选+分解)
- Concurrent usage detected
- linux--磁盘及文件系统管理
- android ViewPager之PagerAdapter中View的重用
- Jenkins知识地图
- Luogu P1533 可怜的狗狗
- java对象流与序列化
- Django基础四之模板系统
- appcmd创建站点、应用程序、虚拟目录批处理程序
- Python Monitoring UPS with SNMPWALK
- 第六章在U盘上运行openwrt(引导)--补
- 11-09SQLserver 基础-数据库之汇总练习45题
- URAL 1934 spfa算法
- [置顶]
 docker1.12--docker命令行帮助文档