Created
July 5, 2020 05:59
-
-
Save Pikachuxxxx/8101da6d14a5afde80c7c180e3a43644 to your computer and use it in GitHub Desktop.
A Script To Reverse Your 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
using UnityEditor; | |
using UnityEngine; | |
using System.IO; | |
public static class ReverseAnimationContext | |
{ | |
[MenuItem("Assets/Create Reversed Clip", false, 14)] | |
private static void ReverseClip() | |
{ | |
string directoryPath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(Selection.activeObject)); | |
string fileName = Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)); | |
string fileExtension = Path.GetExtension(AssetDatabase.GetAssetPath(Selection.activeObject)); | |
fileName = fileName.Split('.')[0]; | |
string copiedFilePath = directoryPath + Path.DirectorySeparatorChar + fileName + "_Reversed" + fileExtension; | |
var clip = GetSelectedClip(); | |
AssetDatabase.CopyAsset(AssetDatabase.GetAssetPath(Selection.activeObject), copiedFilePath); | |
clip = (AnimationClip)AssetDatabase.LoadAssetAtPath(copiedFilePath, typeof(AnimationClip)); | |
if (clip == null) | |
return; | |
float clipLength = clip.length; | |
var curves = AnimationUtility.GetAllCurves(clip, true); | |
clip.ClearCurves(); | |
foreach (AnimationClipCurveData curve in curves) | |
{ | |
var keys = curve.curve.keys; | |
int keyCount = keys.Length; | |
var postWrapmode = curve.curve.postWrapMode; | |
curve.curve.postWrapMode = curve.curve.preWrapMode; | |
curve.curve.preWrapMode = postWrapmode; | |
for (int i = 0; i < keyCount; i++) | |
{ | |
Keyframe K = keys[i]; | |
K.time = clipLength - K.time; | |
var tmp = -K.inTangent; | |
K.inTangent = -K.outTangent; | |
K.outTangent = tmp; | |
keys[i] = K; | |
} | |
curve.curve.keys = keys; | |
clip.SetCurve(curve.path, curve.type, curve.propertyName, curve.curve); | |
} | |
var events = AnimationUtility.GetAnimationEvents(clip); | |
if (events.Length > 0) | |
{ | |
for (int i = 0; i < events.Length; i++) | |
{ | |
events[i].time = clipLength - events[i].time; | |
} | |
AnimationUtility.SetAnimationEvents(clip, events); | |
} | |
Debug.Log("Animation reversed!"); | |
} | |
[MenuItem("Assets/Create Reversed Clip", true)] | |
static bool ReverseClipValidation() | |
{ | |
return Selection.activeObject.GetType() == typeof(AnimationClip); | |
} | |
public static AnimationClip GetSelectedClip() | |
{ | |
var clips = Selection.GetFiltered(typeof(AnimationClip), SelectionMode.Assets); | |
if (clips.Length > 0) | |
{ | |
return clips[0] as AnimationClip; | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Pikachuxxxx,
This is an excellent and very helpful tool for reversing animations in Unity - Thank you so much for your contribution!
Unfortunately, the method "AnimationUtility.GetAllCurves()" is obsolete. I adapted your script to use what Unity suggests in its stead. I tried to give you as much credit as possible, because I really didn't do much!
You can find the adapted script here: https://gist.github.com/c1aea8eb4cb95862809fe41489b57257.git
Hope this helps and thanks again for taking the time to make this post a while back!
Best Regards,
Brandon Lyman