2015-05-16 62 views
0

我想弄清楚如何在Unity中結合StartCoroutine()methodInfo.Invoke()在Unity中結合協程和反射

首先,非常基本的,顯式調用的代碼,我想這要避免:

if (ACTION_MOVE == action) { 
    StartCoroutine(robotBehaviour.move()); 
} 
else if (ACTION_ROTATE == action) { 
    StartCoroutine(robotBehaviour.rotate()); 
} 
... 

的調用的方法是這樣的:

public override IEnumerator move() 
{ 
    for (int i = 0; i < ITERS; i++) { 
     // Do something 
     yield return new WaitForSeconds(N); 
    } 
} 

因此,與反射的一點點,我設法動態地撥打電話:

RobotBehaviour robotBehaviour = getRobotBehaviour() 
Type type = robotBehaviour.GetType(); 
MethodInfo methodInfo = type.GetMethod((string)sentences[lineNo]); 
result = methodInfo.Invoke(robotBehaviour, null); 

但是,現在我無法設法調用robotBehaviour with StartCoroutine()

任何想法?

回答

1

已解決。我需要做的是告訴robotBehaviour啓動協程:

RobotBehaviour robotBehaviour = getRobotBehaviour() 
Type type = robotBehaviour.GetType(); 
MethodInfo methodInfo = type.GetMethod((string)sentences[lineNo]); 
robotBehaviour.StartCoroutine(methodInfo.Name, 0); // <-- Solved!