定义类似枚举---
using UnityEngine;
using System.Collections;
namespace DelegateTools
{
public delegate void VoidDelegate();// 传方法
public delegate void IdDelegate(long id);// 传带参数的方法(long id)
public delegate void StringDelegate(string text);// 传带参数的方法(string id)
public delegate void IntDelegate(int param, string owner);// 传带参数的方法(int param, string owner)
}
使用方法 ()
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using DelegateTools;
public class UITestParentItem : MonoBehaviour {
void SetValue()
{
UITestItem com = gameObject.GetComponent<UITestItem>();
com.SetValue("test",SetCallBack); // callback使用
}
void SetCallBack(int id)
{
Debug.Log(id); // id = 0
}
}
public class UITestItem : MonoBehaviour {
private DelegateTools.IdDelegate mFunc = null;
public UILabel _Lable ; // lable
int tId = 0;
// 监听按钮点击事件
void Awake()
{
UIEventListener button = gameObject.GetComponent<UIEventListener>();
UITools.AddOnclick(button,OnButtonClick);
}
// 按钮点击 - callback
public void OnButtonClick (GameObject go)
{
if(mFunc != null)
mFunc(tId);
}
// 设置数据 - 赋值callback
public void SetValue (string name,DelegateTools.IdDelegate func)
{
_Lable.text = name;
mFunc = func;
}
}