Freyja Event System

A robust and flexible event system for Unity, enabling efficient communication between game objects.

Features

  • Generic Events: Define events with custom data types for flexibility.

  • Listener Interface: All listeners implement a common interface for easy identification.

  • Automatic Registration: EventListeners automatically register and unregister based on their lifecycle.

  • Safe Event Triggering: Ensures only compatible listeners receive events.

Requirements

  • Unity 2021.3.22.f1 or later

Dependencies

Name
Tags

Installation

  1. Install from Unity Package Manager -> (Window -> Package Manager -> Add package from git URL)

Example Usage

Define your Custom Event

CarEvent
using Freyja.EventSystem;

public class CarEvent : Event<CarEvent>
{
   public string Name { get; set; }
   public int Speed { get; set; }
   
   public override string ToString()
   {
     return $"Name: {Name}, Speed: {Speed}";
   }
}

Create an Event Listener

CarEventListener
using Freyja.EventSystem;

public class CarEventListener : EventListener<CarEvent>
{
     public override void OnEventRaised(object sender, CarEvent value)
     {
         Debug.Log("Car Event Triggered " + value);
     }
}

Trigger Listener

Car
public class Car : MonoBehaviour
{
     public void TriggerCar(string carName, int speed)
     {
         CarEvent.Trigger(this, new CarEvent()
         {
             Name = carName,
             Speed = speed,
         });
     }
};

Last updated