事件

定义事件数据

以服务端为例,在EventType中定义自己事件所需要的数据类型

image-20230724180454253

public struct TestEvent
{
    public string name;
    public int age;
}

定义事件行为

image-20230724180554483

public class TestEvent: AEvent<EventType.TestEvent>
{
    protected override void Run(EventType.TestEvent a)
    {
        Log.Error($"{a.name} {a.age}");
    }
}

派发

派发就很简单了

Game.EventSystem.Publish(new EventType.TestEvent() { name = "xccc", age = 25 });

image-20230724180742949

异步事件

对上述步骤做出如下修改

public class TestEvent: AEventAsync<EventType.TestEvent>
{
    protected override async ETTask Run(EventType.TestEvent a)
    {
        Log.Debug($"xccc,{TimeHelper.ClientNowSeconds()}");
        await TimerComponent.Instance.WaitAsync(2000);
        Log.Debug($"xccc,{TimeHelper.ClientNowSeconds()}");
    }
}

派发的地方也要修改

Game.EventSystem.PublishAsync(new EventType.TestEvent() { name = "xccc", age = 25 }).Coroutine();

然后在日志里可以看到相应的日志

2023-07-24 19:14:20.6863 (TestEvent.cs:7) xccc,1690197260 2023-07-24 19:14:22.6991 (TestEvent.cs:9) xccc,1690197262

可以发现时间确实延后了2秒

ETTask 异步编程

略,参考C#与异步编程