Giter VIP home page Giter VIP logo

eventbetter's Introduction

EventBetter

A Unity pubsub/messaging/event system for the lazy. Here's why:

  • Raising events is alloc-free.
  • No interfaces to implement, no base types to derive from.
  • No initialization, no cleanup.
  • No message codes.
  • Coroutine/async friendly.
  • One source file (everything else here is just for the test environment).
  • Clear and readable.

TL;DR:

Copy EventBetter.cs anywhere to your project or add https://github.com/gwiazdorrr/EventBetter.git in Package Manager (Add package from git URL...). The API you need to know is EventBetter.Listen and EventBetter.Raise. Done!

Example:

class TextMessage
{
    public string text;
}

class SimpleProducer : MonoBehaviour
{
    void Update()
    {
        EventBetter.Raise(new TextMessage() { text = "Hello World!" });
    }
}

class SimpleConsumer : MonoBehaviour
{
    void Awake()
    {
        EventBetter.Listen(this, (TextMessage msg) => Debug.Log(msg.text, this));
    }
}

There's no need to unlisten/unsubsribe from anything.

How does it work

The first parameter in Listen is the listener; as long as it is alive, the handler (the second parameter) will be invoked whenever there is a Raise called with a matching type. If the listener gets destroyed (with Destroy or when changing scenes), the handler will not get invoked anymore and all the references will get cleaned up no later than in the next LateUpdate.

It is possible thanks to UnityEngine.Object being the main citizen in the Unity world - it has a native representation with lifetime controlled entirely by the engine. There's no need to use WeakReference, ConditionalWeakTable and boilerplate like "unsubsribe" to avoid leaks, just keep track of the native parts.

More examples

Maybe you like async/await more?

class SimpleConsumerAsync : MonoBehaviour
{
    async void Awake()
    {
        var msg = await EventBetter.ListenAsync<TextMessage>();
        Debug.Log(msg.text, this);
    }
}

Or maybe you'd rather stick with good old coroutines?

class SimpleConsumerCoro : MonoBehaviour
{
    void Awake()
    {
        StartCoroutine(Coro());
    }

    IEnumerator Coro()
    {
        var listener = EventBetter.ListenWait<TextMessage>();
        yield return listener;
        Debug.Log(listener.First.text, this);
    }
}

Back to the basic Listen, maybe you want to stop listening after the first message arrives?

EventBetter.Listen(this, (TextMessage msg) => Debug.Log(msg.text, this), once: true);

Or listen only if the listening script is active and enabled?

EventBetter.Listen(this, (TextMessage msg) => Debug.Log(msg.text, this), exculdeInactive: true);

If you are not in a MonoBehaviour and still want to use EventBetter, use:

IDisposable listener = EventBetter.ListenManual( (TextMessage msg) => Debug.Log(msg.text, this) );
// ...
listener.Dispose();

eventbetter's People

Contributors

gwiazdorrr avatar leinnan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.