MessageService is a simple message passing system for decoupling components in a Unity application. It provides a mechanism for subscribing to messages of a specific type and publishing messages to all interested subscribers.
To install MessageService in your Unity project, add the package from the git URL: https://github.com/PaulNonatomic/MessageService.git
using the Unity package manager.
- Subscribe to Messages: Listen for specific message types.
- Unsubscribe from Messages: Stop listening for specific message types.
- Publish Messages: Send messages to all subscribers of that message type.
- Automatically unsubscribe: After receiving a message once with SubscribeOnce feature
To subscribe to a message type, use the Subscribe<T>
method where T
is your message type:
_messageService.Subscribe<MyMessage>(HandleMyMessage);
private void HandleMyMessage(MyMessage message)
{
// Handle the message
}
To unsubscribe, use the Unsubscribe method:
_messageService.Unsubscribe<MyMessage>(HandleMyMessage);
To publish a message, use the Publish method:
_messageService.Publish(new MyMessage { Content = "Hello, world!" });
Messages can be subscribed to be received only once using SubscribeOnce. After the message is received for the first time, the handler is automatically unsubscribed.
_messageService.SubscribeOnce<MyMessage>(HandleMyMessage);
Contributions to MessageService are welcome! Please refer to CONTRIBUTING.md for guidelines on contributing to the project.
MessageService is licensed under the MIT license. See LICENSE for more details.