Unity3d 的 Delegate使用方法
1.在Delegate 文件
using UnityEngine;
using System.Collections;
//定义Delegate
public delegate void TestDelegate(int a);
public class DelegateScripts : MonoBehaviour {
//使用
public event TestDelegate testDelegate;
public void DoSomeThing() {;
//调用 testDelegate 作为回调方法
testDelegate(111);
}
public void TestTwoDemo()
{
print("TestTwo22");
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
2.在调用Delegate文件里面
private DelegateScripts delegateScripts;
void Start () {
int a = 3;
delegateScripts = GameObject.FindObjectOfType<DelegateScripts>();
//定义delegate 作为回调 方法 (callBack)
delegateScripts.testDelegate += new TestDelegate(callBack);
//调用delegateScripts的DoSomeThing 方法,回调方法是CallBack
delegateScripts.DoSomeThing();
}
private void callBack( int aa )
{
print(" aa = " + aa);
}