This package provides you with a ready-made event management system. Originally made in Owlcat Games and modified for your own needs.
There are 3 ways to install this plugin:
- import EventBus.unitypackage via Assets-Import Package
- clone/download this repository and move the Plugins folder to your Unity project's Assets folder
- (via Package Manager) Select Add package from git URL from the add menu. A text box and an Add button appear. Enter a valid Git URL in the text box:
"https://github.com/llarean/EventBus.git"
- (via Package Manager) add the following line to Packages/manifest.json:
"com.llarean.eventbus": "https://github.com/llarean/EventBus.git",
- Create an interface that will be inherited from IGlobalSubscriber
using EventBusSystem;
public interface IExampleHandler : IGlobalSubscriber
{
void HandleExample();
}
- The class that should respond to events should inherit from the created interface
using EventBusSystem;
using UnityEngine;
public class ListenerExample : MonoBehaviour, IExampleHandler
{
public void HandleExample()
{
// Some code that needs to be executed when the event is Invoked
}
private void OnEnable()
{
EventBus.Subscribe(this);
private void OnDisable()
{
EventBus.Unsubscribe(this);
}
}
- The class that triggers the events
using EventBusSystem;
using UnityEngine;
public class EventCallerExample : MonoBehaviour
{
[SerializeField] private Button _save;
private void Start()
{
_save.onClick.AddListener(RaiseSaveEvent);
}
private void RaiseSaveEvent()
{
EventBus.RaiseEvent<IExampleHandler>(handler => handler.HandleSave());
}
}
- Follow The Microsoft code convention
- Integration of NUnit tests
- Issue ReadMe