简体   繁体   中英

Unity animation from foreach loop c#

I have a UI text that I update in script off camera then uses an animation to have it slide across the screen. The script uses a foreach loop since the text change and number of times the animation runs are variable. It always skips the third animation (it will print the effect in console but will not play that animation), even when 4 or 5 effects are called.

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);
}

尝试在foreach循环中将“ WaitForSeconds ”更改为“ WaitForSecondsRealtime ”,并告诉我是否已修复

Hopefully this helps someone but to finally get this to work for Nice Bowling I had to add a time delay between changing the text and playing the animation. Here's the code that's running on Nice Bowling right now.

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM