Unity-通过Action和Func来注册事件(event)
目录
1.博文介绍
利用Action 和 Func<>可以简化事件的声明过程,本篇文章只介绍通过Action 和 Func<>来注册事件。
2.Action And Func<>
public delegate void MyDelegate();
public event MyDelegate myEvent;
上述两个式子可以简写为:public event Action myEvent;
Action:表示没有参数无返回值的委托
Action<>:表示带参数的委托
Func<T>:表示带返回值的委托
Func<T,DT>:表示带参数带返回值的委托
3.具体用法
直接撸代码了,大家看代码,清晰明了
TestOne:脚本1声明了事件
using System;
using UnityEngine;
/// <summary>
/// Action and Func<>
/// Date:2018/11/4
/// Author:Mr.Sun
/// </summary>
public class TestOne : MonoBehaviour {
#region Action 表式无参数无返回值
/// <summary>
///这个式子等同于
/// public delegate void Mydelegate();
/// publid static event Mydelegate myEvent;
/// </summary>
public static event Action myEvent;
#endregion
#region Action<> 带参数
/// <summary>
///这个式子等同于
/// public delegate void Mydelegate(string str);
/// publid static event Mydelegate myEventTwo;
/// </summary>
public static event Action<String> myEventTwo;
/// <summary>
///这个式子等同于
/// public delegate void Mydelegate(string str,int num,bool isFuc);
/// publid static event Mydelegate myEventThree;
/// </summary>
public static event Action<String,int,bool> myEventThree;
#endregion
#region Func<string> 带返回值,无参数
/// <summary>
///这个式子等同于
/// public delegate string Mydelegate();
/// publid static event Mydelegate myEventFour;
public static event Func<string> myEventFour;
#endregion
#region Func<string,int> string参数 int返回值
/// <summary>
///这个式子等同于
/// public delegate string Mydelegate(int num);
/// publid static event Mydelegate myEventFive;
public static event Func<int,string> myEventFive;
/// <summary>
///这个式子等同于
/// public delegate string Mydelegate(int num,bool isFuc);
/// publid static event Mydelegate myEventFive;
public static event Func<int,bool,string> myEventSix;
#endregion
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.A))
{
if (myEvent!=null)
{
myEvent();
}
if (myEventTwo!=null)
{
myEventTwo("Action<String>");
}
if (myEventThree!=null)
{
myEventThree("Action<String,int.bool>",10,true);
}
if (myEventFour!=null)
{
print(myEventFour());
}
if (myEventFive!=null)
{
print(myEventFive(1));
}
if (myEventSix!=null)
{
print(myEventSix(10,true));
}
}
}
}
TestTwo:脚本2绑定了方法
using System;
using UnityEngine;
/// <summary>
/// Action and Func<>
/// Date:2018/10/13
/// Author:Mr.Sun
/// </summary>
public class TestTwo : MonoBehaviour {
// Use this for initialization
void Start ()
{
TestOne.myEvent += FucOne;
TestOne.myEventTwo += FucTwo;
TestOne.myEventThree += FucThree;
TestOne.myEventFour += FucFour;
TestOne.myEventFive += FucFive;
TestOne.myEventSix += FucSix;
}
void FucOne()
{
print("【Action】:无参数,无返回值");
}
void FucTwo(string str)
{
print("参数1:"+str);
}
void FucThree(string str,int num,bool isFuc)
{
print("参数1:"+str+"\n参数2:"+num+"\n参数3:"+isFuc);
}
string FucFour()
{
return "【Func<String>】";
}
String FucFive(int num)
{
print("参数:"+num);
return "返回值:【Func<int,string>】";
}
String FucSix(int num,bool isFuc)
{
print("参数1:"+num+"\n参数2:"+isFuc);
return "返回值:【Func<int,bool,string>】";
}
}
非常清晰明了,不用做解释了。
4.资源下载
本文源码:
https://download.csdn.net/download/mr_sun88/10762569
5.结语
希望各位同学看过后能有所收获,另博主能力有限,文中若有错误的地方期望各位看家可以指点交流。
QQ交流群:806091680(Chinar)
该群为CSDN博主Chinar所创,推荐一下!我也在群里!
本文属于原创文章,转载请著名作者出处!!!!!