您的位置:首页 > 移动开发 > Unity3D

(二十四)unity4.6学习Ugui中文文档-------在unity4.6之前使用UnityEvent

2015-03-08 18:44 447 查看
孙广东:2015-3-8/18:43 转载请注明出处:http://blog.csdn.net/u010019717
更全的内容请看我的游戏蛮牛地址:http://www.unitymanual.com/space-uid-18602.html

在unity4.6之后多了个这个东东
当然应该在UI中有应用了那么还有其他别的用处么?
我们来看看官方蛮牛对UnityEvents的介绍:
UnityEvents 是允许用户从编辑时驱动回调的方式来保存,运行时无需额外的编程和脚本配置。
UnityEvents 在很多事情上很有用:
•内容驱动的回调Content driven callbacks
•解耦系统Decoupling systems
•持续回调Persistent callbacks
•预配置的调用事件Preconfigured call events
UnityEvents 可以添加到任何的MonoBehaviour类中和被执行。
他类似.net标准的delegate委托,怎么知道的呢?1、看源代码呀,2、按如下图搜索:



点击进入:





当UnityEvent添加到MonoBehaviour上,它出现在检查器,可以添加持续回调。
使用 UnityEvents
要配置一个回调在编辑器中有几个步骤:
1.请确保您的脚本导入/使用using UnityEngine.Events.
2.选择 + 图标以添加一个回调方法的槽
3.选择UnityEngine.Object您希望接收的回调 (您可以使用为这个对象选择器)
4.选择您希望调用的函数
5.您可以添加更多关于一个事件的回调
在Inspector 检查器中配置的UnityEvent时有两种类型的受支持的函数调用:
•Static静态。静态调用是预配置的调用,在 UI 中设置的预配置值。这意味着当调用回调时,使用已输入到 UI 的参数调用目标函数。
•Dynamic动态。使用代码,从发送的参数来调用动态调用,这绑定到 UnityEvent 正在调用的类型。UI 筛选回调,并只显示有效的 UnityEvent 的动态调用。
泛型 UnityEvents
默认情况下在Monobehaviour的UnityEvent将动态地绑定到 void 函数。这不必是这种情况的 UnityEvents 的动态调用支持绑定到带有达 4 参数的函数。要做到这一点,您需要定义一个自定义的UnityEvent类,支持多个参数。
这是很容易做到:
[Serializable]
public class StringEvent : UnityEvent <string> {}


这个实例添加到您的类中,而不是base UnityEvent ,它将允许回调来将动态绑定到字符串函数。这然后可以通过调用Invoke()函数使用一个string作为参数调用。

UnityEvents 可以用达 4 参数在其泛型定义中定义。
例子:



1、UnityEngine.Events Classes
UnityEventBase 类:
Namespace: UnityEngine.Events
描述:UnityEvents 的抽象基类。
此类为 UnityEvents 提供的基本功能。
UnityEvent 类:
Namespace: UnityEngine.Events/Inherits from: Events.UnityEventBase
描述:零参数持续回调可以被现场保存。
例子:

using UnityEngine;
using System.Collections;
using UnityEngine.Events;
public class Event0 : MonoBehaviour {
	public UnityEvent m_MyEvent;    // 注意这个一定要是public才能应用在Inspector上通过+-号操作
	// Use this for initialization
	void Start () {
		if (m_MyEvent == null)
			m_MyEvent = new UnityEvent ();
		
		m_MyEvent.AddListener (Ping);
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.anyKeyDown && m_MyEvent != null)
		{
			m_MyEvent.Invoke ();
		}
	}
	public void Ping ()
	{
		Debug.Log ("Ping");
	}
}


UnityEvent<T0> 类:
Namespace: UnityEngine.Events/Inherits from: Events.UnityEventBase
描述:一个参数的 UnityEvent 版本。
如果您想要使用泛型的 UnityEvent 类型,您必须重写的类类型。
例子:
using UnityEngine;
using System.Collections;
using UnityEngine.Events;

