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中定义: "在对象之间定义一个一对多的连接方法,当一个对象变换状态时,其他关联的对象都会自动收到通知." 现实中,社交网络就是个例子. 以前的报社,每次出新刊的时候, 报刊便 ...
随机推荐
- STL lower_bound upper_bound 用法
1.lower_bound(begin,end,x) 返回第一个>=x的位置,找不到return .end() 2.upper_bound (begin,end,x) 返回第一个>x的位置 ...
- html 视频播放器
html 视频播放器 <html> <script> /** *视频播放 *参数说明 u - 媒体URL w - 媒体宽度width h - 媒体高度height */ // ...
- Openstack组件实现原理 — Nova 体系结构
目录 目录 前文列表 Nova体系结构 虚拟机实例化流程 前文列表 Openstack组件部署 - Overview和前期环境准备 Openstack组建部署 - Environment of Con ...
- Java学习之集合(Collection接口)
集合类的由来: 对象用于封装特有数据,对象多了需要存储,如果对象的个数不确定,就使用集合容器进行存储 集合特点: 1.用于存储对象的容器 2.集合长度可变 3.不可以存储基本数据类型 集合体系的顶层C ...
- VIM编辑器进阶配置
vim自定义设置 可以选择需要的功能添加至 ~/.vimrc 打开注释使之生效. " 让 vim 关闭所有扩展的功能,尽量模拟 vi 的行为. set nocompatible ...
- docker 详细安装及问题排查
一.安装docker 1.Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker . 通过 uname -r 命令查看 ...
- hdu6395 /// 分块矩阵快速幂
题目大意: F(1)=A, F(2)=B, F(i)=C*F(i-2)+D*F(i-1)+p/i(向下取整) 给定A B C D p n 求F(n) 构造 矩阵A * 矩阵B = ...
- 【转】从SOA到微服务,企业分布式应用架构在云原生时代如何重塑
摘要: SOA 采用中心化的服务总线架构,解耦了业务逻辑和服务治理逻辑:微服务架构回归了去中心化的点对点调用方式,在提升敏捷性和可伸缩性的同时,也牺牲了业务逻辑和服务治理逻辑解耦所带来的灵活性. 为了 ...
- Flyway - Version control for your database
Flyway 是什么? Flyway是个数据库版本管理工具.在开发过程中,数据库难免发生变更,例如数据变更,表结构变更.新建表或者视图等等. 在项目进行时无法保证一旦开发环境中的数据库内容变化候会去测 ...
- 淘宝内核月报2014-11-draft
内核月报2014-11-draft MySQL· 捉虫动态·OPTIMIZE 不存在的表 bug 描述 这是一个和 GTID 相关的Bug,也就是说5.6才会有,并且出现这个 bug 需要满足条件: ...