An event system for DI Framework.
Developed and tested with Unity 2020.3.0f1 LTS
- Open Package Manager through Window/Package Manager
- Click "+" and choose "Add package from git URL..."
- Insert the URL: https://github.com/Delt06/di-events.git?path=Packages/com.deltation.di-events
Add the following line to Packages/manifest.json
:
"com.deltation.di-events": "https://github.com/Delt06/di-events.git?path=Packages/com.deltation.di-events",
Explanation assumes you are familiar with basics of DI Framework.
MyEventBus.cs
:
using DELTation.DIFramework.Events;
using UnityEngine;
public class MyEventBus : ConfigurableEventBus
{
protected override void Configure()
{
To<SpacePressEvent>()
.Subscribe<SpacePressEventSubscriber>() // uses DI for creation
;
To<LmbClickEvent, int>()
.Subscribe((in int button, ref EventCancellationToken token) =>
Debug.Log($"Clicked mouse button: {button}.")
)
;
}
}
public class SpacePressEventSubscriber : EventSubscriber<NoArgs>
{
protected override void OnEventRaised(in NoArgs args, ref EventCancellationToken cancellationToken)
{
Debug.Log("Pressed space.");
}
}
public class SpacePressEvent : IEventTag { }
public class LmbClickEvent : IEventTag<int> { }
ControlsEventEmitter.cs
:
using DELTation.DIFramework.Events;
using UnityEngine;
public class ControlsEventEmitter : MonoBehaviour
{
// Access event bus through DI
public void Construct(IEventBus eventBus)
{
_eventBus = eventBus;
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
RaiseEvent.On(_eventBus).WithTag<SpacePressEvent>();
for (var i = 0; i < 3; i++)
{
if (Input.GetMouseButtonDown(i))
RaiseEvent.On(_eventBus).WithArguments(i).AndTag<LmbClickEvent>();
}
}
private IEventBus _eventBus;
}