/di-events

An event system for DI Framework.

Primary LanguageC#MIT LicenseMIT

DI Events

Version License: MIT

An event system for DI Framework.

Developed and tested with Unity 2020.3.0f1 LTS

Installation

Option 1

Option 2

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",

Example

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;
}