diff --git a/Unity Runtime Recorder/Scripts/UnityAnimSaver/Editor/UnityAnimationRecorderEditor.cs b/Unity Runtime Recorder/Scripts/UnityAnimSaver/Editor/UnityAnimationRecorderEditor.cs index 815ea91..aedd84b 100644 --- a/Unity Runtime Recorder/Scripts/UnityAnimSaver/Editor/UnityAnimationRecorderEditor.cs +++ b/Unity Runtime Recorder/Scripts/UnityAnimSaver/Editor/UnityAnimationRecorderEditor.cs @@ -3,92 +3,103 @@ using System.Collections; [CustomEditor(typeof(UnityAnimationRecorder))] -public class UnityAnimationRecorderEditor : Editor { +public class UnityAnimationRecorderEditor : Editor +{ - // save file path - SerializedProperty savePath; - SerializedProperty fileName; + // save file path + SerializedProperty savePath; + SerializedProperty fileName; - SerializedProperty startRecordKey; - SerializedProperty stopRecordKey; + SerializedProperty startRecordKey; + SerializedProperty stopRecordKey; - // options - SerializedProperty showLogGUI; - SerializedProperty recordLimitedFrames; - SerializedProperty recordFrames; + // options + SerializedProperty showLogGUI; + SerializedProperty recordLimitedFrames; + SerializedProperty recordFrames; - SerializedProperty changeTimeScale; - SerializedProperty timeScaleOnStart; - SerializedProperty timeScaleOnRecord; + SerializedProperty changeTimeScale; + SerializedProperty timeScaleOnStart; + SerializedProperty timeScaleOnRecord; + SerializedProperty frameDelta; - void OnEnable () { + void OnEnable() + { - savePath = serializedObject.FindProperty ("savePath"); - fileName = serializedObject.FindProperty ("fileName"); + savePath = serializedObject.FindProperty("savePath"); + fileName = serializedObject.FindProperty("fileName"); - startRecordKey = serializedObject.FindProperty ("startRecordKey"); - stopRecordKey = serializedObject.FindProperty ("stopRecordKey"); + startRecordKey = serializedObject.FindProperty("startRecordKey"); + stopRecordKey = serializedObject.FindProperty("stopRecordKey"); - showLogGUI = serializedObject.FindProperty ("showLogGUI"); - recordLimitedFrames = serializedObject.FindProperty ("recordLimitedFrames"); - recordFrames = serializedObject.FindProperty ("recordFrames"); + showLogGUI = serializedObject.FindProperty("showLogGUI"); + recordLimitedFrames = serializedObject.FindProperty("recordLimitedFrames"); + recordFrames = serializedObject.FindProperty("recordFrames"); - changeTimeScale = serializedObject.FindProperty ("changeTimeScale"); - timeScaleOnStart = serializedObject.FindProperty ("timeScaleOnStart"); - timeScaleOnRecord = serializedObject.FindProperty ("timeScaleOnRecord"); - - } + changeTimeScale = serializedObject.FindProperty("changeTimeScale"); + timeScaleOnStart = serializedObject.FindProperty("timeScaleOnStart"); + timeScaleOnRecord = serializedObject.FindProperty("timeScaleOnRecord"); + frameDelta = serializedObject.FindProperty("frameDelta"); + } - public override void OnInspectorGUI () { - serializedObject.Update (); + public override void OnInspectorGUI() + { + serializedObject.Update(); - EditorGUILayout.LabelField ("== Path Settings =="); + EditorGUILayout.LabelField("== Path Settings =="); - if (GUILayout.Button ("Set Save Path")) { - string defaultName = serializedObject.targetObject.name + "-Animation"; - string targetPath = EditorUtility.SaveFilePanelInProject ("Save Anim File To ..", defaultName, "", "please select a folder and enter the file name"); + if (GUILayout.Button("Set Save Path")) + { + string defaultName = serializedObject.targetObject.name + "-Animation.anim"; + string targetPath = EditorUtility.SaveFilePanelInProject("Save Anim File To ..", defaultName, "anim", "please select a folder and enter the file name"); - int lastIndex = targetPath.LastIndexOf ("/"); - savePath.stringValue = targetPath.Substring (0, lastIndex + 1); - string toFileName = targetPath.Substring (lastIndex + 1); + int lastIndex = targetPath.LastIndexOf("/"); + savePath.stringValue = targetPath.Substring(0, lastIndex + 1); + string toFileName = targetPath.Substring(lastIndex + 1); - fileName.stringValue = toFileName; - } - EditorGUILayout.PropertyField (savePath); - EditorGUILayout.PropertyField (fileName); + if (toFileName.IndexOf(".anim") < 0) + toFileName += ".anim"; + fileName.stringValue = toFileName; + } + EditorGUILayout.PropertyField(savePath); + EditorGUILayout.PropertyField(fileName); - EditorGUILayout.Space (); - // keys setting - EditorGUILayout.LabelField( "== Control Keys ==" ); - EditorGUILayout.PropertyField (startRecordKey); - EditorGUILayout.PropertyField (stopRecordKey); + EditorGUILayout.Space(); - EditorGUILayout.Space (); + // keys setting + EditorGUILayout.LabelField("== Control Keys =="); + EditorGUILayout.PropertyField(startRecordKey); + EditorGUILayout.PropertyField(stopRecordKey); - // Other Settings - EditorGUILayout.LabelField( "== Other Settings ==" ); - bool timeScaleOption = EditorGUILayout.Toggle ( "Change Time Scale", changeTimeScale.boolValue); - changeTimeScale.boolValue = timeScaleOption; + EditorGUILayout.Space(); - if (timeScaleOption) { - timeScaleOnStart.floatValue = EditorGUILayout.FloatField ("TimeScaleOnStart", timeScaleOnStart.floatValue); - timeScaleOnRecord.floatValue = EditorGUILayout.FloatField ("TimeScaleOnRecord", timeScaleOnRecord.floatValue); - } + // Other Settings + EditorGUILayout.LabelField("== Other Settings =="); + bool timeScaleOption = EditorGUILayout.Toggle("Change Time Scale", changeTimeScale.boolValue); + changeTimeScale.boolValue = timeScaleOption; - // gui log message - showLogGUI.boolValue = EditorGUILayout.Toggle ("Show Debug On GUI", showLogGUI.boolValue); + if (timeScaleOption) + { + timeScaleOnStart.floatValue = EditorGUILayout.FloatField("TimeScaleOnStart", timeScaleOnStart.floatValue); + timeScaleOnRecord.floatValue = EditorGUILayout.FloatField("TimeScaleOnRecord", timeScaleOnRecord.floatValue); + } - // recording frames setting - recordLimitedFrames.boolValue = EditorGUILayout.Toggle( "Record Limited Frames", recordLimitedFrames.boolValue ); + // gui log message + showLogGUI.boolValue = EditorGUILayout.Toggle("Show Debug On GUI", showLogGUI.boolValue); - if (recordLimitedFrames.boolValue) - EditorGUILayout.PropertyField (recordFrames); + // recording frames setting + recordLimitedFrames.boolValue = EditorGUILayout.Toggle("Record Limited Frames", recordLimitedFrames.boolValue); - serializedObject.ApplyModifiedProperties (); + if (recordLimitedFrames.boolValue) + EditorGUILayout.PropertyField(recordFrames); - //DrawDefaultInspector (); - } + frameDelta.floatValue = 1.0f / EditorGUILayout.FloatField("Frame Rate", 1.0f / frameDelta.floatValue); + + serializedObject.ApplyModifiedProperties(); + + //DrawDefaultInspector (); + } } diff --git a/Unity Runtime Recorder/Scripts/UnityAnimSaver/UnityAnimationRecorder.cs b/Unity Runtime Recorder/Scripts/UnityAnimSaver/UnityAnimationRecorder.cs index 26484d0..8bf0746 100644 --- a/Unity Runtime Recorder/Scripts/UnityAnimSaver/UnityAnimationRecorder.cs +++ b/Unity Runtime Recorder/Scripts/UnityAnimSaver/UnityAnimationRecorder.cs @@ -3,156 +3,174 @@ using UnityEditor; using System.Collections; -public class UnityAnimationRecorder : MonoBehaviour { +public class UnityAnimationRecorder : MonoBehaviour +{ + // save file path + public string savePath; + public string fileName; - // save file path - public string savePath; - public string fileName; - - // use it when save multiple files - int fileIndex = 0; - - public KeyCode startRecordKey = KeyCode.Q; - public KeyCode stopRecordKey = KeyCode.W; - - // options - public bool showLogGUI = false; - string logMessage = ""; - - public bool recordLimitedFrames = false; - public int recordFrames = 1000; - int frameIndex = 0; - - public bool changeTimeScale = false; - public float timeScaleOnStart = 0.0f; - public float timeScaleOnRecord = 1.0f; - - Transform[] recordObjs; - UnityObjectAnimation[] objRecorders; - - bool isStart = false; - float nowTime = 0.0f; - - // Use this for initialization - void Start () { - SetupRecorders (); - - } - - void SetupRecorders () { - recordObjs = gameObject.GetComponentsInChildren (); - objRecorders = new UnityObjectAnimation[recordObjs.Length]; - - frameIndex = 0; - nowTime = 0.0f; - - for (int i = 0; i < recordObjs.Length; i++) { - string path = AnimationRecorderHelper.GetTransformPathName (transform, recordObjs [i]); - objRecorders [i] = new UnityObjectAnimation ( path, recordObjs [i]); - } - - if (changeTimeScale) - Time.timeScale = timeScaleOnStart; - } - - // Update is called once per frame - void Update () { - - if (Input.GetKeyDown (startRecordKey)) { - StartRecording (); - } - - if (Input.GetKeyDown (stopRecordKey)) { - StopRecording (); - } - - if (isStart) { - nowTime += Time.deltaTime; - - for (int i = 0; i < objRecorders.Length; i++) { - objRecorders [i].AddFrame (nowTime); - } - } - - } - - public void StartRecording () { - CustomDebug ("Start Recorder"); - isStart = true; - Time.timeScale = timeScaleOnRecord; - } - - - public void StopRecording () { - CustomDebug ("End Record, generating .anim file"); - isStart = false; - - ExportAnimationClip (); - ResetRecorder (); - } - - void ResetRecorder () { - SetupRecorders (); - } - - - void FixedUpdate () { - - if (isStart) { - - if (frameIndex < recordFrames) { - for (int i = 0; i < objRecorders.Length; i++) { - objRecorders [i].AddFrame (nowTime); - } - - ++frameIndex; - } else { - isStart = false; - ExportAnimationClip (); - CustomDebug ("Recording Finish, generating .anim file"); - } - } - } - - void OnGUI () { - if (showLogGUI) - GUILayout.Label (logMessage); - } - - void ExportAnimationClip () { - - string exportFilePath = savePath + fileName; - - // if record multiple files when run - if (fileIndex != 0) - exportFilePath += "-" + fileIndex + ".anim"; - else - exportFilePath += ".anim"; - - - AnimationClip clip = new AnimationClip (); - clip.name = fileName; - - for (int i = 0; i < objRecorders.Length; i++) { - UnityCurveContainer[] curves = objRecorders [i].curves; - - for (int x = 0; x < curves.Length; x++) { - clip.SetCurve (objRecorders [i].pathName, typeof(Transform), curves [x].propertyName, curves [x].animCurve); - } - } - - clip.EnsureQuaternionContinuity (); - AssetDatabase.CreateAsset ( clip, exportFilePath ); - - CustomDebug (".anim file generated to " + exportFilePath); - fileIndex++; - } - - void CustomDebug ( string message ) { - if (showLogGUI) - logMessage = message; - else - Debug.Log (message); - } + public KeyCode startRecordKey = KeyCode.Q; + public KeyCode stopRecordKey = KeyCode.W; + + // options + public bool showLogGUI = false; + string logMessage = ""; + + public bool recordLimitedFrames = false; + public int recordFrames = 1000; + int frameIndex = 0; + + public bool changeTimeScale = false; + public float timeScaleOnStart = 0.0f; + public float timeScaleOnRecord = 1.0f; + public float frameDelta = 0.033f; + + Transform[] recordObjs; + UnityObjectAnimation[] objRecorders; + + bool isStart = false; + float nowTime = 0.0f; + private float timeDelta = 0.0f; + + // Use this for initialization + void Start() + { + recordObjs = gameObject.GetComponentsInChildren(); + objRecorders = new UnityObjectAnimation[recordObjs.Length]; + + for (int i = 0; i < recordObjs.Length; i++) + { + string path = AnimationRecorderHelper.GetTransformPathName(transform, recordObjs[i]); + objRecorders[i] = new UnityObjectAnimation(path, recordObjs[i]); + } + + if (changeTimeScale) + Time.timeScale = timeScaleOnStart; + } + + // Update is called once per frame + void Update() + { + + if (Input.GetKeyDown(startRecordKey)) + { + StartRecording(); + } + + if (Input.GetKeyDown(stopRecordKey)) + { + StopRecording(); + } + + if (isStart) + { + nowTime += Time.deltaTime; + timeDelta += Time.deltaTime; + + if (timeDelta < frameDelta) + { + return; + } + else + { + timeDelta = 0.0f; + } + + for (int i = 0; i < objRecorders.Length; i++) + { + objRecorders[i].AddFrame(nowTime); + } + } + + } + + public void StartRecording() + { + CustomDebug("Start Recorder"); + isStart = true; + Time.timeScale = timeScaleOnRecord; + } + + + public void StopRecording() + { + CustomDebug("End Record, generating .anim file"); + isStart = false; + + ExportAnimationClip(); + } + + + + + void FixedUpdate() + { + + if (isStart) + { + + if (timeDelta < frameDelta) + { + return; + } + else + { + timeDelta = 0.0f; + } + + if (frameIndex < recordFrames) + { + for (int i = 0; i < objRecorders.Length; i++) + { + objRecorders[i].AddFrame(nowTime); + } + + ++frameIndex; + } + else + { + isStart = false; + ExportAnimationClip(); + CustomDebug("Recording Finish, generating .anim file"); + } + } + } + + void OnGUI() + { + if (showLogGUI) + GUILayout.Label(logMessage); + } + + void ExportAnimationClip() + { + string exportFilePath = savePath + fileName; + + AnimationClip clip = new AnimationClip(); + clip.name = fileName; + + for (int i = 0; i < objRecorders.Length; i++) + { + UnityCurveContainer[] curves = objRecorders[i].curves; + + for (int x = 0; x < curves.Length; x++) + { + clip.SetCurve(objRecorders[i].pathName, typeof(Transform), curves[x].propertyName, curves[x].animCurve); + } + } + + clip.EnsureQuaternionContinuity(); + AssetDatabase.CreateAsset(clip, exportFilePath); + + CustomDebug(".anim file generated to " + exportFilePath); + } + + void CustomDebug(string message) + { + if (showLogGUI) + logMessage = message; + else + Debug.Log(message); + } } #endif \ No newline at end of file