private void Rename() { List <string> lstPaths = new List <string>(); List <DirectoryInfo> lstDir = new List <DirectoryInfo>(); lstDir.Add(new DirectoryInfo(PathHelper.AssetsPath2ABSPath(animresPath))); if (lstDir == null) { Log.e("未选中文件夹!"); return; } List <FileInfo> lstFile = new List <FileInfo>(); for (int i = 0; i < lstDir.Count; i++) { FileInfo[] files = lstDir[i].GetFiles("*", SearchOption.AllDirectories); if (files == null || files.Length == 0) { return; } for (int j = 0; j < files.Length; j++) { if (!files[j].FullName.Contains(".meta")) { lstFile.Add(files[j]); } } if (lstFile.Count == 0) { return; } for (int k = 0; k < lstFile.Count; k++) { if (isAvailable(lstFile[k].FullName)) { ShowProgress(k / lstFile.Count, lstFile.Count, k, lstFile[k].Name); string assetPath = PathHelper.ABSPath2AssetsPath(lstFile[k].FullName); ModelImporter modelImporter = AssetImporter.GetAtPath(assetPath) as ModelImporter; switch (animType) { case AnimType.Legacy: modelImporter.animationType = ModelImporterAnimationType.Legacy; break; case AnimType.Generic: modelImporter.animationType = ModelImporterAnimationType.Generic; modelImporter.autoGenerateAvatarMappingIfUnspecified = true; break; case AnimType.Humanoid: modelImporter.animationType = ModelImporterAnimationType.Human; // modelImporter.autoGenerateAvatarMappingIfUnspecified = true; if (avatar == null) { modelImporter.autoGenerateAvatarMappingIfUnspecified = true; } else { modelImporter.sourceAvatar = avatar; } break; } var clips = modelImporter.defaultClipAnimations; ModelImporterClipAnimation[] newClips = new ModelImporterClipAnimation[clips.Length]; for (int n = 0; n < clips.Length; n++) { ModelImporterClipAnimation clip = clips[i]; clips[i].name = RenameClip(lstFile[k].Name); clips[i].loopTime = isLoopAnim(clips[i].name); clip.lockRootHeightY = true; clip.lockRootPositionXZ = true; clip.lockRootRotation = true; clips[i].keepOriginalOrientation = true; clips[i].keepOriginalPositionXZ = true; clips[i].keepOriginalPositionY = true; newClips[n] = clip; } modelImporter.clipAnimations = newClips; modelImporter.SaveAndReimport(); } } } EditorUtility.ClearProgressBar(); AssetDatabase.Refresh(); }
static void GetAnmationClips(Avatar avatar, string resPath, string outputPath, AnimType animType) { List <string> lstPaths = new List <string>(); List <DirectoryInfo> lstDir = new List <DirectoryInfo>(); lstDir.Add(new DirectoryInfo(PathHelper.AssetsPath2ABSPath(resPath))); if (lstDir == null) { Log.e("未选中文件夹!"); return; } string absPath = PathHelper.ABSPath2AssetsPath(outputPath); if (!Directory.Exists(absPath)) { Log.i("不存在文件夹,自动生成"); Directory.CreateDirectory(absPath); } List <FileInfo> lstFile = new List <FileInfo>(); for (int i = 0; i < lstDir.Count; i++) { FileInfo[] files = lstDir[i].GetFiles("*", SearchOption.AllDirectories); if (files == null || files.Length == 0) { return; } for (int j = 0; j < files.Length; j++) { if (!files[j].FullName.Contains(".meta")) { lstFile.Add(files[j]); } } if (lstFile.Count == 0) { return; } for (int k = 0; k < lstFile.Count; k++) { if (isAvailable(lstFile[k].FullName)) { ShowProgress(k / lstFile.Count, lstFile.Count, k, lstFile[k].Name); string assetPath = PathHelper.ABSPath2AssetsPath(lstFile[k].FullName); ModelImporter modelImporter = AssetImporter.GetAtPath(assetPath) as ModelImporter; switch (animType) { case AnimType.Legacy: modelImporter.animationType = ModelImporterAnimationType.Legacy; break; case AnimType.Generic: modelImporter.animationType = ModelImporterAnimationType.Generic; modelImporter.autoGenerateAvatarMappingIfUnspecified = true; break; case AnimType.Humanoid: modelImporter.animationType = ModelImporterAnimationType.Human; // modelImporter.autoGenerateAvatarMappingIfUnspecified = true; if (avatar == null) { modelImporter.autoGenerateAvatarMappingIfUnspecified = true; } else { modelImporter.sourceAvatar = avatar; } break; } modelImporter.SaveAndReimport(); AnimationClip newClip = new AnimationClip(); var obj = AssetDatabase.LoadAssetAtPath <AnimationClip>(assetPath); EditorUtility.CopySerialized(AssetDatabase.LoadAssetAtPath <AnimationClip>(assetPath), newClip); newClip.name = RenameAnim(lstFile[k].Name, animType); AnimationClipSettings settings = AnimationUtility.GetAnimationClipSettings(newClip); settings.loopTime = isLoopAnim(newClip.name); settings.keepOriginalOrientation = true; settings.keepOriginalPositionXZ = true; settings.keepOriginalPositionY = true; settings.loopBlendOrientation = true; settings.loopBlendPositionXZ = true; settings.loopBlendPositionY = true; AnimationUtility.SetAnimationClipSettings(newClip, settings); string newClipName = PathHelper.FileNameWithoutSuffix(newClip.name); AssetDatabase.CreateAsset(newClip, outputPath + newClipName + ".anim"); AssetDatabase.SaveAssets(); } } EditorUtility.ClearProgressBar(); AssetDatabase.Refresh(); } }