如何委托调用动态方法名称和动态参数

1 c# unity-game-engine

它可以有一个盲目处理动态方法名称和动态参数的函数吗?

我正在使用Unity和C#.我希望在从场景中删除一个对象(被破坏)后,Gizmo的绘制会持续一段时间,所以我想动态地将绘图职责传递给另一个对象.

例如,我希望能够这样做:

GizmoManager.RunThisMethod(Gizmos.DrawSphere (Vector3.zero, 1f));
GizmoManager.RunThisMethod(Gizmos.DrawWireMesh (myMesh));
Run Code Online (Sandbox Code Playgroud)

正如您所看到的那样,调用方法并且参数计数会有所不同.有没有办法实现我的目标,而无需为每个Gizmo类型编写一个非常精细的包装器?(有11个.)(侧面目标:我想添加自己的论点来说明经理应该多长时间绘制Gizmo,但这是可选的.)

Dar*_*con 5

我建议把电话变成一个lambda.这将允许GizmoManager根据需要多次调用它.

class GizmoManager
{
  void RunThisMethod(Action a)
  {
    // To draw the Gizmo at some point
    a();
  }
  // You can also pass other parameters before or after the lambda
  void RunThisMethod(Action a, TimeSpan duration)
  {
    // ...
  }
}

// Make the drawing actions lambdas
GizmoManager.RunThisMethod(() => Gizmos.DrawSphere(Vector3.zero, 1f));
GizmoManager.RunThisMethod(() => Gizmos.DrawWireMesh(myMesh));

// You could also draw multiple if needed:
GizmoManager.RunThisMethod(() => {
    Gizmos.DrawSphere(Vector3.zero, 1f);
    Gizmos.DrawWireMesh(myMesh);
});
Run Code Online (Sandbox Code Playgroud)