public class MyIntEvent : UnityEvent<int> {}
public class Event1 : MonoBehaviour {

	public MyIntEvent m_MyEvent;
	// Use this for initialization
	void Start () {
		if (m_MyEvent == null)
			m_MyEvent = new MyIntEvent ();
		
		m_MyEvent.AddListener (Ping);
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.anyKeyDown && m_MyEvent != null)
		{
			m_MyEvent.Invoke (5);
		}
	}
	void Ping ( int i)
	{
		Debug.Log ("Ping" + i);
	}
}


UnityEvent<T0,T1> 类 :
Namespace: UnityEngine.Events/Inherits from: Events.UnityEventBase
描述:两个参数的 UnityEvent 版本。
如果您想要使用泛型的 UnityEvent 类型,您必须重写的类类型。
例子:
using UnityEngine;
using System.Collections;
using UnityEngine.Events;

public class My2ArgEvent : UnityEvent<int, int> {}
public class Event2 : MonoBehaviour {
	public My2ArgEvent m_MyEvent;
	// Use this for initialization
	void Start () {
		if (m_MyEvent == null)
			m_MyEvent = new My2ArgEvent ();
		
		m_MyEvent.AddListener (Ping);
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.anyKeyDown && m_MyEvent != null)
		{
			m_MyEvent.Invoke (5, 6);
		}
	}
	void Ping (int i0, int i1)
	{
		Debug.Log (i0 + i1);
	}
}


UnityEvent<T0,T1,T2> 类:
Namespace: UnityEngine.Events/Inherits from: Events.UnityEventBase
描述:3 个参数的 UnityEvent 版本。
如果您想要使用泛型的 UnityEvent 类型,您必须重写的类类型。
例子:
using UnityEngine;
using System.Collections;
using UnityEngine.Events;

public class My3ArgEvent : UnityEvent<int, int, int> {}
public class Event3 : MonoBehaviour 
{
	public My3ArgEvent m_MyEvent;
	// Use this for initialization
	void Start () {
		if (m_MyEvent == null)
			m_MyEvent = new My3ArgEvent ();
		
		m_MyEvent.AddListener (Ping);
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.anyKeyDown && m_MyEvent != null)
		{
			m_MyEvent.Invoke (5, 6, 7);
		}
	}
	void Ping (int i0,int i1,int i2)
	{
		Debug.Log (i0 + i1 + i2);
	}
}


UnityEvent<T0,T1,T2,T3> 类:
Namespace: UnityEngine.Events/Inherits from: Events.UnityEventBase
描述:4 个参数的 UnityEvent 版本。
如果您想要使用泛型的 UnityEvent 类型,您必须重写的类类型。
例子:

using System.Collections;
using UnityEngine.Events;

public class My4ArgEvent : UnityEvent<int, int, int, int> {}
public class Event4 : MonoBehaviour 
{
	public My4ArgEvent m_MyEvent;
	// Use this for initialization
	void Start () {
		if (m_MyEvent == null)
			m_MyEvent = new My4ArgEvent ();
		
		m_MyEvent.AddListener (Ping);
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.anyKeyDown && m_MyEvent != null)
		{
			m_MyEvent.Invoke (5, 6, 7, -5);
		}
	}
	void Ping (int i0,int i1,int i2,int i3)
	{
		Debug.Log (i0 + i1 + i2 + i3);
	}
}


2 Uni tyEngine.Events Enumerations
PersistentListenerMode
描述:侦听器的运作模式。
UnityEventCallState
描述:控制 UnityEvent 回调的范围。

知道了它的用法,那么回到标题吧,基本上现在的项目都还是在使用NGUI所以不会使用unity4.6版本。
但是又不想错过unity原生的好东西。 怎么办?那就得到源代码吧。
本人测试后是没有问题的!
下载地址如下:http://download.csdn.net/detail/u010019717/8483193
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: