Last active
December 6, 2023 20:09
-
-
Save Maoyeedy/756d993b533e50265df4b581b238a39f to your computer and use it in GitHub Desktop.
[Unity Editor Script] Rename Mixamo animation clips from "mixamo.com" to .fbx filename
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 System.Collections.Generic; | |
using System.IO; | |
using UnityEditor; | |
using UnityEngine; | |
public class AnimationRenamingProcessor : AssetPostprocessor | |
{ | |
// Include any AnimationClip names here that should be automatically replaced | |
private static readonly HashSet<string> NamesToReplace = new() | |
{ | |
"mixamo.com" | |
}; | |
private string NewName => Path.GetFileNameWithoutExtension(context.assetPath); | |
private void OnPostprocessModel(GameObject gameObject) | |
{ | |
if (assetImporter is not ModelImporter modelImporter) return; | |
ModelImporterClipAnimation[] clipAnimations = modelImporter.clipAnimations; | |
if (clipAnimations.Length < 1) | |
{ | |
// Try to use the existing clipAnimations if they exist to preserve other settings (e.g. AnimationEvents), | |
// but fallback to creating new clipAnimations from the defaultClipAnimations | |
clipAnimations = modelImporter.defaultClipAnimations; | |
} | |
bool isDirty = false; | |
foreach (ModelImporterClipAnimation clipAnimation in clipAnimations) | |
{ | |
if (NamesToReplace.Contains(clipAnimation.name)) | |
{ | |
clipAnimation.name = NewName; | |
isDirty = true; | |
} | |
} | |
if (isDirty) // To prevent needlessly setting new clipAnimations in case of any side effects | |
{ | |
modelImporter.clipAnimations = clipAnimations; | |
} | |
} | |
private void OnPostprocessAnimation(GameObject root, AnimationClip clip) | |
{ | |
if (NamesToReplace.Contains(clip.name)) | |
{ | |
clip.name = NewName; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment