2016-07-28 92 views
2

这个问题的实例是用于Unity3D的实用程序脚本,但问题是刚刚在C#;寻找方法

我提供的字符串(onClickCallbackPath)的脚本看起来像“GameObjectName.ComponentTypeName.CallbackDelegateName”。

查找游戏对象和组件是没有问题的,但看看这个代码:

string[] ss = onClickCallbackPath.Split ("." [0]); 
    Transform onClickTarget = Tools.FindDeepChild (transform.root, ss [0]); 
    MonoBehaviour[] allComponentsOnTarget = onClickTarget.GetComponents<MonoBehaviour>(); 

    foreach (MonoBehaviour mb in allComponentsOnTarget) 
    { 
     if (mb.GetType().Name == ss [1]) 
     { 
      MethodInfo[] methods = mb.GetType().GetMethods (BindingFlags.Public); 

      foreach (MethodInfo mi in methods) 
      { 
       if (mi.Name == ss [2]) 
       { 
        // And here is how I imagine it to work, and look for something similar... 
        // but of course there is no GetInstance method in MethodInfo 
        Action a = (Action) mi.GetInstance(mb); 
        break; 
       } 
      } 

      break; 
     } 
    } 

正如你看到的,我需要找到Action类型的对象(我确定它是法正确的签名),从我发现的MonoBehaviour内。

我想看看所有的MethodInfo属性有类似的东西,我找了,也试图找到网解决方案(这里也对SO),但没有成功。我敢打赌,找到解决方案的问题只是错误地指出了问题。

但我希望你明白什么是我的问题。

任何帮助表示赞赏。

回答

5

什么你要找的是

(Action)Delegate.CreateDelegate(typeof(Action), mb, mi) 
+0

完美!谢谢。 –