一、目的
1、想知道:Unity中动态给button的OnClick添加代码
1、想知道:比如预制件产生按钮后,这个按钮身上的OnClick不见了,所以想Unity中动态给button的OnClick添加代码
二、参考
1、Unity用代码给按钮添加点击事件
https://blog.csdn.net/qq_18995513/article/details/53159107
- 总结:待检测
1、在unity中动态为button添加带参数的点击事件
https://www.jianshu.com/p/e24ec59ac0e8
- 总结:good:模仿它进行写的,超级好
三、操作:一:将代码挂在按钮身上
1、预制件的OnClick里面代码是空的,所以新建一个代码,给这个button
1、MyButton_close.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MyButton_close : MonoBehaviour
{
void Start()
{
this.GetComponent<Button>().onClick.AddListener
(delegate()
{
OnClick_close(this.gameObject);
});
}
public void OnClick_close(GameObject _obj)
{
print("点击了按钮:"+_obj.name);
}
}