簡體   English   中英

統一-一次播放動畫

[英]unity - play animations one at a time

我有一個協程函數,看起來像這樣:

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

我用如下循環激活它:

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

問題在於循環一次全部完成,並且所有動畫同時進行。 我想確保,例如,件[2]僅在件[1]達到動畫時間的一半時才開始他的動畫協程。

可能嗎?

謝謝

你必須打電話給你

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

來自另一個協程函數,然后等待每個協程函數返回。 您可以通過yielding StartCoroutine函數調用來實現。 下面的代碼將調用yourCoroutineFunction函數,然后等待其完成,然后再運行下一個循環。

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

我不能告訴您函數的位置,但下面應該適合您。 如果不是,請使用上面的示例解決您的問題。

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

然后調用它,使用StartCoroutine(playAll());

如果要相對於上一個控制每個片段(如上所述的動畫時間的一半),則希望協程每個片段操作產生但要在一個協程中完成,而不是為每個片段單獨創建一個協程。

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

這將依次開始每個片段的動畫,然后等待一半的動畫時間,然后循環到下一個。 只需調用startAnimationSequence即可開始循環。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM