Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/joanstinson/unitymessagingsystem
A typesafe, lightweight Unity message bus system that respects the Open-Closed principle.
https://github.com/joanstinson/unitymessagingsystem
csharp message-bus open-closed-principle unity-events unity3d unity3d-editor unity3d-plugin
Last synced: about 1 month ago
JSON representation
A typesafe, lightweight Unity message bus system that respects the Open-Closed principle.
- Host: GitHub
- URL: https://github.com/joanstinson/unitymessagingsystem
- Owner: JoanStinson
- License: mit
- Created: 2022-11-26T13:49:22.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2022-11-29T00:11:08.000Z (almost 2 years ago)
- Last Synced: 2024-09-23T11:03:40.088Z (about 2 months ago)
- Topics: csharp, message-bus, open-closed-principle, unity-events, unity3d, unity3d-editor, unity3d-plugin
- Language: C#
- Homepage:
- Size: 135 KB
- Stars: 8
- Watchers: 4
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Unity Messaging System
A typesafe, lightweight Unity message bus system that respects the Open-Closed principle.
## 📣 How It Works
For starters, import the package located in the [Releases](https://github.com/JoanStinson/UnityMessagingSystem/releases) section into your project.* ### Step 1 - Write a message. It can be a class or a struct.
```csharp
public readonly struct PlayerSpawnMessage
{
public readonly int Health;
public readonly float Speed;
}
```
> Structs are preferred to reduce GC work and because messages will 99% of the time only contain data.If the message doesn't require data, you can have an empty class or struct too.
```csharp
public readonly struct PlayerDeathMessage { }
```* ### Step 2 - Inherit from IMessagingSubscriber<> explicitly defining the message type and subscribe to it.
```csharp
public class MessagingSubscriberExample : MonoBehaviour,
IMessagingSubscriber,
IMessagingSubscriber
{
private void OnEnable()
{
MessagingSystem.Instance.Subscribe(this);
MessagingSystem.Instance.Subscribe(this);
}private void OnDisable()
{
MessagingSystem.Instance.Unsubscribe(this);
MessagingSystem.Instance.Unsubscribe(this);
}public void OnReceiveMessage(PlayerSpawnMessage message)
{
Debug.Log(message.ToString());
}public void OnReceiveMessage(PlayerDeathMessage message)
{
Debug.Log("Received Player Death message");
}
}
```
> You can make use of the DefaultMessagingSystem singleton via the Instance property. Nevertheless, I prefer to inject the dependency, so that later on I can mock it and do unit tests, apart from having a different messaging system implementation if it where required.* ### Step 3 - Create a message instance and dispatch it.
```csharp
public class ExampleDispatcherClass : MonoBehaviour
{
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
MessagingSystem.Instance.Dispatch(new PlayerSpawnMessage(3, 5));
MessagingSystem.Instance.Dispatch(new PlayerDeathMessage());
}
}
}
```
> The same as I said before, be sure to have the same instance referenced. Either by injecting it via a constructor/initialize method or making use of the singleton Instance (although less recommended for obvious reasons, unless you are a beginner).## 🔍 Unit Tests
Unit tested with 100% code coverage to be certain the messaging system implementation works properly.