using UnityEngine; using UnityEditor; using System.Collections; using System.IO; using System.Text.RegularExpressions; public class FbxAnimListPostprocessor : MonoBehaviour { [MenuItem("Assets/SplitAnimNew")] public static void SplitAnim() { UnityEngine.Object obj = Selection.activeObject; if (null != obj) { string assetPath = AssetDatabase.GetAssetPath(obj); SplitAnim(assetPath); } } [MenuItem("Assets/SplitAnimFloder")] public static void SetAplitAnim() { Object[] selectedObjects = Selection.GetFiltered(SelectionMode.DeepAssets); foreach (Object obj in selectedObjects) { if (obj != null) { string path = AssetDatabase.GetAssetPath(obj); if (Directory.Exists(path)) { EnableFBXReadWriteInFolder(path); } else if (path.EndsWith(".fbx", System.StringComparison.OrdinalIgnoreCase)) { EnableFBXReadWriteForFile(path); } } } } public static void EnableFBXReadWriteInFolder(string folderPath) { string[] files = Directory.GetFiles(folderPath, "*.fbx", SearchOption.AllDirectories); foreach (string file in files) { EnableFBXReadWriteForFile(file); } } public static void EnableFBXReadWriteForFile(string filePath) { string relativePath = filePath; SplitAnim(filePath, true); } public static void SplitAnim(string assetPath, bool autoImport = false) { try { string fileAnim; fileAnim = assetPath; string ClipText = Path.ChangeExtension(fileAnim, ".txt"); StreamReader file = new StreamReader(ClipText); string sAnimList = file.ReadToEnd(); file.Close(); // if (autoImport) { System.Collections.ArrayList List = new ArrayList(); ParseAnimFile(sAnimList, ref List); ModelImporter modelImporter = ModelImporter.GetAtPath(assetPath) as ModelImporter; modelImporter.animationType = ModelImporterAnimationType.Legacy; //modelImporter.clipAnimations. = true; modelImporter.clipAnimations = (ModelImporterClipAnimation[]) List.ToArray(typeof(ModelImporterClipAnimation)); AssetDatabase.ImportAsset(assetPath); } else { if (EditorUtility.DisplayDialog("FBX Animation Import from file", fileAnim, "Import", "Cancel")) { System.Collections.ArrayList List = new ArrayList(); ParseAnimFile(sAnimList, ref List); ModelImporter modelImporter = ModelImporter.GetAtPath(assetPath) as ModelImporter; modelImporter.animationType = ModelImporterAnimationType.Legacy; //modelImporter.clipAnimations. = true; modelImporter.clipAnimations = (ModelImporterClipAnimation[]) List.ToArray(typeof(ModelImporterClipAnimation)); AssetDatabase.ImportAsset(assetPath); EditorUtility.DisplayDialog("导入成功", "Number of imported clips: " + modelImporter.clipAnimations.GetLength(0).ToString(), "OK"); } } } catch { } // (Exception e) { EditorUtility.DisplayDialog("Imported animations", e.Message, "OK"); } } static void ParseAnimFile(string sAnimList, ref System.Collections.ArrayList List) { Regex regexString = new Regex(" *(?[0-9]+) *- *(?[0-9]+) *(?(loop|noloop| )) *(?[^\r^\n]*[^\r^\n^ ])", RegexOptions.Compiled | RegexOptions.ExplicitCapture); Match match = regexString.Match(sAnimList, 0); while (match.Success) { ModelImporterClipAnimation clip = new ModelImporterClipAnimation(); if (match.Groups["firstFrame"].Success) { clip.firstFrame = System.Convert.ToInt32(match.Groups["firstFrame"].Value, 10); } if (match.Groups["lastFrame"].Success) { clip.lastFrame = System.Convert.ToInt32(match.Groups["lastFrame"].Value, 10); } if (match.Groups["loop"].Success) { if (match.Groups["loop"].Value == "loop") { clip.loop = true; clip.wrapMode = WrapMode.Loop; } else { clip.loop = false; } } if (match.Groups["name"].Success) { clip.name = match.Groups["name"].Value; } List.Add(clip); match = regexString.Match(sAnimList, match.Index + match.Length); } } }