C# 传方法function / 传带参数的方法function

来源:互联网 发布:java 接口文档编写 编辑:程序博客网 时间:2024/06/10 22:20

定义类似枚举---

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 ; // lableint tId = 0;// 监听按钮点击事件void Awake(){UIEventListener button = gameObject.GetComponent<UIEventListener>();UITools.AddOnclick(button,OnButtonClick);}// 按钮点击 - callbackpublic void OnButtonClick (GameObject go){if(mFunc != null)mFunc(tId);}// 设置数据 - 赋值callbackpublic void SetValue (string name,DelegateTools.IdDelegate func){_Lable.text = name;mFunc = func;}}


0 0