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();
} [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可以演示具体过程(录制时有些卡顿):