我不知道我在协程函数中是否使用了太多的'yield return new WaitForSeconds'

时间:2018-07-11 02:07:24

标签: c# unity3d coroutine

** Unity5.6.5f1 / C#

我用协程运行动画。

而不是在更新函数中创建多个if语句,

我认为协程会更好。

但是我认为我过多地使用了'yield return new WaitForSeconds',

以这种方式编写协程是否常见?

private IEnumerator AnimationControl()
{
    yield return new WaitForSeconds(41f);        
    objAni.gameObject.SetActive(true);
    obj2Ani.gameObject.SetActive(true);
    obj3Ani.gameObject.SetActive(true);
    objAni.Play("Stay");
    obj2Ani.Play("Stay");
    obj3Ani.Play("Stay");

    yield return new WaitForSeconds(5.667f);        
    objAni.Play("Run");
    obj2Ani.Play("Run");
    obj3Ani.Play("Action");

    yield return new WaitForSeconds(3.666f);
    objAni.Play("Fire01");

    yield return new WaitForSeconds(2.334f);
    obj3Ani.Play("Panic");
    obj2Ani.gameObject.SetActive(false);

    yield return new WaitForSeconds(8.333f);
    objAni.Play("Fire02");

    yield return new WaitForSeconds(6.333f);      
    objAni.Play("Fire03");

    yield return new WaitForSeconds(6.1337f);       
    obj3Ani.Play("Stay");
    objAni.Play("Stay");

    yield return new WaitForSeconds(3.2003f);        
    obj3Ani.Play("Surprise02");
    objAni.Play("Surprise");

    yield return new WaitForSeconds(3.333f);                
    obj3Ani.Play("Run");
    objAni.Play("Run");

    yield return new WaitForSeconds(6f);
    objAni.Play("Stay");

    yield return new WaitForSeconds(1.667f);      
    objAni.Play("Run");

    yield return new WaitForSeconds(2.333f);        
    obj3Ani.gameObject.SetActive(false);

    yield return new WaitForSeconds(0.667f);        
    objAni.gameObject.SetActive(false);

    yield return new WaitForSeconds(1.333f);
    textObj.SetActive(false);

    yield return new WaitForSeconds(2.667f);
    endingObj.SetActive(true);
    allObj.SetActive(false);
}

感谢阅读。

1 个答案:

答案 0 :(得分:2)

这正是协程的用途,它是按顺序执行许多代码,甚至可以随时等待。其实不算太多。我看到的唯一问题是每次都创建新的WaitForSeconds。如果您知道等待时间或对时间进行了硬编码,请在函数外部声明WaitForSeconds,然后屈服于它们。这实际上取决于您调用AnimationControl函数的频率。

例如

private IEnumerator AnimationControl()
{
    yield return new WaitForSeconds(41f);
    objAni.gameObject.SetActive(true);
    obj2Ani.gameObject.SetActive(true);
    obj3Ani.gameObject.SetActive(true);
    objAni.Play("Stay");
    obj2Ani.Play("Stay");
    obj3Ani.Play("Stay");

    yield return new WaitForSeconds(5.667f);
    objAni.Play("Run");
    obj2Ani.Play("Run");
    obj3Ani.Play("Action");
}

成为

WaitForSeconds wTime1 = new WaitForSeconds(41f);
WaitForSeconds wTime2 = new WaitForSeconds(5.667f);

private IEnumerator AnimationControl()
{
    yield return wTime1;
    objAni.gameObject.SetActive(true);
    obj2Ani.gameObject.SetActive(true);
    obj3Ani.gameObject.SetActive(true);
    objAni.Play("Stay");
    obj2Ani.Play("Stay");
    obj3Ani.Play("Stay");

    yield return wTime2;
    objAni.Play("Run");
    obj2Ani.Play("Run");
    obj3Ani.Play("Action");
}


如果您确实想减少WaitForSeconds代码,则可以围绕它编写包装类。此类应保留等待时间甚至更好的时间,请使用WaitForSeconds实例,然后使用Action表示要在等待之后执行的代码。为此List创建一个class,在其上循环,等待,然后执行Action变量中的代码。

包装器类:

public class AnimControlInfo
{
    public WaitForSeconds waitTime;
    public Action action;
}

创建列表:

List<AnimControlInfo> animInfo = new List<AnimControlInfo>();

初始化List,以便可以在AnimationControl函数中使用它:

void InitControlAnimInfo()
{
    AnimControlInfo aI1 = new AnimControlInfo();
    aI1.waitTime = new WaitForSeconds(41f);
    aI1.action = delegate
    {
        objAni.gameObject.SetActive(true);
        obj2Ani.gameObject.SetActive(true);
        obj3Ani.gameObject.SetActive(true);
        objAni.Play("Stay");
        obj2Ani.Play("Stay");
        obj3Ani.Play("Stay");
    };

    AnimControlInfo aI2 = new AnimControlInfo();
    aI2.waitTime = new WaitForSeconds(5.667f);
    aI2.action = delegate
    {
        objAni.Play("Run");
        obj2Ani.Play("Run");
        obj3Ani.Play("Action");
    };

    //Add to List
    animInfo.Add(aI1);
    animInfo.Add(aI2);
}

然后使用您新的AnimationControl函数来等待并播放动画或激活/停用GameObects:

private IEnumerator AnimationControl()
{
    //Loop over the List, wait then do action
    for (int i = 0; i < animInfo.Count; i++)
    {
        yield return animInfo[i].waitTime;
        if (animInfo[i].action != null)
            animInfo[i].action();
    }
}

AnimationControl函数中的代码量已减少。它还减少了WaitForSeconds类的过度使用。