结合协同中的协同与反思

时间:2015-05-16 01:06:17

标签: c# reflection unity3d coroutine

我试图找出如何将Unity中的 StartCoroutine() methodInfo.Invoke()结合起来。

首先,我想避免使用非常基本的显式调用代码:

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);

但是,现在我无法通过 StartCoroutine()调用 robotBehaviour

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

解决。我需要做的是告诉robotBehaviour启动协程:

class AppointmentSerializer(serializers.ModelSerializer):

  id            = serializers.IntegerField(read_only=True)
  doctor        = ContactField()
  patient       = ContactField()

  class Meta:
    model  = Appointment
    fields = (
      'id',
      'doctor',
      'patient',
    )