From 4cb397d57575c700733db816016ec14d3d77094b Mon Sep 17 00:00:00 2001 From: wszczepan Date: Fri, 22 Jul 2016 13:40:57 +0200 Subject: [PATCH 1/2] I created option after if eneabled it will smooth tangents of all animation curves in the animation. I't makes animation look much better eaven with small numbers of keyframes --- .../Editor/UnityAnimationRecorderEditor.cs | 12 ++++++++---- .../Scripts/UnityAnimSaver/UnityAnimationRecorder.cs | 10 +++++++++- .../Scripts/UnityAnimSaver/UnityCurveContainer.cs | 7 +++++++ .../Scripts/UnityAnimSaver/UnityObjectAnimation.cs | 7 +++++++ 4 files changed, 31 insertions(+), 5 deletions(-) diff --git a/Unity Runtime Recorder/Scripts/UnityAnimSaver/Editor/UnityAnimationRecorderEditor.cs b/Unity Runtime Recorder/Scripts/UnityAnimSaver/Editor/UnityAnimationRecorderEditor.cs index f2b2b87..05c5490 100644 --- a/Unity Runtime Recorder/Scripts/UnityAnimSaver/Editor/UnityAnimationRecorderEditor.cs +++ b/Unity Runtime Recorder/Scripts/UnityAnimSaver/Editor/UnityAnimationRecorderEditor.cs @@ -20,9 +20,10 @@ public class UnityAnimationRecorderEditor : Editor { SerializedProperty changeTimeScale; SerializedProperty timeScaleOnStart; SerializedProperty timeScaleOnRecord; + SerializedProperty smoothTangents; - void OnEnable () { + void OnEnable () { savePath = serializedObject.FindProperty ("savePath"); fileName = serializedObject.FindProperty ("fileName"); @@ -37,8 +38,9 @@ void OnEnable () { changeTimeScale = serializedObject.FindProperty ("changeTimeScale"); timeScaleOnStart = serializedObject.FindProperty ("timeScaleOnStart"); timeScaleOnRecord = serializedObject.FindProperty ("timeScaleOnRecord"); - - } + smoothTangents = serializedObject.FindProperty("smoothTangents"); + + } public override void OnInspectorGUI () { serializedObject.Update (); @@ -87,7 +89,9 @@ public override void OnInspectorGUI () { // recording frames setting recordLimitedFrames.boolValue = EditorGUILayout.Toggle( "Record Limited Frames", recordLimitedFrames.boolValue ); - if (recordLimitedFrames.boolValue) + smoothTangents.boolValue = EditorGUILayout.Toggle("Smooth tangents of animation curves", smoothTangents.boolValue); + + if (recordLimitedFrames.boolValue) EditorGUILayout.PropertyField (recordFrames); serializedObject.ApplyModifiedProperties (); diff --git a/Unity Runtime Recorder/Scripts/UnityAnimSaver/UnityAnimationRecorder.cs b/Unity Runtime Recorder/Scripts/UnityAnimSaver/UnityAnimationRecorder.cs index 7459ec2..cbd4c27 100644 --- a/Unity Runtime Recorder/Scripts/UnityAnimSaver/UnityAnimationRecorder.cs +++ b/Unity Runtime Recorder/Scripts/UnityAnimSaver/UnityAnimationRecorder.cs @@ -22,6 +22,7 @@ public class UnityAnimationRecorder : MonoBehaviour { public bool changeTimeScale = false; public float timeScaleOnStart = 0.0f; public float timeScaleOnRecord = 1.0f; + public bool smoothTangents = true; Transform[] recordObjs; UnityObjectAnimation[] objRecorders; @@ -107,7 +108,14 @@ void OnGUI () { void ExportAnimationClip () { string exportFilePath = savePath + fileName; - + if(smoothTangents) + { + for (int i = 0; i < objRecorders.Length; i++) + { + objRecorders[i].smoothCurves(); + + } + } AnimationClip clip = new AnimationClip (); clip.name = fileName; diff --git a/Unity Runtime Recorder/Scripts/UnityAnimSaver/UnityCurveContainer.cs b/Unity Runtime Recorder/Scripts/UnityAnimSaver/UnityCurveContainer.cs index f80a652..ae12235 100644 --- a/Unity Runtime Recorder/Scripts/UnityAnimSaver/UnityCurveContainer.cs +++ b/Unity Runtime Recorder/Scripts/UnityAnimSaver/UnityCurveContainer.cs @@ -16,4 +16,11 @@ public void AddValue( float animTime, float animValue ) Keyframe key = new Keyframe (animTime, animValue, 0.0f, 0.0f); animCurve.AddKey (key); } + public void SmoothTangents() + { + for (int i = 0; i < animCurve.keys.Length; ++i) + { + animCurve.SmoothTangents(i, 0); //zero weight means average + } + } } diff --git a/Unity Runtime Recorder/Scripts/UnityAnimSaver/UnityObjectAnimation.cs b/Unity Runtime Recorder/Scripts/UnityAnimSaver/UnityObjectAnimation.cs index e67ffa0..f328095 100644 --- a/Unity Runtime Recorder/Scripts/UnityAnimSaver/UnityObjectAnimation.cs +++ b/Unity Runtime Recorder/Scripts/UnityAnimSaver/UnityObjectAnimation.cs @@ -44,4 +44,11 @@ public void AddFrame ( float time ) { curves [9].AddValue (time, observeGameObject.localScale.z); } + public void smoothCurves() + { + for (int i = 0; i < curves.Length; i++) + { + curves[i].SmoothTangents(); + } + } } From 662e35645fff915179712c3f7ed41e1113fce789 Mon Sep 17 00:00:00 2001 From: wszczepan Date: Fri, 22 Jul 2016 13:59:32 +0200 Subject: [PATCH 2/2] In this commit i created an option which allow you to record frames in some period of time. combined with previous commit which made animation curves smoother it creates oportunity to create realy small animation file sizes on simpler animations. --- .../Editor/UnityAnimationRecorderEditor.cs | 12 ++++++ .../UnityAnimSaver/UnityAnimationRecorder.cs | 37 +++++++++++-------- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/Unity Runtime Recorder/Scripts/UnityAnimSaver/Editor/UnityAnimationRecorderEditor.cs b/Unity Runtime Recorder/Scripts/UnityAnimSaver/Editor/UnityAnimationRecorderEditor.cs index 05c5490..87cdd3e 100644 --- a/Unity Runtime Recorder/Scripts/UnityAnimSaver/Editor/UnityAnimationRecorderEditor.cs +++ b/Unity Runtime Recorder/Scripts/UnityAnimSaver/Editor/UnityAnimationRecorderEditor.cs @@ -21,6 +21,8 @@ public class UnityAnimationRecorderEditor : Editor { SerializedProperty timeScaleOnStart; SerializedProperty timeScaleOnRecord; SerializedProperty smoothTangents; + SerializedProperty recordEachFrame; + SerializedProperty howOftenFrame; void OnEnable () { @@ -40,6 +42,9 @@ void OnEnable () { timeScaleOnRecord = serializedObject.FindProperty ("timeScaleOnRecord"); smoothTangents = serializedObject.FindProperty("smoothTangents"); + recordEachFrame = serializedObject.FindProperty("recordEachFrame"); + howOftenFrame = serializedObject.FindProperty("howOftenFrame"); + } public override void OnInspectorGUI () { @@ -91,6 +96,13 @@ public override void OnInspectorGUI () { smoothTangents.boolValue = EditorGUILayout.Toggle("Smooth tangents of animation curves", smoothTangents.boolValue); + recordEachFrame.boolValue = EditorGUILayout.Toggle("Record animation in every frame", recordEachFrame.boolValue); + if (!recordEachFrame.boolValue) + { + howOftenFrame.floatValue = EditorGUILayout.FloatField("Distance betwen recorded frames", howOftenFrame.floatValue); + + } + if (recordLimitedFrames.boolValue) EditorGUILayout.PropertyField (recordFrames); diff --git a/Unity Runtime Recorder/Scripts/UnityAnimSaver/UnityAnimationRecorder.cs b/Unity Runtime Recorder/Scripts/UnityAnimSaver/UnityAnimationRecorder.cs index cbd4c27..0495c86 100644 --- a/Unity Runtime Recorder/Scripts/UnityAnimSaver/UnityAnimationRecorder.cs +++ b/Unity Runtime Recorder/Scripts/UnityAnimSaver/UnityAnimationRecorder.cs @@ -55,13 +55,7 @@ void Update () { StopRecording (); } - if (isStart) { - nowTime += Time.deltaTime; - - for (int i = 0; i < objRecorders.Length; i++) { - objRecorders [i].AddFrame (nowTime); - } - } + } @@ -79,19 +73,30 @@ public void StopRecording () { ExportAnimationClip (); } - - - - void FixedUpdate () { + + public bool recordEachFrame = true; + + public float howOftenFrame = 0.2f; + + float lastTime = 0f; + void FixedUpdate () { if (isStart) { if (frameIndex < recordFrames) { - for (int i = 0; i < objRecorders.Length; i++) { - objRecorders [i].AddFrame (nowTime); - } - - ++frameIndex; + + nowTime += Time.fixedDeltaTime; + if (lastTime==0|| nowTime > lastTime + howOftenFrame||recordEachFrame) + { + lastTime = nowTime; + + for (int i = 0; i < objRecorders.Length; i++) + { + objRecorders[i].AddFrame(nowTime); + } + + ++frameIndex; + } } else { isStart = false; ExportAnimationClip ();