[176407-캡처.png|176407]
This code doesn’t work at runtime. It’s only zero.
However, if you add it from the inspector, it works normally work.
I use Unity 2019.4.15
[176407-캡처.png|176407]
This code doesn’t work at runtime. It’s only zero.
However, if you add it from the inspector, it works normally work.
I use Unity 2019.4.15
Runtime assignment via AddListener() adds non-persistent listeners only.
There doesnt seem to be a native function to read non-persistent listener data, however there is a way using reflection:
public static int GetListenerNumber(this UnityEventBase unityEvent)
{
var field = typeof(UnityEventBase).GetField("m_Calls", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly );
var invokeCallList = field.GetValue(unityEvent);
var property = invokeCallList.GetType().GetProperty("Count");
return (int)property.GetValue(invokeCallList);
}
Note: this is an extension method, you will be able to call it on your event directly. it will return the aggregated listener count, meaning inspector-assigned and runtime events. for runtime events only, for runtime only, go to InvokableCallList
class definition and use the corresponding list.