修改动画分帧工具的bug

This commit is contained in:
shenjianxing 2025-01-09 16:32:17 +08:00
parent cff16c54cf
commit 9201c7343a

View File

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