Skip to content

Instantly share code, notes, and snippets.

@vertxxyz
Last active January 9, 2020 06:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vertxxyz/a22468aaf81a673d7a007aa0a2f625c8 to your computer and use it in GitHub Desktop.
Save vertxxyz/a22468aaf81a673d7a007aa0a2f625c8 to your computer and use it in GitHub Desktop.
Extremely quick and definitely not perfect recorder
#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.Animations;
using UnityEngine;
using UnityEngine.Playables;
namespace Vertx
{
public class QuickRecorder : MonoBehaviour
{
public PlayableDirector PlayableDirector;
public int FrameRate = 30;
public float EndTime = 10;
private float endTimeUsing;
private float recordingingDelta;
float time;
private GameObjectRecorder recorder;
private bool firstRecording;
void Start()
{
PlayableDirector.timeUpdateMode = DirectorUpdateMode.Manual;
PlayableDirector.playOnAwake = false;
recordingingDelta = 1 / (float) FrameRate;
recorder = new GameObjectRecorder(gameObject);
endTimeUsing = EndTime + float.Epsilon;
}
void Update()
{
recorder.BindComponent(transform);
PlayableDirector.time = time;
PlayableDirector.Evaluate();
}
private void LateUpdate()
{
recorder.TakeSnapshot(firstRecording ? 0 : recordingingDelta);
firstRecording = false;
time += recordingingDelta;
if (time <= endTimeUsing)
return;
enabled = false;
AnimationClip clip = new AnimationClip();
#if UNITY_2020_1_OR_NEWER
var curveFilterOptions = new CurveFilterOptions {keyframeReduction = false};
recorder.SaveToClip(clip, FrameRate, curveFilterOptions);
#else
recorder.SaveToClip(clip, FrameRate);
#endif
SaveAndWriteFileDialog($"{gameObject.name}", clip);
EditorApplication.isPlaying = false;
}
private const string extension = "anim";
static bool SaveAndWriteFileDialog(string fileName, AnimationClip clip)
{
string path = EditorUtility.SaveFilePanel("Save", Application.dataPath, fileName, extension);
if (string.IsNullOrEmpty(path))
return false;
path = path.Replace(Application.dataPath, string.Empty);
path = $"Assets{path}";
AssetDatabase.CreateAsset(clip, path);
AssetDatabase.ImportAsset(path);
EditorGUIUtility.PingObject(clip);
return true;
}
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment