Skip to main content

Global Events

Warudo contains a EventBus class that allows you to subscribe to and broadcast global events. To define a custom event class, inherit from the Warudo.Core.Events.Event class. For example:

public class MyEvent : Event {
public string Message { get; }
public MyEvent(string message) {
Message = message;
}
}

To subscribe (listen) to an event:

Context.EventBus.Subscribe<MyEvent>(e => {
Debug.Log(e.Message);
});

To broadcast (fire) an event:

Context.EventBus.Broadcast(new MyEvent("Hello, world!"));

You should unsubscribe from events when you no longer need to listen to them:

var subscriptionId = Context.EventBus.Subscribe<MyEvent>(e => {
Debug.Log(e.Message);
});
// Later
Context.EventBus.Unsubscribe<MyEvent>(subscriptionId);

Subscribing Events In Entities

If you are writing code inside an entity type, you can use the Subscribe method directly to avoid handling event subscription IDs:

// Inside an entity type, i.e., asset, node, plugin, structured data
Subscribe<MyEvent>(e => {
Debug.Log(e.Message);
});

The events are automatically unsubscribed when the entity is destroyed.

当前页面还没有中文翻译,帮助我们改进!

你正在阅读的页面还没有对应的中文翻译。 如果可以的话可以在 Github 提交你的翻译帮助我们改进!

上次更新于 2024.06.15