Unity-通过Action和Func来注册事件(event)

                     Unity-通过Action和Func来注册事件(event)


目录

1.博文介绍

2.Action And Func<>

3.具体用法

4.资源下载

5.结语


1.博文介绍

利用Action 和 Func<>可以简化事件的声明过程,本篇文章只介绍通过Action 和 Func<>来注册事件。


2.Action And Func<>

public delegate void MyDelegate();

public event MyDelegate myEvent;

上述两个式子可以简写为:public event Action myEvent;

 

Action:表示没有参数无返回值的委托

Action<>:表示带参数的委托

Func:表示带返回值的委托

Func:表示带参数带返回值的委托


3.具体用法

直接撸代码了,大家看代码,清晰明了

TestOne:脚本1声明了事件

using System;
using UnityEngine;

/// 
/// Action and Func<>
/// Date:2018/11/4
/// Author:Mr.Sun
/// 
public class TestOne : MonoBehaviour {

	#region Action 表式无参数无返回值

	/// 
	///这个式子等同于
	/// public delegate void Mydelegate();
	/// publid static event Mydelegate myEvent;
	/// 
	public static event Action myEvent;

	
	#endregion
	
	#region Action<> 带参数

	/// 
	///这个式子等同于
	/// public delegate void Mydelegate(string str);
	/// publid static event Mydelegate myEventTwo;
	/// 
	public static event Action myEventTwo;

	/// 
	///这个式子等同于
	/// public delegate void Mydelegate(string str,int num,bool isFuc);
	/// publid static event Mydelegate myEventThree;
	/// 
	public static event Action myEventThree;
	
	#endregion

	#region Func 带返回值,无参数

	/// 
	///这个式子等同于
	/// public delegate string Mydelegate();
	/// publid static event Mydelegate myEventFour;
	public static event Func myEventFour; 

	#endregion

	#region Func string参数 int返回值

	/// 
	///这个式子等同于
	/// public delegate string Mydelegate(int num);
	/// publid static event Mydelegate myEventFive;
	public static event Func myEventFive; 
	
	/// 
	///这个式子等同于
	/// public delegate string Mydelegate(int num,bool isFuc);
	/// publid static event Mydelegate myEventFive;
	public static event Func myEventSix; 

	#endregion
	
	// Update is called once per frame
	void Update () {

		if (Input.GetKeyDown(KeyCode.A))
		{
			if (myEvent!=null)
			{
				myEvent();
			}
			
			if (myEventTwo!=null)
			{
				myEventTwo("Action");
			}
			
			if (myEventThree!=null)
			{
				myEventThree("Action",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;


/// 
/// Action and Func<>
/// Date:2018/10/13
/// Author:Mr.Sun
/// 
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 FucFive(int num)
	{
		print("参数:"+num);
		return "返回值:【Func】";
	}
	
	
	String FucSix(int num,bool isFuc)
	{
		print("参数1:"+num+"\n参数2:"+isFuc);
		return "返回值:【Func】";
	}
	
}

非常清晰明了,不用做解释了。


4.资源下载

本文源码:

https://download.csdn.net/download/mr_sun88/10762569


5.结语

       希望各位同学看过后能有所收获,另博主能力有限,文中若有错误的地方期望各位看家可以指点交流。

       QQ交流群:806091680(Chinar)

       该群为CSDN博主Chinar所创,推荐一下!我也在群里!

       本文属于原创文章,转载请著名作者出处!!!!!

你可能感兴趣的:(Unity,委托,事件,Action,Func)