zoukankan      html  css  js  c++  java
  • Unity导入FBX自动进行动画切分

    手动处理动画分割

    在导入FBX模型过程中,若带有动画呢,需要对它进行切分。

    当然这个工作可以在Unity中完成。

    比如:

    这样手动来分割进行。

    自动动画切分

     
    这就需要代码了。
    把代码保存成cs文件,然后放在Editor文件夹中。若没有此文件夹,就自己创建一个!
     
     
    代码如下:
    [csharp] view plain copy
     
     print?
    1. // FbxAnimListPostprocessor.cs : Use an external text file to import a list of   
    2. // splitted animations for FBX 3D models.  
    3. //  
    4. // Put this script in your "Assets/Editor" directory. When Importing or   
    5. // Reimporting a FBX file, the script will search a text file with the   
    6. // same name and the ".txt" extension.  
    7. // File format: one line per animation clip "firstFrame-lastFrame loopFlag animationName"  
    8. // The keyworks "loop" or "noloop" are optional.  
    9. // Example:  
    10. // 0-50 loop Move forward  
    11. // 100-190 die  
    12.   
    13. using UnityEngine;  
    14. using UnityEditor;  
    15. using System.Collections;  
    16. using System.IO;  
    17. using System.Text.RegularExpressions;  
    18. using System;  
    19. using System.IO;  
    20.    
    21.    
    22. public class FbxAnimListPostprocessor : AssetPostprocessor  
    23. {  
    24.     public void OnPreprocessModel()  
    25.     {  
    26.         if (Path.GetExtension(assetPath).ToLower() == ".fbx"  
    27.             && !assetPath.Contains("@"))  
    28.         {  
    29.             try  
    30.             {  
    31.                 string fileAnim;  
    32.                 if (DragAndDrop.paths.Length <= 0)  
    33.                 {  
    34.                     return;  
    35.                 }  
    36.                 fileAnim = DragAndDrop.paths[0];  
    37.                 string ClipText = Path.ChangeExtension(fileAnim, ".txt");  
    38.                 StreamReader file = new StreamReader(ClipText);  
    39.                 string sAnimList = file.ReadToEnd();  
    40.                 file.Close();  
    41.                 //  
    42.                 if (EditorUtility.DisplayDialog("FBX Animation Import from file",  
    43.                     fileAnim, "Import", "Cancel"))  
    44.                 {  
    45.                     System.Collections.ArrayList List = new ArrayList();  
    46.                     ParseAnimFile(sAnimList, ref List);  
    47.   
    48.                     ModelImporter modelImporter = assetImporter as ModelImporter;  
    49.                     //modelImporter.clipAnimations. = true;  
    50.                     modelImporter.clipAnimations = (ModelImporterClipAnimation[])  
    51.                         List.ToArray(typeof(ModelImporterClipAnimation));  
    52.   
    53.                     EditorUtility.DisplayDialog("Imported animations",  
    54.                         "Number of imported clips: "  
    55.                         + modelImporter.clipAnimations.GetLength(0).ToString(), "OK");  
    56.                 }  
    57.             }  
    58.             catch { }  
    59.             // (Exception e) { EditorUtility.DisplayDialog("Imported animations", e.Message, "OK"); }  
    60.         }  
    61.     }  
    62.   
    63.     void ParseAnimFile(string sAnimList, ref System.Collections.ArrayList List)  
    64.     {  
    65.         Regex regexString = new Regex(" *(?<firstFrame>[0-9]+) *- *(?<lastFrame>[0-9]+) *(?<loop>(loop|noloop| )) *(?<name>[^ ^ ]*[^ ^ ^ ])",  
    66.             RegexOptions.Compiled | RegexOptions.ExplicitCapture);  
    67.   
    68.         Match match = regexString.Match(sAnimList, 0);  
    69.         while (match.Success)  
    70.         {  
    71.             ModelImporterClipAnimation clip = new ModelImporterClipAnimation();  
    72.   
    73.             if (match.Groups["firstFrame"].Success)  
    74.             {  
    75.                 clip.firstFrame = System.Convert.ToInt32(match.Groups["firstFrame"].Value, 10);  
    76.             }  
    77.             if (match.Groups["lastFrame"].Success)  
    78.             {  
    79.                 clip.lastFrame = System.Convert.ToInt32(match.Groups["lastFrame"].Value, 10);  
    80.             }  
    81.             if (match.Groups["loop"].Success)  
    82.             {  
    83.                 clip.loop = match.Groups["loop"].Value == "loop";  
    84.             }  
    85.             if (match.Groups["name"].Success)  
    86.             {  
    87.                 clip.name = match.Groups["name"].Value;  
    88.             }  
    89.   
    90.             List.Add(clip);  
    91.   
    92.             match = regexString.Match(sAnimList, match.Index + match.Length);  
    93.         }  
    94.     }  
    95. }  


    怎么使用呢?
    在你的FBX同目录文件夹下,创建一个txt文件,名字与FBX文件同名即可。
     
    txt内容,为 每个动画的起始帧和结束帧,是否循环播放,和帧名。
    CowGirl_Ani.txt
    [html] view plain copy
     
     print?
    1. 0-50 loop Move forward  
    2. 100-190 die  


     
     
    把FBX文件拖入到Unity的资源中,可以看到弹出对话框。选择Import导入,就会自动弹出分割动画的数量。
    若点击cancle ,就会直接导入动画,而不会分割动画。
  • 相关阅读:
    被下属骂,记一次矛盾升级——有心无心,蝴蝶效应?
    技术管理进阶——团队合并、解散怎么办?
    “技术转产品”比产品更恶心的几个点
    javaScript系列 [43]TS、Class and ES5
    javaScript系列 [42]node中 require函数的加载过程
    javaScript系列 [52]模板引擎的实现逻辑
    Base64简单介绍
    异或运算(XOR)
    javaScript系列 [51]Rollup 打包器
    javaScript系列 [49] ast && render
  • 原文地址:https://www.cnblogs.com/chenliyang/p/6558471.html
Copyright © 2011-2022 走看看