zoukankan      html  css  js  c++  java
  • 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可以演示具体过程(录制时有些卡顿):

  • 相关阅读:
    C# 判断系统空闲(键盘、鼠标不操作一段时间)
    c#做动态(gif)中文验证码
    vue 移动端公众号采坑经验
    git常用命令汇总
    关于面试的相关问题
    jQuery请求后台接口
    谈谈前端行业一两点
    JQuery获取Dom元素的方法
    Vue--props
    函数的上下文就是函数里面的this是谁
  • 原文地址:https://www.cnblogs.com/hont/p/5095237.html
Copyright © 2011-2022 走看看