I would like to get the name of the function (String). I tried using:
var callback:function() = MyCallback;
....
Debug.Log(callback.Method.Name);
....
function MyCallback(){
}
And it logs “Invoke” where I would like to log “MyCallback”
I would like to get the name of the function (String). I tried using:
var callback:function() = MyCallback;
....
Debug.Log(callback.Method.Name);
....
function MyCallback(){
}
And it logs “Invoke” where I would like to log “MyCallback”
So there could be actually two ways you want to figure out your issue:
first using reflection considering you may not know the name of the method but you know the class where it belongs.
#pragma strict
import System;
import System.Reflection;
function Start () {
var myClass:Type = typeof(MyClass);
var infos :MethodInfo[] = myClass.GetMethods(BindingFlags.Public | BindingFlags.Instance|BindingFlags.DeclaredOnly);
for(var info : MethodInfo in infos){
print(info.Name);
}
}
function MyMethodName(){
print("I do not do much...");
}
Thing is, this will print out all the methods you created. The BindingFlags are there to select out all the inherited methods. Try and see what happens if you remove BindingFlags.DeclaredOnly
Second way is more likely to be the one you are after actually.
#pragma strict
import System;
var method :Action;
function Start () {
method = MyMethodName;
print(method.Method);
}
function Update(){
if(Input.GetKeyDown(KeyCode.Space))print(method.Method);
}
function MyMethodName(){
print("I do not do much...");
}
So you are just creating a reference of type Action which stands for delegate for methods that return void. The Method property returns the name of the method it is currently pointing to.