Unity Event Framework is a simple and elegant way to handle events between your objects in Unity.
The framework uses the PubSub pattern with current Payload states for easy access to previously sent events.
Inspired by the work of SuperMaxim
There are several advantages to this approach, ranging from reducing cohesion in your project by not directly referencing objects to resolving asynchronous events within the project. You can also retrieve the state of an event at any time if it has been previously sent.
Installation process:
- Install a package using Unity Package Manager (https://github.com/DevsDaddy/UnityEventFramework.git?path=/DevsDaddy/Shared/EventFramework/);
- See usage examples below;
Unity Package Manager URL:
https://github.com/DevsDaddy/UnityEventFramework.git?path=/DevsDaddy/Shared/EventFramework/
Create Payload (Event Model) for your Events:
public class TestPayload : IPayload
{
public string Message = "";
}
To Subscribe for Payload (event) you can add listener via EventMessenger Class:
EventMessenger.Main.Subscribe<TestPayload>(payload => {
Debug.Log(payload.Message);
});
To Publish New Payload (Event) you can:*
EventMessenger.Main.Publish(new TestPayload {
Message = "Hello world!"
});
Also you can get latest state for any early published payload (or null if not published yet):*
TestPayload currentPayload = EventMessenger.Main.GetState<TestPayload>();
Debug.Log(currentPayload.Message);
See framework usage examples in other projects:
- Game Shield - Unity Game Security and Anti-Cheat Toolkit;
- Simple UI Framework for management of your UI;
- OneUI Kit - Full-featured UI Kit for your applications and Games. Ready for MVC/MVP/MVVM;
I plan to add the following functionality in the near future:
- Reactive Fields with Auto-Resolving via EventMessenger Class;
You can support the development and updating of libraries and assemblies by dropping a coin:
Bitcoin (BTC) | bc1qef2d34r4xkrm48zknjdjt7c0ea92ay9m2a7q55 |
Etherium (ETH) | 0x1112a2Ef850711DF4dE9c432376F255f416ef5d0 |
Boosty | https://boosty.to/devsdaddy |