Skip to content

Event System

Introduction

The event system is an important functional component in the ET framework, providing a complete event management and distribution mechanism. The main features include:

  • Scene-based event isolation
  • Async event support
  • Automatic registration mechanism

Core Concepts

Event Definition

Events in the ET framework are implemented by defining structs. Each event structure can contain data that needs to be passed.

csharp
namespace MH
{
    public struct Main_Init
    {
    }
    public struct Main_Init2
    {
        public long Id;
    }
}

Scene Isolation

Events in the ET framework are isolated based on scenes, mainly divided into the following scene types:

  • Main: Main scene, typically used for handling global events
  • Current: Current scene, handles events in specific game scenes

Event Distribution

Event distribution is managed uniformly through the EventSystem:

csharp
// Trigger event
EventSystem.Instance.Publish(Root, new Main_Init());
EventSystem.Instance.Publish(Root, new Main_Init2()
{
    Id = 1
});

// Async event
EventSystem.Instance.PublishAsync(Root, new Main_Init()).Coroutine();

await EventSystem.Instance.PublishAsync(Root, new Main_Init());

Event Handler Classes

Event handler classes are defined through attributes, and each event needs to specify its scene type:

csharp

namespace MH
{
    [Event(SceneType.Main)]
    public class MainInit_EventView : AEvent<Scene, Main_Init>
    {
        protected override async ETTask Run(Scene scene, Main_Init a)
        {
            scene.AddComponent<GlobalComponent>();
            scene.AddComponent<CoroutineLockComponent>();
            scene.AddComponent<ResourcesComponent>();
            scene.AddComponent<UIPathComponent>();
            scene.AddComponent<UIEventComponent>();
            scene.AddComponent<CurrentSceneComponent>();
            var uiComponent = scene.AddComponent<UIComponent>();

            scene.AddComponent<UnitComponent>();
            scene.AddComponent<AudioComponent>();
            scene.AddComponent<LocalizationComponent>();
            uiComponent.ShowWindow(WindowID.WindowID_Login);
            // await SceneChangeHelper.SceneChangeTo(scene, "Game");
        }
    }
}

Design Advantages

Main Advantages of the Event System

  1. Decoupled Design:

    • Event senders and receivers are completely decoupled
    • Modules communicate through events without direct dependencies
  2. Scene Isolation:

    • Events in different scenes don't interfere with each other
    • Avoids accidental event triggering and cross-scene pollution
  3. Async Support:

    • Supports asynchronous event handling
    • Suitable for handling complex business logic

Technical Support

Get Help

  • 💬 Join QQ group for discussion (ET Framework Group) : 474643097
  • ⭐ Follow the project on GitHub for the latest updates

Released under the MIT License.