1. 通过直接绑定脚本来绑定事件
(1) 建一个ButtonTest脚本,挂在一个游戏对象上(里面的方法public或private都可以).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ButtonTest : MonoBehaviour {
private GameObject Obj;
private void Start()
{
Obj = GameObject.Find("Button");
Obj.GetComponent<Button>().onClick.AddListener(Func1);
Obj.GetComponent<Button>().onClick.AddListener(Func2);
}
void Func1()
{
print("执行了Func1方法!");
}
public void Func2()
{
print("执行了Func2方法!");
}
}
(2) 运行,点击按钮执行两个方法.
Obj.GetComponent<Button>().onClick.AddListener(Func1);可以换成
Obj.GetComponent<Button>().onClick.AddListener
(
delegate () {Func1(); }
);
(1) 建一个ButtonTest脚本,挂在一个游戏对象上(里面的方法public或private都可以).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ButtonTest : MonoBehaviour {
private GameObject Obj;
private void Start()
{
Obj = GameObject.Find("Button");
Obj.GetComponent<Button>().onClick.AddListener(Func1);
Obj.GetComponent<Button>().onClick.AddListener(Func2);
}
void Func1()
{
print("执行了Func1方法!");
}
public void Func2()
{
print("执行了Func2方法!");
}
}
(2) 运行,点击按钮执行两个方法.
Obj.GetComponent<Button>().onClick.AddListener(Func1);可以换成
Obj.GetComponent<Button>().onClick.AddListener
(
delegate () {Func1(); }
);