Unity 之事件系统
游戏开发过程中事件是非常多的,可以通过 Messenger 事件系统来解藕,用法如下:
使用方法 例子:在按下拍照按钮后通知刷新好友面板
步骤1、添加事件字段,该字段具有唯一性
在MessangerEventDef脚本(可以每个功能都有一个事件字段脚本,类似于消息)中添加字段"Demo_EventType". 步骤2、广播事件:在按下拍照按钮方法中调用
Messenger.Broadcast(MessangerEventDef.Demo_EventType); 步骤3、在OnEnable()方法中注册事件:在FriendUI_Ctrl中的OnEnable方法中添加
Messenger.AddListener(MessangerEventDef.Demo_EventType, OnCall); 步骤4、在OnDisable()方法中移除事件:在FriendUI_Ctrl中的OnDisable方法中添加
Messenger.RemoveListener(MessangerEventDef.Demo_EventType, OnCall); void OnCall()
{
Debug.LogError("===OnCall==");
//TODO 刷新好友相关代码
} 注意事项
、AddListener和RemoveListener必须成对出现。
代码如下:
// Messenger.cs v1.0 by Magnus Wolffelt, magnus.wolffelt@gmail.com
//
// Inspired by and based on Rod Hyde's Messenger:
// http://www.unifycommunity.com/wiki/index.php?title=CSharpMessenger
//
// This is a C# messenger (notification center). It uses delegates
// and generics to provide type-checked messaging between event producers and
// event consumers, without the need for producers or consumers to be aware of
// each other. The major improvement from Hyde's implementation is that
// there is more extensive error detection, preventing silent bugs.
//
// Usage example:
// Messenger<float>.AddListener("myEvent", MyEventHandler);
// ...
// Messenger<float>.Broadcast("myEvent", 1.0f); using System;
using System.Collections.Generic; namespace Common.Messenger
{
public enum MessengerMode : byte
{
DONT_REQUIRE_LISTENER,
REQUIRE_LISTENER,
} static internal class MessengerInternal
{
static public Dictionary<string, Delegate> eventTable = new Dictionary<string, Delegate>();
static public readonly MessengerMode DEFAULT_MODE = MessengerMode.DONT_REQUIRE_LISTENER; static public void OnListenerAdding(string eventType, Delegate listenerBeingAdded)
{
if (!eventTable.ContainsKey(eventType))
{
eventTable.Add(eventType, null);
} Delegate d = eventTable[eventType];
if (d != null && d.GetType() != listenerBeingAdded.GetType())
{
throw new ListenerException(string.Format("Attempting to add listener with inconsistent signature for event type {0}. Current listeners have type {1} and listener being added has type {2}", eventType, d.GetType().Name, listenerBeingAdded.GetType().Name));
}
} static public void OnListenerRemoving(string eventType, Delegate listenerBeingRemoved)
{
if (eventTable.ContainsKey(eventType))
{
Delegate d = eventTable[eventType]; if (d == null)
{
throw new ListenerException(string.Format("Attempting to remove listener with for event type {0} but current listener is null.", eventType));
}
else if (d.GetType() != listenerBeingRemoved.GetType())
{
throw new ListenerException(string.Format("Attempting to remove listener with inconsistent signature for event type {0}. Current listeners have type {1} and listener being removed has type {2}", eventType, d.GetType().Name, listenerBeingRemoved.GetType().Name));
}
}
else
{
throw new ListenerException(string.Format("Attempting to remove listener for type {0} but Messenger doesn't know about this event type.", eventType));
}
} static public void OnListenerRemoved(string eventType)
{
if (eventTable[eventType] == null)
{
eventTable.Remove(eventType);
}
} static public void OnBroadcasting(string eventType, MessengerMode mode)
{
if (mode == MessengerMode.REQUIRE_LISTENER && !eventTable.ContainsKey(eventType))
{
throw new MessengerInternal.BroadcastException(string.Format("Broadcasting message {0} but no listener found.", eventType));
}
} static public BroadcastException CreateBroadcastSignatureException(string eventType)
{
return new BroadcastException(string.Format("Broadcasting message {0} but listeners have a different signature than the broadcaster.", eventType));
} public class BroadcastException : Exception
{
public BroadcastException(string msg)
: base(msg)
{
}
} public class ListenerException : Exception
{
public ListenerException(string msg)
: base(msg)
{
}
}
} // No parameters
static public class Messenger
{
private static Dictionary<string, Delegate> eventTable = MessengerInternal.eventTable; static public void AddListener(string eventType, Callback handler)
{
MessengerInternal.OnListenerAdding(eventType, handler);
eventTable[eventType] = (Callback)eventTable[eventType] + handler;
} static public void RemoveListener(string eventType, Callback handler)
{
MessengerInternal.OnListenerRemoving(eventType, handler);
eventTable[eventType] = (Callback)eventTable[eventType] - handler;
MessengerInternal.OnListenerRemoved(eventType);
} static public void Broadcast(string eventType)
{
Broadcast(eventType, MessengerInternal.DEFAULT_MODE);
} static public void Broadcast(string eventType, MessengerMode mode)
{
MessengerInternal.OnBroadcasting(eventType, mode);
Delegate d;
if (eventTable.TryGetValue(eventType, out d))
{
Callback callback = d as Callback;
if (callback != null)
{
callback();
}
else
{
throw MessengerInternal.CreateBroadcastSignatureException(eventType);
}
}
}
} // One parameter
static public class Messenger<T>
{
private static Dictionary<string, Delegate> eventTable = MessengerInternal.eventTable; static public void AddListener(string eventType, Callback<T> handler)
{
MessengerInternal.OnListenerAdding(eventType, handler);
eventTable[eventType] = (Callback<T>)eventTable[eventType] + handler;
} static public void RemoveListener(string eventType, Callback<T> handler)
{
MessengerInternal.OnListenerRemoving(eventType, handler);
eventTable[eventType] = (Callback<T>)eventTable[eventType] - handler;
MessengerInternal.OnListenerRemoved(eventType);
} static public void Broadcast(string eventType, T arg1)
{
Broadcast(eventType, arg1, MessengerInternal.DEFAULT_MODE);
} static public void Broadcast(string eventType, T arg1, MessengerMode mode)
{
MessengerInternal.OnBroadcasting(eventType, mode);
Delegate d;
if (eventTable.TryGetValue(eventType, out d))
{
Callback<T> callback = d as Callback<T>;
if (callback != null)
{
callback(arg1);
}
else
{
throw MessengerInternal.CreateBroadcastSignatureException(eventType);
}
}
}
} // Two parameters
static public class Messenger<T, U>
{
private static Dictionary<string, Delegate> eventTable = MessengerInternal.eventTable; static public void AddListener(string eventType, Callback<T, U> handler)
{
MessengerInternal.OnListenerAdding(eventType, handler);
eventTable[eventType] = (Callback<T, U>)eventTable[eventType] + handler;
} static public void RemoveListener(string eventType, Callback<T, U> handler)
{
MessengerInternal.OnListenerRemoving(eventType, handler);
eventTable[eventType] = (Callback<T, U>)eventTable[eventType] - handler;
MessengerInternal.OnListenerRemoved(eventType);
} static public void Broadcast(string eventType, T arg1, U arg2)
{
Broadcast(eventType, arg1, arg2, MessengerInternal.DEFAULT_MODE);
} static public void Broadcast(string eventType, T arg1, U arg2, MessengerMode mode)
{
MessengerInternal.OnBroadcasting(eventType, mode);
Delegate d;
if (eventTable.TryGetValue(eventType, out d))
{
Callback<T, U> callback = d as Callback<T, U>;
if (callback != null)
{
callback(arg1, arg2);
}
else
{
throw MessengerInternal.CreateBroadcastSignatureException(eventType);
}
}
}
} // Three parameters
static public class Messenger<T, U, V>
{
private static Dictionary<string, Delegate> eventTable = MessengerInternal.eventTable; static public void AddListener(string eventType, Callback<T, U, V> handler)
{
MessengerInternal.OnListenerAdding(eventType, handler);
eventTable[eventType] = (Callback<T, U, V>)eventTable[eventType] + handler;
} static public void RemoveListener(string eventType, Callback<T, U, V> handler)
{
MessengerInternal.OnListenerRemoving(eventType, handler);
eventTable[eventType] = (Callback<T, U, V>)eventTable[eventType] - handler;
MessengerInternal.OnListenerRemoved(eventType);
} static public void Broadcast(string eventType, T arg1, U arg2, V arg3)
{
Broadcast(eventType, arg1, arg2, arg3, MessengerInternal.DEFAULT_MODE);
} static public void Broadcast(string eventType, T arg1, U arg2, V arg3, MessengerMode mode)
{
MessengerInternal.OnBroadcasting(eventType, mode);
Delegate d;
if (eventTable.TryGetValue(eventType, out d))
{
Callback<T, U, V> callback = d as Callback<T, U, V>;
if (callback != null)
{
callback(arg1, arg2, arg3);
}
else
{
throw MessengerInternal.CreateBroadcastSignatureException(eventType);
}
}
}
} // Four parameters
static public class Messenger<T, U, V, W>
{
private static Dictionary<string, Delegate> eventTable = MessengerInternal.eventTable; static public void AddListener(string eventType, Callback<T, U, V, W> handler)
{
MessengerInternal.OnListenerAdding(eventType, handler);
eventTable[eventType] = (Callback<T, U, V, W>)eventTable[eventType] + handler;
} static public void RemoveListener(string eventType, Callback<T, U, V, W> handler)
{
MessengerInternal.OnListenerRemoving(eventType, handler);
eventTable[eventType] = (Callback<T, U, V, W>)eventTable[eventType] - handler;
MessengerInternal.OnListenerRemoved(eventType);
} static public void Broadcast(string eventType, T arg1, U arg2, V arg3, W arg4)
{
Broadcast(eventType, arg1, arg2, arg3, arg4, MessengerInternal.DEFAULT_MODE);
} static public void Broadcast(string eventType, T arg1, U arg2, V arg3, W arg4, MessengerMode mode)
{
MessengerInternal.OnBroadcasting(eventType, mode);
Delegate d;
if (eventTable.TryGetValue(eventType, out d))
{
Callback<T, U, V, W> callback = d as Callback<T, U, V, W>;
if (callback != null)
{
callback(arg1, arg2, arg3, arg4);
}
else
{
throw MessengerInternal.CreateBroadcastSignatureException(eventType);
}
}
}
} // Five parameters
static public class Messenger<T, U, V, W, X>
{
private static Dictionary<string, Delegate> eventTable = MessengerInternal.eventTable; static public void AddListener(string eventType, Callback<T, U, V, W, X> handler)
{
MessengerInternal.OnListenerAdding(eventType, handler);
eventTable[eventType] = (Callback<T, U, V, W, X>)eventTable[eventType] + handler;
} static public void RemoveListener(string eventType, Callback<T, U, V, W, X> handler)
{
MessengerInternal.OnListenerRemoving(eventType, handler);
eventTable[eventType] = (Callback<T, U, V, W, X>)eventTable[eventType] - handler;
MessengerInternal.OnListenerRemoved(eventType);
} static public void Broadcast(string eventType, T arg1, U arg2, V arg3, W arg4, X arg5)
{
Broadcast(eventType, arg1, arg2, arg3, arg4, arg5, MessengerInternal.DEFAULT_MODE);
} static public void Broadcast(string eventType, T arg1, U arg2, V arg3, W arg4, X arg5, MessengerMode mode)
{
MessengerInternal.OnBroadcasting(eventType, mode);
Delegate d;
if (eventTable.TryGetValue(eventType, out d))
{
Callback<T, U, V, W, X> callback = d as Callback<T, U, V, W, X>;
if (callback != null)
{
callback(arg1, arg2, arg3, arg4, arg5);
}
else
{
throw MessengerInternal.CreateBroadcastSignatureException(eventType);
}
}
}
} // Six parameters
static public class Messenger<T, U, V, W, X, Y>
{
private static Dictionary<string, Delegate> eventTable = MessengerInternal.eventTable; static public void AddListener(string eventType, Callback<T, U, V, W, X, Y> handler)
{
MessengerInternal.OnListenerAdding(eventType, handler);
eventTable[eventType] = (Callback<T, U, V, W, X, Y>)eventTable[eventType] + handler;
} static public void RemoveListener(string eventType, Callback<T, U, V, W, X, Y> handler)
{
MessengerInternal.OnListenerRemoving(eventType, handler);
eventTable[eventType] = (Callback<T, U, V, W, X, Y>)eventTable[eventType] - handler;
MessengerInternal.OnListenerRemoved(eventType);
} static public void Broadcast(string eventType, T arg1, U arg2, V arg3, W arg4, X arg5, Y arg6)
{
Broadcast(eventType, arg1, arg2, arg3, arg4, arg5, arg6, MessengerInternal.DEFAULT_MODE);
} static public void Broadcast(string eventType, T arg1, U arg2, V arg3, W arg4, X arg5, Y arg6, MessengerMode mode)
{
MessengerInternal.OnBroadcasting(eventType, mode);
Delegate d;
if (eventTable.TryGetValue(eventType, out d))
{
Callback<T, U, V, W, X, Y> callback = d as Callback<T, U, V, W, X, Y>;
if (callback != null)
{
callback(arg1, arg2, arg3, arg4, arg5, arg6);
}
else
{
throw MessengerInternal.CreateBroadcastSignatureException(eventType);
}
}
}
}
}
Messenger
// MessengerUnitTest.cs v1.0 by Magnus Wolffelt, magnus.wolffelt@gmail.com
//
// Delegates used in Messenger.cs.
namespace Common.Messenger
{
public delegate void Callback();
public delegate void Callback<T>(T arg1);
public delegate void Callback<T, U>(T arg1, U arg2);
public delegate void Callback<T, U, V>(T arg1, U arg2, V arg3);
public delegate void Callback<T, U, V, W>(T arg1, U arg2, V arg3, W arg4);
public delegate void Callback<T, U, V, W, X>(T arg1, U arg2, V arg3, W arg4, X arg5);
public delegate void Callback<T, U, V, W, X, Y>(T arg1, U arg2, V arg3, W arg4, X arg5, Y arg6); public delegate T CallbackReturn<T>();
public delegate T CallbackReturn<T, U>(U arg1);
}
Callback
namespace Common.Messenger
{
/// <summary>
/// This class is used to define all eventId.
/// Event string must be different.
/// </summary>
public class MessengerEventDef
{ }
}
MessengerEventDef
转载请注明出处:https://www.cnblogs.com/jietian331/p/11003386.html
Unity 之事件系统的更多相关文章
- Unity3d项目入门之虚拟摇杆
Unity本身不提供摇杆的组件,开发者可以使用牛逼的EasyTouch插件或者应用NGUI实现相关的需求,下面本文通过Unity自身的UGUI属性,实现虚拟摇杆的功能. 主参考 <Unity:使 ...
- [Unity3D] 04 - Event Manager
message消息管理 脚本与GameObject的关系 被显式添加到 Hierarchy 中的 GameObject 会被最先实例化,GameObject 被实例化的顺序是从下往上. GameObj ...
- Unity事件系统
# 1.前言Unity中事件/委托有着广泛的应用,本文通过封装一个简易的事件的系统,来统一管理消息的传递.此功能在简易应用或者事件较少的体现不出太好的作用,但是对于事件应用较多时,可以减少脚本之间的耦 ...
- Unity事件系统EventSystem简析
相关组件和类 EventSystem 1.负责InputModule的切换(因为现在游戏大部分都只有一个StanaloneInputModule,所以切换这部分可以先不考虑). 2.负责InputMo ...
- Unity开发心路历程——制作画板
有人说 编程是份很无聊的工作 因为整个工作时间面对的都是电脑这种机器 因为眼睛盯着的内容都是索然无味的代码 因为总是会有意想不到的bug让你怀疑自己的智商 而我认为 编程是件及其有意思的事情 可观的收 ...
- Unity 4.6 uGUI的点击事件
因为Unity 4.6刚刚发布,自带的uGUI功能的相关资料还不是很完善,今天刚装的Unity 4.6,想看一下uGUI是否好用,那么开始就今天的学习吧啊! 1,新建一个空的工程.
- Unity GUI选择与评价
因为Unity内建的GUI不管是不是从开发效率或效率强制,因此,许多派生GUI插入,什么插件的选择,是一个非常值它被认为是. 既然是评价,就会有非常多的主观意识,这不一定成为选择的根据. 再比方.我已 ...
- Unity For Android Cardboard App ( 1 ):基础入门
作者: ericzwhuang 前言 目前Google官方推出的VR设备有DayDream(2016年推出)和Cardboard(2014年推出)两种. Daydream是消费级VR解决方案,提供了手 ...
- 【Unity与23种设计模式】观察者模式(Observer)
GoF中定义: "在对象之间定义一个一对多的连接方法,当一个对象变换状态时,其他关联的对象都会自动收到通知." 现实中,社交网络就是个例子. 以前的报社,每次出新刊的时候, 报刊便 ...
随机推荐
- 深入理解js——构造函数的继承
看了阮一峰的网络日志(http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_inheritance.html),记录一下构 ...
- Delphi 获取系统当前进程、窗口句柄、文件属性以及程序运行状态
uses TLHelp32,PsAPI;(1)显示进程列表: procedure TForm1.Button2Click(Sender: TObject); var lppe: TProcessEnt ...
- HTML-参考手册: Px、Em 换算工具
ylbtech-HTML-参考手册: Px.Em 换算工具 1.返回顶部 1. Px.Em 换算工具 以下工具提供了em和px的换算工具. 第一个输入框:设置了网页默认的字体像素 (通常 16px) ...
- 10、jqueryEasyUI感觉自己还是改行做前端吧
1.jquery的局部方法开发 //首先引入jquery的相关包组件 <script type="text/javascript" src="../js/jquer ...
- nginx填坑补充(nginx根据上下文跳转ip或者域名)
今天有一个需求,要根据上下文调到不同的ip或域名地址,使用上下文做域名跳转的时候,proxy_pass域名后面一定要带‘/’否则会把nginx的上下文自动带入,这样就行. location ^~ /d ...
- PAT_A1062#Talent and Virtue
Source: PAT A1062 Talent and Virtue (25 分) Description: About 900 years ago, a Chinese philosopher S ...
- springbot项目中使用继承
package com.example.demo.controller; import com.sun.org.apache.bcel.internal.generic.NEW; import org ...
- usb-host与外设通信(二)
本文是接着上一篇的的usb-host与外设通信(一)接着写的 3.枚举设备 当你的程序运行时,如果应用程序对当前连接的USB设备都感兴趣的,程序可以枚举所以当前的设备.使用getDeviceList( ...
- 浅谈fetch
在开发过程中,我们向服务端发送请求,一般会使用三种方式, XMLHttpRequest(XHR),jQuery实现的AJAX,Fetch ,让我们首先来比较一下这三者的使用示例. XMLHttpReq ...
- Web开发常规调试方法与常见问题分析
一.Web项目基本原理 现在的web项目大都已经前后端独立开发与部署. 前后端独立开发,一般是前端与后端通过web接口(常见的有RESTful与websocket)文档进行交流.前端开发人员先更具业务 ...