- Home /
How to get the subscribed functions of a multicast delegate?
How can I get a list a all the functions that subscribe to a multicast delegate? Just trying to debug and keep things clear. It would help if I could display them in the inspector of the script of the delegate. Cheers.
Answer by Bunny83 · Oct 24, 2020 at 06:15 PM
I stumbled across your question when you asked it but I hadn't the time to post an answer so this tab got lost between my hundreds of other tabs ^^.
The main question is what exactly do you want in that "List of functions"? Delegates are direct method references. So they may be bound to a certain instance of an object and consists of a MethodInfo that describes the method it is referencing. This information can be extracted through reflection. Though it may not have a neat reference to the source code if that's what you're interested in.
I quickly created this delegate debug window for Unity. Just copy this file into a subfolder called "editor" in your project. you can open this editor window through the "Tools" menu item of the Unity editor. Just select a gameobject at runtime and it will automatically look for components which have fields of a delegate type. It will also search for nested / referenced objects as well. For every delegate / event field it finds it will list all subscribed methods by the method name as well as the containing type. If the method is an instance method of an object and the object is another MonoBehaviour derived class it will show an object field which allow you to figure out which object the subscribed method belongs to.
Note that this debugger only searches through the fields of your class. So it ignores properties. Though in the end a delegate always have to be stored in a field even when it's an auto-property. However the hidden field names of auto properties have quite nasty names. Also if the method you subscribed is a lambda expression / closure you get the type name and method name of the compiler generated hidden class which may not be that helpful.