vendredi 15 mai 2015

Combining Coroutines and Reflection in Unity

I'm trying to figure out how can I combine StartCoroutine() with methodInfo.Invoke() in Unity.

First, the very basic, explicit calls code which I'd like to avoid:

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

The called methods look like this:

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

So with a little bit of reflection, I managed to make the call dynamically:

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

However, now I can't manage to invoke robotBehaviour with StartCoroutine().

Any ideas?





Aucun commentaire:

Enregistrer un commentaire