【Unity 框架】QFramework v1.0 使用指南 工具篇:13. 其他事件工具 | Unity 游戏框架 | Unity 游戏开发 | Unity 独立游戏

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

EnumEventSystem

EnumEventSystem 前身是 老版本 QFramework 的 QEventSystem

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

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

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。

更多内容

猜你喜欢

转载自blog.csdn.net/u010125551/article/details/127357767