简体   繁体   English

统一-一次播放动画

[英]unity - play animations one at a time

I have a coroutine function that looks something like that: 我有一个协程函数,看起来像这样:

// Replacing on start game
public virtual void Replace()
{
    StartCoroutine(ReplaceCoroutine());
}

// the Replacing animation
private IEnumerator ReplaceCoroutine()
{
    // check if the piece has an animation attached
    Animator animator = GetComponent<Animator>();
    if (animator)
    {
        animator.Play(clearAnimation.name);
        yield return new WaitForSeconds(clearAnimation.length);
    }
}

and I activate it with a loop like so: 我用如下循环激活它:

// mixing pieces
for (int i = 0; i < 5; i++)
{
    pieces[i].Replace();
}

The problem is that the loop goes all at once, and everything animates at the same time. 问题在于循环一次全部完成,并且所有动画同时进行。 I want to make sure, that pieces[2] for example, will start he's animation coroutine only when pieces[1] got to half of it's time in the animation. 我想确保,例如,件[2]仅在件[1]达到动画时间的一半时才开始他的动画协程。

is it possible? 可能吗?

thank you 谢谢

You have to call your 你必须打电话给你

for (int i = 0; i < 5; i++)
{
    pieces[i].Replace();
}

from another coroutine function then wait for each coroutine function to return. 来自另一个协程函数,然后等待每个协程函数返回。 You do that by yielding the StartCoroutine function call. 您可以通过yielding StartCoroutine函数调用来实现。 The code below will call yourCoroutineFunction function and then wait for it to finish before running the next loop. 下面的代码将调用yourCoroutineFunction函数,然后等待其完成,然后再运行下一个循环。

for (int i = 0; i < 5; i++)
{
    yield return StartCoroutine(yourCoroutineFunction());
}

I can't tell where your functions are placed but below should work for you. 我不能告诉您函数的位置,但下面应该适合您。 If not, use the example above to fix your problem. 如果不是,请使用上面的示例解决您的问题。

public IEnumerator ReplaceCoroutine()
{
    // check if the piece has an animation attached
    Animator animator = GetComponent<Animator>();
    if (animator)
    {
        animator.Play(clearAnimation.name);
        yield return new WaitForSeconds(clearAnimation.length);
    }
}

IEnumerator playAll()
{
    for (int i = 0; i < 5; i++)
    {
        yield return pieces[i].StartCoroutine(ReplaceCoroutine());
    }
}

Then to call it, use StartCoroutine(playAll()); 然后调用它,使用StartCoroutine(playAll());

If you want to control each piece relative to the previous one (half the animation time as mentioned), you want your coroutine to yield after each piece operation but do it all in one single coroutine, not a separate coroutine for each piece. 如果要相对于上一个控制每个片段(如上所述的动画时间的一半),则希望协程每个片段操作产生但要在一个协程中完成,而不是为每个片段单独创建一个协程。

public IEnumerator ReplaceCoroutine()
{
     for(int i=0; i<pieces.Length; i++)
     {
         // check if the piece has an animation attached
         Animator animator = pieces[i].GetComponent<Animator>();
         if (animator!=null)
         {
            animator.Play(pieces[i].clearAnimation.name);
            yield return new WaitForSeconds(pieces[i].clearAnimation.length/2.0f);
         }
    }
}

void startAnimationSequence
{
    StartCoroutine(ReplaceCoroutine());
}

This will kick off each piece's animation sequentially and then wait for half the animation time before looping to the next. 这将依次开始每个片段的动画,然后等待一半的动画时间,然后循环到下一个。 Just call startAnimationSequence to start the loop. 只需调用startAnimationSequence即可开始循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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