• International中文
  • SetAnimation會殘留上個動畫的最後一幀才接著播放新動畫

我使用一個SkeletonGraphic,預設是不顯示,當要播放動畫時才出現,播放完之後又隱藏
由於參考了類似問題的處理方式,目前的程式碼範例如下:

void Update()
{
    if (shouldPlayAnim && !isBusy)
    {
        isBusy = true;
        m_SkeletonGraphic.gameObject.SetActive(true);
        m_SkeletonGraphic.AnimationState.SetEmptyAnimation(0, 0);
        var entry = m_SkeletonGraphic.AnimationState.SetAnimation(0, "anim", false);
        m_SkeletonGraphic.AnimationState.Update(0);
        m_SkeletonGraphic.Update(0);
        entry.Complete += (entry) =>
        {
            isBusy = false;
            m_SkeletonGraphic.gameObject.SetActive(false);
        };
    }
}

第一次播放沒問題,問題是第二次播放之後都會先看到上次動畫的最後一幀才接著演出所指定的動畫
隱藏的手段我試過SetActive、enabled、localScale=0等作法,結果都一樣
這邊搜尋到不少同樣的問題,而我照做卻無法解決,請問是哪邊弄錯了呢?
使用的版本是spine-unity 4.2

Related Discussions
...

@kaworucloud In your code you are calling AnimationState.SetEmptyAnimation() after SetAnimation when run the second time, which will perform mixing-out over at least one frame instead of instantly transitioning to your new animation. So you would want to use the following instead:

m_SkeletonGraphic.AnimationState.ClearTrack(0);

You might want to call this after SetActive(false); in your entry.Complete callback delegate.

謝謝。改成這樣之後似乎符合預期了

void Update()
{
    if (shouldPlayAnim && !isBusy)
    {
        isBusy = true;
        m_SkeletonGraphic.enabled = true;
        var entry = m_SkeletonGraphic.AnimationState.SetAnimation(0, "anim", false);
        m_SkeletonGraphic.AnimationState.Update(0);
        m_SkeletonGraphic.Update(0);
        entry.Complete += (entry) =>
        {
            isBusy = false;
            m_SkeletonGraphic.enabled = false;
            m_SkeletonGraphic.AnimationState.ClearTrack(0);
        };
    }
}

關於SetAnimation之後的兩行Update(0)也都是必要的嗎?
還是加上ClearTrack(0)之後就可以移除其中一行或兩行了?

@kaworucloud Glad it helped.

You can definitely remove the following redundant line:

m_SkeletonGraphic.AnimationState.Update(0);

This is because the line m_SkeletonGraphic.Update(0); below calls AnimationState.Update(0) automatically.

Whether the line m_SkeletonGraphic.Update(0); can be removed as well depends on your script's script execution order, when your script's Update method is called. If your script is executed before SkeletonGraphic, it is safe to remove m_SkeletonGraphic.Update(0); because it will be called in the same frame after your script anyway. If your script executes after SkeletonGraphic, you have to keep the call to see the effect in the same frame.

See this documentation section on script execution order.