Introduction

I started making these events because my projects used lots of events and it wasn’t always clear what was happening. UnityEvents are nice when you have a small project, but because they don’t show up in your IDE it’s easy to lose track of them. Especially with deeply nested UI elements. So sooner or later you will switch to C# Events. Then at least your code trails don’t have dead ends (0 calls on a public method because it was set up in the Editor). However, then you don’t see the events in the Editor anymore like you could with UnityEvents. Besides that, C# Events can be error prone (like accidently having listeners registered twice). That’s why I wanted to create something that takes the best of both solutions and make Events Trackable.

So with Trackable Events you use C# Events to avoid those pesky dead ends when tracing down a code path. It then keeps track of all the listeners and shows them in the inspector. Whenever an Event gets Triggered it logs it to the console with the Sender’s Class + Method and the Listener’s Class + Method. To quickly test an Event the inspector also includes a Trigger button with potential Test Values.

Simple Events

Create

using AD.TrackableEvents;

public class ExampleScript : MonoBehaviour
{
    public TEvent trackableEvent;
}

Add using AD.TrackableEvents; to the top.

In the inspector you will see this:

Untitled

The Trigger Button becomes active during Play Mode.

Trigger

trackableEvent.Trigger();
trackableEvent.Trigger(true);

The parameter is whether to Log the Event to the Console

Listen

private void OnEnable()
{
	trackableEvent.AddListener(OnEvent);
}

private void OnDisable()
{
	trackableEvent.RemoveListener(OnEvent);
}

Always a good habit to RemoveListener() as well.

Events with Values

Create

using AD.TrackableEvents;

public class ExampleScript : MonoBehaviour
{
    public TEvent_Color ColorEvent;
}