Skip to content

Instantly share code, notes, and snippets.

@giacomelli
Last active November 6, 2021 18:25
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giacomelli/28ee9438ad522985a9611c21312813bc to your computer and use it in GitHub Desktop.
Save giacomelli/28ee9438ad522985a9611c21312813bc to your computer and use it in GitHub Desktop.
UnityEvent Dump Log
using System.Runtime.CompilerServices;
using System.Text;
using UnityEngine;
using UnityEngine.Events;
namespace Giacomelli.Framework
{
public static class UnityEventExtensions
{
[System.Diagnostics.Conditional("DEBUG")]
public static void Dump<TArgument>(
this UnityEvent<TArgument> instance,
string eventName,
[CallerFilePath] string filePath = null,
[CallerLineNumber] int lineNumber = 0,
[CallerMemberName] string memberName = null)
{
var listenersCount = instance.GetPersistentEventCount();
var msg = new StringBuilder();
msg.AppendFormat($"[UNITY EVENT DUMP]\n");
msg.AppendFormat($"{filePath} ({lineNumber}): {memberName}\n");
msg.AppendFormat($"\t- Listeners: {listenersCount}\n");
for (int i = 0; i < listenersCount; i++)
{
msg.AppendFormat($"\t\t- Listener {i}: {instance.GetPersistentTarget(i)}.{instance.GetPersistentMethodName(i)}\n");
}
Debug.Log(msg.ToString());
}
}
}
public UnityEvent<float> OnEnergyLost;
// ...
OnEnergyLost.Dump("OnEnergyLost");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment