Skip to content

rossogames/Rossoforge-Events

Repository files navigation

Rosso Games

Rossoforge

Rossoforge - Events

Rossoforge-Events is a lightweight and decoupled event system for Unity, built around generic interfaces and event buses. It allows different parts of your application to communicate through events without tight coupling or dependencies between components.

The following dependencies must be installed

Watch the tutorial on https://www.youtube.com/watch?v=YG_RjQMdM94

// Setup (requires Rossoforge-Services)
ServiceLocator.SetLocator(new DefaultServiceLocator());
ServiceLocator.Register<IEventService>(new EventService());
ServiceLocator.Initialize();

// 1. Define your event
public readonly struct PlayerDamagedEvent : IEvent
{
    public readonly int Damage;

    public PlayerDamagedEvent(int damage)
    {
        Damage = damage;
    }
}

// 2. Register the listener
public class DamageLogger : MonoBehaviour, IEventListener<PlayerDamagedEvent>
{
    private IEventService _eventService;

    void Start()
    {
        _eventService = ServiceLocator.Get<IEventService>();
        _eventService.RegisterListener(this);
    }
    private void OnDestroy()
    {
        _eventService.UnregisterListener(this);
    }

    public void OnEventInvoked(PlayerDamagedEvent eventArg)
    {
        Debug.Log($"Player took {eventArg.Damage} damage.");
    }
}

// 3. Raise the event
public class GameLogic : MonoBehaviour
{
    private IEventService _eventService;

    void Start()
    {
        _eventService = ServiceLocator.Get<IEventService>();
    }

    public void DoSomething()
    {
        // Raise event
        _eventService.Raise(new PlayerDamagedEvent(10));
    }
}

This package is part of the Rossoforge suite, designed to streamline and enhance Unity development workflows.

Developed by Agustin Rosso https://www.linkedin.com/in/rossoagustin/

About

A lightweight and decoupled event system for Unity, built around generic interfaces and event buses

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages