Unity-Animator深入系列---錄制與回放


回到 Animator深入系列總目錄

 

 

Animator自帶了簡單的動畫錄制,回放功能。但可惜的是不支持持久化的數據輸出。因而不能作為錄像保存

不過這種可以作為競速,格斗類游戲在結束時經常出現的游戲回放,還是比較有用的

 


 

 

測試所用腳本

using UnityEngine;

public class AnimatorRecordingExample : MonoBehaviour
{
    public Animator animator;
    bool mIsStartPlayback;
    float mTime;


    void Update()
    {
        if (mIsStartPlayback)
        {
            mTime += Time.deltaTime;
            if (animator.recorderStopTime > mTime)
            {
                animator.playbackTime = mTime;
                Debug.Log("animator.playbackTime: " + animator.playbackTime);
            }
        }
        else
        {
            Debug.Log("animator.recorderStartTime " + animator.recorderStartTime);
            Debug.Log("animator.recorderStopTime: " + animator.recorderStopTime);
        }
    }

    [ContextMenu("StartRecording")]
    void StartRecording()
    {
        animator.StartRecording(0);
    }

    [ContextMenu("StopRecording")]
    void StopRecording()
    {
        animator.StopRecording();
    }

    [ContextMenu("StartPlayback")]
    void StartPlayback()
    {
        animator.StartPlayback();
        mTime = animator.recorderStartTime;
        mIsStartPlayback = true;
    }

    [ContextMenu("StopPlayback")]
    void StopPlayback()
    {
        animator.StopPlayback();
        mIsStartPlayback = false;
    }
}
AnimatorRecordingExample

 

 

調用方式:

 

 

 

寫了一個比較簡單的腳本測試錄制功能

大致邏輯是先調用StartRecording進行錄制,結束時調用StopRecording

然后再需要時進行回放,需要注意調用StartPlayback開始回放之后,回放的時間需要手動更新

每一幀的更新值可以使用DeltaTime,而開始值可以用animator.recorderStartTime

 

這時,還需要判斷playback的時間是否大於錄制結束時間,否則會有警告:

Animator Recorder does not have recorded data at given time, Animator will update based on current AnimatorParameters

 

還需要注意兩點

1.animator.StartRecording(...)的參數如果小於1,會被判定為不限時間錄制。

2.非Animator驅動的位移,都會被錄制進去。由於Animator的更新時間是在Update之后,LateUpdate之前。

所以移動控制寫在LateUpdate里的時候,在回播時會有操作沖突

 


 

 

下面這個gif可以演示具體過程(錄制時有些卡頓):

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM