如何通过脚本批量设置Unity导入动画的Loop Time属性?
批量设置Unity动画导入参数的解决方案
当然可以通过编辑器脚本实现批量修改,Unity提供了ModelImporter和ModelImporterClipAnimation API直接操控动画导入设置,以下是具体实现方案:
实现步骤
- 创建Editor脚本
在项目中新建一个名为Editor的文件夹(若没有则创建),在其中添加如下C#脚本:
using UnityEditor; using UnityEngine; public class AnimationBatchTool : EditorWindow { [MenuItem("Tools/Batch Animation Settings")] public static void OpenToolWindow() { GetWindow<AnimationBatchTool>("批量动画设置"); } private bool enableLoopTime = true; private bool enableBakeRotation = true; private void OnGUI() { GUILayout.Label("批量动画导入参数设置", EditorStyles.boldLabel); enableLoopTime = EditorGUILayout.Toggle("启用 Loop Time", enableLoopTime); enableBakeRotation = EditorGUILayout.Toggle("启用 Bake Rotation into Pose", enableBakeRotation); if (GUILayout.Button("应用到选中资产", GUILayout.Height(30))) { ProcessSelectedAssets(); } } private void ProcessSelectedAssets() { var selectedObjects = Selection.GetFiltered<Object>(SelectionMode.Assets); int updatedCount = 0; foreach (var obj in selectedObjects) { string assetPath = AssetDatabase.GetAssetPath(obj); ModelImporter importer = AssetImporter.GetAtPath(assetPath) as ModelImporter; // 跳过非模型资产(比如单独的动画剪辑文件) if (importer == null) continue; ModelImporterClipAnimation[] clipSettings = importer.clipAnimations; bool needsUpdate = false; // 遍历每个动画剪辑的设置 foreach (var clip in clipSettings) { if (enableLoopTime && clip.loopTime != true) { clip.loopTime = true; needsUpdate = true; } if (enableBakeRotation && clip.bakeRotationIntoPose != true) { clip.bakeRotationIntoPose = true; needsUpdate = true; } } // 有修改才保存并重新导入 if (needsUpdate) { importer.clipAnimations = clipSettings; EditorUtility.SetDirty(importer); importer.SaveAndReimport(); updatedCount++; } } Debug.Log($"已完成{updatedCount}个资产的动画参数批量修改"); } }
- 使用方式
- 保存脚本后,在Unity顶部菜单点击
Tools->Batch Animation Settings打开工具窗口 - 在Project窗口选中所有需要修改的模型资产(.fbx/.obj等包含动画的文件)
- 在工具窗口勾选需要开启的参数选项,点击「应用到选中资产」即可完成批量修改
额外说明
- 若需针对特定动画剪辑修改(比如只修改名为"Walk"的动画),可在遍历
clipSettings时加入名称判断:if (clip.name == "Walk") { // 执行修改逻辑 } - 修改操作会触发资产重新导入,处理大量大体积模型时可能需要等待一段时间
- 该脚本仅在编辑器环境生效,不会打包到最终游戏中
内容的提问来源于stack exchange,提问作者sina5an
相关产品推荐
相关产品推荐

