来自foreach循环的Unity动画c#

时间:2018-02-18 15:58:03

标签: c# unity3d foreach unity5

我有一个UI文本,我在脚本中更新相机然后使用动画让它在屏幕上滑动。该脚本使用foreach循环,因为文本更改和动画运行的次数是可变的。它总是跳过第三个动画(它将在控制台中打印效果,但不会播放该动画),即使调用了4或5个效果。

private Animation Anim;
public Text NBtext;
public GameObject NBEffect, Tut, TouchInput;

public IEnumerator NiceBowlingEffects(List<string> Effects, bool FirstFrame)
{
    Anim = GetComponent<Animation>();
    NBEffect.SetActive(true);
    yield return new WaitForSeconds(.2f); //give frame ect a chance to load.
    foreach (var Effect in Effects)
    {
        NBtext.text = Effect;
        Print(Effect);
        Anim.Play();
        yield return new WaitForSeconds(Anim.clip.length);
    }
    NBEffect.SetActive(false);
    if (FirstFrame)
    {
        Tut.SetActive(true);
    }
    TouchInput.SetActive(true);
}

2 个答案:

答案 0 :(得分:1)

尝试更改&#34; WaitForSeconds &#34;到&#34; WaitForSecondsRealtime &#34;在foreach循环中,并告诉我它是否修复了它

答案 1 :(得分:0)

希望这对某人有帮助,但最终要使它适用于Nice Bowling,我不得不在更改文本和播放动画之间添加时间延迟。这是当前在Nice Bowling上运行的代码。

public IEnumerator NiceBowlingEffects(List<string> Effects, bool FirstFrame)
{
    Anim = GetComponent<Animation>();
    NBEffect.SetActive(true);
    yield return new WaitForSecondsRealtime(.1f); //give frame ect a chance to load.
    foreach (var Effect in Effects)
    {
        NBtext.text = Effect;
        yield return new WaitForSecondsRealtime(.1f); //text time to change
        Anim.Play();
        yield return new WaitForSecondsRealtime(Anim.clip.length +.01f);
    }
    NBEffect.SetActive(false);
    if (FirstFrame)
    {
        Tut.SetActive(true);
        Tut.GetComponent<UISprite>().Trigger();
    }
    TouchInput.SetActive(true);
}