Unity事件系统
# 1.前言
Unity中事件/委托有着广泛的应用,本文通过封装一个简易的事件的系统,来统一管理消息的传递。此功能在简易应用或者事件较少的体现不出太好的作用,但是对于事件应用较多时,可以减少脚本之间的耦合。通过此事件系统架起不同脚本之间的桥梁,对于大量应用事件场景 中具有良好的效果。
# 2.事件系统
## 2.1 事件管理类
管理事件的注册与广播
```csharp
using System;
using System.Collections.Generic;
namespace EventManager
{
public delegate void ActionHandler(Message message);
public class EventManager
{
#region Instance
private static EventManager instance;
public static EventManager GetInstance()
{
if (instance == null)
{
instance = new EventManager();
}
return instance;
}
private EventManager() { }
#endregion
private Dictionary<string, ActionHandler> actions = new Dictionary<string, ActionHandler>();
public void AddListener(string actionKey,ActionHandler action)
{
ActionHandler handler;
bool exist = actions.TryGetValue(actionKey, out handler);
if (exist)
{
//避免重复添加
Delegate[] delegates = handler.GetInvocationList();
if (Array.IndexOf(delegates, action) == -1)
{
handler += action;
actions[actionKey] = handler;
}
}
else
{
actions.Add(actionKey, action);
}
}
public void RemoveListener(string actionKey,ActionHandler action)
{
ActionHandler handler;
bool exist = actions.TryGetValue(actionKey, out handler);
if (exist)
{
handler -= action;
if (handler == null)
{
actions.Remove(actionKey);
}
else
{
actions[actionKey] = handler;
}
}
}
public bool BroadcastMessage(string actionKey, Message message)
{
ActionHandler handler;
bool exist = actions.TryGetValue(actionKey, out handler);
if (exist)
{
handler(message);
return true;
}
else
{
return false;
}
}
}
}
```
## 2.2 消息类
通过统一的消息类来传递参数,避免泛型编程时需要考虑的多种情况。如下所示Message类,此类的参数参考android的handler类参数。可以通过继承Message的新类来传递自定义参数。
```csharp
namespace EventManager
{
public class Message
{
public int arg1;
public int arg2;
public string message;
public Message(int arg1, int arg2, string message)
{
this.arg1 = arg1;
this.arg2 = arg2;
this.message = message;
}
public Message() { }
}
}
```
## 2.3 事件标记
在EventManager中 注册事件时,通过string类型的参数来标记需要广播事件。此处可以改为枚举类型,后续可以优化。
```csharp
namespace EventManager
{
public class EventType
{
public readonly static string EVENTTYPE_METHOD1 = "EventType_Method1";
}
}
```
# 3.应用案例
```csharp
using MSG;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class EventManagerTest : MonoBehaviour
{
public Button button;
private void Start()
{
button.onClick.AddListener(() =>
{
EventManager.EventManager.GetInstance().BroadcastMessage(EventManager.EventType.EVENTTYPE_METHOD1, new EventManager.Message());
});
}
void OnEnable ()
{
EventManager.EventManager.GetInstance().AddListener(EventManager.EventType.EVENTTYPE_METHOD1, Method);
}
private void OnDisable()
{
EventManager.EventManager.GetInstance().RemoveListener(EventManager.EventType.EVENTTYPE_METHOD1, Method);
}
void Method(EventManager.Message message)
{
Debug.Log("Method : Message -{0}- obtained from channel {1}");
}
void Method1(Message message)
{
Debug.LogFormat("Method1 : Message -{0}- obtained from channel {1}", message.arg1, message.what);
}
void Method2(Message message)
{
NetworkMessage networkMessage = message as NetworkMessage;
if(networkMessage != null)
{
Debug.LogFormat("NetworkMessage obtained from channel {0}", message.what);
}
}
}
```
Unity事件系统的更多相关文章
- Unity事件系统EventSystem简析
相关组件和类 EventSystem 1.负责InputModule的切换(因为现在游戏大部分都只有一个StanaloneInputModule,所以切换这部分可以先不考虑). 2.负责InputMo ...
- SteamVR Unity Plugin - v2.0.1中的InteractionSystem
最近写VR项目的时候用到了SteamVR Unity Plugin - v2.0.1插件,感觉比之前用到的SteamVR plugin for Unity - v1.2.2版本改进了很多,就算不用VR ...
- [Unity3D] 04 - Event Manager
message消息管理 脚本与GameObject的关系 被显式添加到 Hierarchy 中的 GameObject 会被最先实例化,GameObject 被实例化的顺序是从下往上. GameObj ...
- Unity 之事件系统
游戏开发过程中事件是非常多的,可以通过 Messenger 事件系统来解藕,用法如下: 使用方法 例子:在按下拍照按钮后通知刷新好友面板 步骤1.添加事件字段,该字段具有唯一性 在MessangerE ...
- 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中定义: "在对象之间定义一个一对多的连接方法,当一个对象变换状态时,其他关联的对象都会自动收到通知." 现实中,社交网络就是个例子. 以前的报社,每次出新刊的时候, 报刊便 ...
随机推荐
- Vue.js+vue-element搭建属于自己的后台管理模板:Vue.js快速入门(二)
Vue.js+vue-element搭建属于自己的后台管理模板:Vue.js快速入门(二) 前言 上篇文章对Vue.js有了初步理解,接下来我们把Vue.js基础语法快速的过一遍,先混个脸熟留个印象就 ...
- Spring boot 梳理 - 模版引擎 -freemarker
开发环境中关闭缓存 spring: thymeleaf: cache: false freemarker: cache: false Spring boot 集成 freemarker <dep ...
- Spring 梳理 - AOP那些学术概念—通知、增强处理连接点(JoinPoint)切面(Aspect)
Spring AOP那些学术概念—通知.增强处理连接点(JoinPoint)切面(Aspect) 1.我所知道的AOP 初看起来,上来就是一大堆的术语,而且还有个拉风的名字,面向切面编程,都说是 ...
- 读《深入理解Elasticsearch》点滴-查询模版(结合官网手册,版本5.1)
1.为什么使用查询模版 让应用程序开发者只需要把查询传递给elasticsearch,而不需要考虑查询语句的构造.查询DSL语法.查询结果过滤等细节知识. 2.使用版本5.1,查询模版在5.6中发生变 ...
- 使用servlet+jdbc+MD5实现用户加密登录
/** * 分析流程: * 1.前端页面提交登录请求 * 2.被web.xml拦截,进入到LoginServlet(有两种方式:方式一,在web.xml文件中配置servlet拦截器;方式二,不用在w ...
- linux常用开发命令总结
linux常用命令 文件操作命令 1. cd 目录名/目录名 切换目录 cd .. 切换到上一级目录 (change dictionary) Ctrl+C强制退出命令行,回到上一级 2.ls ...
- 是男人就过八题A_A String Game题解
题意 给一个字符串\(s\),和\(n\)个子串\(t[i]\),两个人博弈,每次取出一个串\(t[i]\),在后面加入一个字符,保证新字符串仍然是\(s\)的子串,无法操作的人输. 分析 n个子串, ...
- 地图的折线:Polyline
(1)var polyline = new BMap.Polyline([new BMap.Point(X1,Y1),new BMap.Point(X2,Y2),new BMap.Point(X3,Y ...
- Mysql高手系列 - 第20篇:异常捕获及处理详解(实战经验)
Mysql系列的目标是:通过这个系列从入门到全面掌握一个高级开发所需要的全部技能. 这是Mysql系列第20篇. 环境:mysql5.7.25,cmd命令中进行演示. 代码中被[]包含的表示可选,|符 ...
- oracle之新建用户与授权
1.登录,口令为Oracle12c 2.新建用户 3.口令自己设置 4.按下图给角色授权,点击用用 5.登录刚刚创建的用户