快速获取按钮类型的点击事件
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class aaaa : MonoBehaviour {
private GameObject tests;
private Button[] btns;
// Use this for initialization
void Start ()
{
tests = transform.gameObject;
btns = tests.GetComponentsInChildren<Button>();
for (int i = 0; i < btns.Length; i++)
{
GameObject sender = btns[i].gameObject;
btns[i].onClick.AddListener(() =>
{
OnClickBtn(sender);
});
}
}
void OnClickBtn(GameObject obj)
{
switch (obj.name)
{
case "1":
Run(obj.name);
break;
case "2":
Run(obj.name);
break;
default:
break;
}
}
private void Run(string name)
{
print(name + "跑");
}
}
直接拖入按钮
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class OnClickBtn : MonoBehaviour {
// Use this for initialization
void Start ()
{
GetComponent<Button>().onClick.AddListener(() =>
{
AAA();
});
}
void AAA()
{
}
// Update is called once per frame
void Update () {
}
}
网友评论