QFramework 除了支持了 TypeEventSystem、EasyEvent 还支持了 EnumEventSystem、StringEventSystem。
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 的前身是,老版本的 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
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:
EasyEvent
EnumEventSystem
StringEventSystem
目前官方推荐使用 TypeEventSystem 和 EasyEvent 这两个工具。
如果要和网络通信则选择用 EnumEventSystem。
如果要和其他脚本层通信选择用 StringEventSystem。