Unity3d代码添加button.OnClick()事件的方法
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class MyTest : MonoBehaviour
{
public Transform[] myBtn; //对应的Button按钮数组
void Start()
{
Button btn;
for (int i = 0; i < myBtn.Length; i++)
{
btn = myBtn[i].GetComponent<Button>();
btn.onClick.AddListener(delegate()
{
this.OnClick(myBtn[i]);
});
}
}
private void OnClick(Transform trans)
{
switch (trans.name)
{
case "BtnPlay":
break;
default: break;
}
}
}
文章来自:http://www.cnblogs.com/Yellow0-0River/p/4239715.html