﻿# 13. 其他事件工具

QFramework 除了支持了  TypeEventSystem、EasyEvent 还支持了 EnumEventSystem、StringEventSystem。


## EnumEventSystem

EnumEventSystem 前身是 老版本 QFramework 的 QEventSystem

``` csharp
using UnityEngine;

namespace QFramework
{
	public class EnumEventExample : MonoBehaviour
	{
		#region 事件定义

		public enum TestEvent
		{
			Start,
			TestOne,
			End,
		}

		public enum TestEventB
		{
			Start = TestEvent.End, // 为了保证每个消息 Id 唯一，需要头尾相接
			TestB,
			End,
		}

		#endregion 事件定义
		
		void Start()
		{
			EnumEventSystem.Global.Register(TestEvent.TestOne, OnEvent);
		}

		void OnEvent(int key, params object[] obj)
		{
			switch (key)
			{
				case (int) TestEvent.TestOne:
					Debug.Log(obj[0]);
					break;
			}
		}

		private void Update()
		{
			if (Input.GetMouseButtonDown(0))
			{
				EnumEventSystem.Global.Send(TestEvent.TestOne, "Hello World!");
			}
		}

		private void OnDestroy()
		{
			EnumEventSystem.Global.UnRegister(TestEvent.TestOne, OnEvent);
		}
	}
}
```


## StringEventSystem

StringEventSystem 的前身是，老版本的 MsgDispatcher

``` csharp
using UnityEngine;

namespace QFramework
{
	public class EnumEventExample : MonoBehaviour
	{
		#region 事件定义

		public enum TestEvent
		{
			Start,
			TestOne,
			End,
		}

		public enum TestEventB
		{
			Start = TestEvent.End, // 为了保证每个消息 Id 唯一，需要头尾相接
			TestB,
			End,
		}

		#endregion 事件定义
		
		void Start()
		{
			EnumEventSystem.Global.Register(TestEvent.TestOne, OnEvent);
		}

		void OnEvent(int key, params object[] obj)
		{
			switch (key)
			{
				case (int) TestEvent.TestOne:
					Debug.Log(obj[0]);
					break;
			}
		}

		private void Update()
		{
			if (Input.GetMouseButtonDown(0))
			{
				EnumEventSystem.Global.Send(TestEvent.TestOne, "Hello World!");
			}
		}

		private void OnDestroy()
		{
			EnumEventSystem.Global.UnRegister(TestEvent.TestOne, OnEvent);
		}
	}
}
// 输出结果
// 点击鼠标左键
// Hello World
```


## StringEventSystem

``` csharp
using UnityEngine;

namespace QFramework.Example
{
    public class StringEventSystemExample : MonoBehaviour
    {
        void Start()
        {
            StringEventSystem.Global.Register("TEST_ONE", () =>
            {
                Debug.Log("TEST_ONE");
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
            
            // 事件 + 参数
            StringEventSystem.Global.Register<int>("TEST_TWO", (count) =>
            {
                Debug.Log("TEST_TWO:" + count);

            }).UnRegisterWhenGameObjectDestroyed(gameObject);
        }

        private void Update()
        {
            if (Input.GetMouseButtonDown(0))
            {
                StringEventSystem.Global.Send("TEST_ONE");
                StringEventSystem.Global.Send("TEST_TWO",10);
                
            }
        }
    }
}

// 输出结果
// 点击鼠标左键
// TEST_ONE
// TEST_TWO:10

```




## 对比

* TypeEventSystem：
  * 事件体定义简洁
  * 比较适合用于设计框架
  * 支持 struct 获得较好内存性能
  * 使用反射，CPU 性能相对比较差

* EasyEvent
  * 方便、易用、开发效率高
  * CPU 性能、内存性能较好，接近委托
  * 功能有限
  * 比较适合设计通用解决工具，比如通用背包、全局生命周期触发等
  * StringEventSystem、TypeEventSystem 的底层由 EasyEvent 实现

* EnumEventSystem
  * 使用枚举作为事件 id，比较适合和服务端的 protobuf 或带有消息 id 的长链接通信
  * 性能较好
  * 枚举用于定义消息体有维护成本

* StringEventSystem
  * 使用字符串作为事件 id，比较适合和其他脚本层通信，比如 Lua、ILRuntime、PlayMaker 等。
  * 性能一般


目前官方推荐使用 TypeEventSystem 和 EasyEvent 这两个工具。

如果要和网络通信则选择用 EnumEventSystem。

如果要和其他脚本层通信选择用 StringEventSystem。

## 更多内容

*   转载请注明地址：[liangxiegame.com](https://liangxiegame.com) （首发） 微信公众号：凉鞋的笔记
*   QFramework 主页：[qframework.cn](https://qframework.cn)
*   QFramework 交流群: 623597263
*   QFramework Github 地址: [https://github.com/liangxiegame/qframework](https://github.com/liangxiegame/qframework)
*   QFramework Gitee 地址：[https://gitee.com/liangxiegame/QFramework](https://gitee.com/liangxiegame/QFramework)
*   GamePix 独立游戏学院 & Unity 进阶小班地址：[https://www.gamepixedu.com/](https://www.gamepixedu.com/)
    
    
    
        