Message Service
A simple, efficient C# messaging system for Unity. Enables decoupled communication between game systems.
com.nonatomic.messageservice 
Install via UPM
Add to Unity Package Manager using this URL
https://www.pkglnk.dev/messageservice.git README Markdown
Copy this to your project's README.md
## Installation
Add **Message Service** to your Unity project via Package Manager:
1. Open **Window > Package Manager**
2. Click **+** > **Add package from git URL**
3. Enter:
```
https://www.pkglnk.dev/messageservice.git
```
[](https://www.pkglnk.dev/pkg/messageservice)Dependencies (1)
README
MessageService
Overview
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.
Installation
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.
Support
If you like my work then please consider showing your support by buying me a brew

Features
- 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
Usage
Creating Messages
Messages can be any type, struct or class, depending on your needs.
Structs are often preferred because they are value types, can be more efficient in some scenarios, and have well-defined copy semantics.
Classes might be a better choice if your message needs reference semantics, inheritance, or more complex structures.
public struct MyMessage
{
public string Content;
}
Subscribing to a Message
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
}
Unsubscribing from a Message
To unsubscribe, use the Unsubscribe
_messageService.Unsubscribe<MyMessage>(HandleMyMessage);
Publishing a Message
To publish a message, use the Publish
_messageService.Publish(new MyMessage { Content = "Hello, world!" });
Optionally pass a publisher object to the Publish method for debugging purposes and potential future tooling:
_messageService.Publish(new MyMessage { Content = "Hello, world!" }, publisher: this);
Subscribe Once
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);
Contributing
Contributions to MessageService are welcome! Please refer to CONTRIBUTING.md for guidelines on contributing to the project.
License
MessageService is licensed under the MIT license. See LICENSE for more details.
No comments yet. Be the first!