Refer to one of many coroutines in Unity3D?

…衆ロ難τιáo~ 提交于 2019-12-24 13:58:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!