问题
Is there a way to have a variable point to one of a number of coroutines in C# in Unity3D?
public class Example : MonoBehaviour
{
? something ? crt;
private IEnumerator CoroutineA()
{
}
private IEnumerator CoroutineB()
{
}
void Start()
{
crt = CoroutineA;
StartCoroutine(crt);
}
}
回答1:
The type that you are looking for is a delegate. Delegates are similar to function pointers, and are not specific to Unity3D.
public class Example : MonoBehaviour
{
private delegate IEnumerator CoroutineDelegate();
private IEnumerator CoroutineA()
{
}
private IEnumerator CoroutineB()
{
}
public void Start()
{
CoroutineDelegate crt = CoroutineA;
StartCoroutine(crt());
}
}
来源:https://stackoverflow.com/questions/34550109/refer-to-one-of-many-coroutines-in-unity3d