Skip to content

Instantly share code, notes, and snippets.

@Maoyeedy
Last active December 6, 2023 20:09
Show Gist options
  • Save Maoyeedy/756d993b533e50265df4b581b238a39f to your computer and use it in GitHub Desktop.
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
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