Unity-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; } }
调用方式:
写了一个比较简单的脚本测试录制功能
大致逻辑是先调用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可以演示具体过程(录制时有些卡顿):
分类:
Unity3D
标签:
UnityAnimator
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
2015-01-02 C++一次指针问题和解决(char指针乱码)