Skip to content

Fast Event system for Unity with IDisposables and without unnecessary allocations

License

MIT, Unknown licenses found

Licenses found

MIT
LICENSE
Unknown
LICENSE.meta
Notifications You must be signed in to change notification settings

Pepengineers/PEPEvents

Repository files navigation

PEPEvents

Fast Event system for Unity with IDisposables and without unnecessary allocations

System Requirements

Unity 2019.4 or later versions. Don't forget to include the PEPools namespace and add assemly defenition reference.

Installation

You can also install via git url by adding this entry in your manifest.json

"com.pepervice": "https://github.com/Pepengineers/PEPEvents",

Overview

IMessage

The IMessage interface is needed to bind your game data to be publish to the subscriber. To pass the data you want to all subscribers of the object you need to inherit this data from the IMessage interface

public enum KeyStatus : sbyte
{
  Pressed,
  Unpressed
}

public readonly struct KeyChangeStatus : IMessage
{
  public readonly string Name;
  public readonly KeyStatus Status;
  public KeyChangeStatus(string name, KeyStatus status)
  {
    Name = name;
    Status = status;
  }
}

Implemented extension method in PEPEvents.Extensions namespace for userfrendly work with messages

public static void Publish<T>(this IBroker broker, T message);

IBroker and ISubscriber

To create your own data providers and subscribers you need IBroker and ISubscriber interfaces respectively

public readonly struct TestMessage : IMessage
{
}

internal sealed class TestSubscriber : ISubscriber<TestMessage>
{
  public int OnNextValue;
  public void OnNext(TestMessage message)
  {
    OnNextValue++;
  }
}

internal sealed class TestBroker : IBroker
{
  public void Shutdown()
  {
    this.UnsubscribeAll();
  }
}


var subscriber = new TestSubscriber();
var broker = new TestBroker();
broker.Subscribe(subscriber);
broker.Publish(new TestMessage());

Extension methods have been added to make it easier to subscribe and unsubscribe from brokers/subscribers

public static void Subscribe<TMessage>(this ISubscriber<TMessage> subscriber, IBroker broker);
public static void Subscribe<TMessage>(this IBroker broker, ISubscriber<TMessage> subscriber);
public static void Unsubscribe<TMessage>(this ISubscriber<TMessage> subscriber, IBroker broker);
public static void Unsubscribe<TMessage>(this IBroker broker, ISubscriber<TMessage> subscriber);
public static void UnsubscribeAll(this ISubscriber subscriber);
public static void UnsubscribeAll(this IBroker broker);

GameEvent

ScriptableObject class for the game event. To create a custom event, you need to inherit it and specify the CreateAssetMenu attribute

[CreateAssetMenu(menuName = "Create Event/Input/PressAnyKeyEvent", fileName = "PressAnyKeyEvent", order = 0)]
internal sealed class PressAnyKeyEvent : GameEvent {}

There is also a Generic version of this class which will be associated with IMessage data

[CreateAssetMenu(menuName = "Create Event/Input/PressAnyKeyEvent", fileName = "PressAnyKeyEvent", order = 0)]
internal sealed class PressAnyKeyEvent : GameEvent<KeyChangeStatus> {}

GameEventSubscriber

MonoBehaviour encapsulating GameEvent subscription mechanism Needed if you want to implement OnNext(TMessage message) from the code

internal sealed class TestUnitySubscriber : GameEventSubscriber<TestMessage, TestGameEvent>
{
  public override void OnNext(TestMessage message)
  {
  }
}

GameEventTrigger

Сontains a built-in UnityEvent for subscribing other components to events

internal sealed class TestUnityTrigger : GameEventTrigger<TestMessage>
{
}

изображение

There is an extended version if your game component also want gets data from the message.

internal sealed class TestUnityListener : TestUnityTrigger<TestMessage, TestGameEvent>
{
}

изображение

About

Fast Event system for Unity with IDisposables and without unnecessary allocations

Resources

License

MIT, Unknown licenses found

Licenses found

MIT
LICENSE
Unknown
LICENSE.meta

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages