Skip to content

Non-thread-safe event manager. Use only in single thread environment. Preferably Unity thread.

License

Notifications You must be signed in to change notification settings

Vadimskyi/EventManager

Repository files navigation

Event Manager for C# (Unity, .NET, .NET Core)

openupm

Table of Contents

Installation

This library is distributed via Unity's built-in package manager. Required Unity 2018.3 or later. Sinse Unity's package manager does not support git-url dependencies you should install them manually, if required.

Unity Package

  • Open Unity project
  • Download and run .unitypackage file from the latest release

UPM CLI

# go to the unity project folder
$ cd ~/Document/projects/hello-openupm

# add package
$ openupm add com.vadimskyi.eventmanager
  • Open Unity for package to be installed

Quick Start

Define the class for your event and derive it from EventBase<ClassName> abstarct base class.

public class TestEvent : EventBase<TestEvent>
{
}

Optionally, you can also define argument data or pass it directly for the generic argument type.

public class PlayerMovedEvent : EventBase<PlayerMovedEvent, PlayerMovedData>
{
}

public readonly struct PlayerMovedData
{
    public readonly int PlayerId;
    public readonly Vector3 Position;
    public PlayerMovedData(int id, Vector3 pos)
    {
        PlayerId = id;
        Position = pos;
    }
}

Subscribe to event:

public class SomeEventListener : MonoBehaviour
{
    public void Start()
    {
        PlayerMovedEvent.Subscribe(OnPlayerMoved);
    }
    
    public void OnPlayerMoved(PlayerMovedData data) { ... }
    
    public void OnDestroyed()
    {
        PlayerMovedEvent.Unsubscribe(OnPlayerMoved);
    }
}

Invoke event:

public class Player : MonoBehaviour
{
    private int _playerId;
    
    public void Update()
    {
        if(positionChanged)
        {
            PlayerMovedEvent.Invoke(new PlayerMovedData(_playerId, transform.localPosition));
        }
    }
}

Main reasoning

  • Allows communication between components with regards to separations of concerns principle.
  • Light-weight and easy on GC. Allowing it to be used in tight loops and Unity Update cycles.
  • Maintainable and scalable architecture allows this system to be further optimized by using only blittable types for events and/or by managing native pointers instead of events-array.

Precaution

Internally, this event manager is operating as "global" manager. Meaning that every event subscription will be persistent through all application lifecycle, unless unsubscribed manually. This, in fact, makes unsubscription of anonymous delegates problematic. More on this issue: https://stackoverflow.com/questions/25563518/why-cant-i-unsubscribe-from-an-event-using-a-lambda-expression/25564492#25564492 Bottom line is: try not to use anonymous delegates with this [Event Manager] if possible.

Author Info

Vadim Zakrzhewskyi (a.k.a. Vadimskyi) a software developer from Ukraine.

~10 years of experience working with Unity3d, mostly freelance/outsource.

About

Non-thread-safe event manager. Use only in single thread environment. Preferably Unity thread.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages