Created
October 1, 2017 22:33
-
-
Save heybdj/f8c47b00aa2e4a6d647d3d903b71dd1c to your computer and use it in GitHub Desktop.
Set constant tangets on a Unity animation clip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This script is published under the CC0 1.0 Universal License | |
// https://creativecommons.org/publicdomain/zero/1.0/ | |
// To the extent possible under law, Spryly® Ltd. has waived all copyright | |
// and related or neighboring rights to this script. This work is published from: Canada. | |
using UnityEngine; | |
using UnityEditor; | |
using System.Collections; | |
public static class SetConstantTangents { | |
// Takes the selected animation clips and converts all keyframe tangents to constant / step tangents | |
[MenuItem ("Maple/Animation/Set Constant Tangents")] | |
static public void SetConstantTangentsOnAnimationClip() { | |
Object[] clips = Selection.GetFiltered( typeof( AnimationClip ), SelectionMode.TopLevel ); | |
for( int i = 0; i < clips.Length; ++i ) { | |
AnimationClip clip = clips[ i ] as AnimationClip; | |
EditorCurveBinding[] floatBindings = AnimationUtility.GetCurveBindings( clip ); | |
for( int j = 0; j < floatBindings.Length; ++j ) { | |
EditorCurveBinding binding = floatBindings[ j ]; | |
AnimationCurve curve = AnimationUtility.GetEditorCurve( clip, binding ); | |
for( int k = 0; k < curve.keys.Length; ++k ) { | |
AnimationUtility.SetKeyLeftTangentMode( curve, k, AnimationUtility.TangentMode.Constant ); | |
AnimationUtility.SetKeyRightTangentMode( curve, k, AnimationUtility.TangentMode.Constant ); | |
} | |
Undo.RecordObject( clip, "Set Constant Tangents" ); | |
clip.SetCurve( binding.path, typeof( Transform ), binding.propertyName, curve ); | |
} | |
} | |
} | |
} |
SetConstantTangents works the following way:
- Add SetConstantTangents.cs to the editor
- Impport Animation on prefab
- Resample curves = False
- Anim. Compression = Off
- Hit Apply
- Select all Animation Clips on the Project's Prefab
- Duplicate (Command + D)
- Add the new duplicates on a folder (to keep organized)
- Select them all and go to Tools > SelectConstantTangents
Once the operation is done, the selected Animation scripts should have Constant Tangents
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Life saver! <3