I’ve been working on a couple custom inspectors for a small event handling system. I want to get references to certain kinds of methods, and be able to select them via editor. So far so good, thanks to XML acting as a database (to get standard event names) and System.Reflection (GetMethod and checking parameters).
Problem is: I’ve found no way to call those methods. I’ve got reference to the GameObject that holds the method, and both the script name and method name (as strings). Invoke won’t work for me, because I need to store those methods for later use (acting as delegates/callbacks), and Invoke only seems to call methods right at the moment.
Thanks for the reply. I finally found what I was looking for (thanks to a script from the NGUI guys). So, anyone in the future looking for something like this, here it is:
//All checks done previously before call
//You want to make sure to send the proper Type, and check the method's name isn't empty and the method itself has a similar structure to MyDelegate
//Limitation: your GameObject can only have one Component of the Type you're sending (maybe storing the Component index somewhere might do the trick)
private static MyDelegate GetEventHandler(GameObject _actionGameObject, string _componentType, string _methodName){
var actionComponent = actionGameObject.GetComponent(_componentType);
MyDelegate d = System.Delegate.CreateDelegate(typeof(MyDelegate), actionComponent, methodName) as MyDelegate;
return d;
}
Invoke() wasn’t an option because I needed the reference to the delegate for later use.