Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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: 2 months ago
JSON representation

A typesafe, lightweight Unity message bus system that respects the Open-Closed principle.

Awesome Lists containing this project

README

        

# Unity Messaging System
A typesafe, lightweight Unity message bus system that respects the Open-Closed principle.



Made With Unity


License


Last Commit


Repo Size


Downloads


Last Release





## 📣 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.