Unity编辑器 - Rigidbody动力学Bake到AnimationClip

Unity文档移动平台优化部分提到Physics对CPU的消耗较大

将动力学的特效如破碎等Bake成动画也是优化性能的手段之一,在Unity中可以很方便的将刚体动力学录制成AnimationClip。

Unity编辑器 - Rigidbody动力学Bake到AnimationClip-LMLPHP

using UnityEditor.Experimental.Animations;
using UnityEngine;
using UnityEditor; public class TransformAnimRecorder : MonoBehaviour {
private AnimationClip clip;
//停止录制时保存Clip
public bool record = true; private GameObjectRecorder _recorder; void Start() {
clip = new AnimationClip();
_recorder = new GameObjectRecorder();
_recorder.root = gameObject;
_recorder.BindComponent<Transform>(gameObject, true);
} void LateUpdate() {
if (clip == null)
return; if (record) {
_recorder.TakeSnapshot(Time.deltaTime);
}
else if (_recorder.isRecording) {
_recorder.SaveToClip(clip);
_recorder.ResetRecording();
AssetDatabase.CreateAsset(clip, "Assets/RecordTest/testClip.anim");
}
}
}
05-11 04:09