Unity导入FBX自动进行动画切分
2017-03-16 11:16
603 查看
手动处理动画分割
在导入FBX模型过程中,若带有动画呢,需要对它进行切分。当然这个工作可以在Unity中完成。
比如:
这样手动来分割进行。
自动动画切分
这就需要代码了。把代码保存成cs文件,然后放在Editor文件夹中。若没有此文件夹,就自己创建一个!
代码如下:
[csharp] view
plain copy
print?
// FbxAnimListPostprocessor.cs : Use an external text file to import a list of
// splitted animations for FBX 3D models.
//
// Put this script in your "Assets/Editor" directory. When Importing or
// Reimporting a FBX file, the script will search a text file with the
// same name and the ".txt" extension.
// File format: one line per animation clip "firstFrame-lastFrame loopFlag animationName"
// The keyworks "loop" or "noloop" are optional.
// Example:
// 0-50 loop Move forward
// 100-190 die
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.IO;
using System.Text.RegularExpressions;
using System;
using System.IO;
public class FbxAnimListPostprocessor : AssetPostprocessor
{
public void OnPreprocessModel()
{
if (Path.GetExtension(assetPath).ToLower() == ".fbx"
&& !assetPath.Contains("@"))
{
try
{
string fileAnim;
if (DragAndDrop.paths.Length <= 0)
{
return;
}
fileAnim = DragAndDrop.paths[0];
string ClipText = Path.ChangeExtension(fileAnim, ".txt");
StreamReader file = new StreamReader(ClipText);
string sAnimList = file.ReadToEnd();
file.Close();
//
if (EditorUtility.DisplayDialog("FBX Animation Import from file",
fileAnim, "Import", "Cancel"))
{
System.Collections.ArrayList List = new ArrayList();
ParseAnimFile(sAnimList, ref List);
ModelImporter modelImporter = assetImporter as ModelImporter;
//modelImporter.clipAnimations. = true;
modelImporter.clipAnimations = (ModelImporterClipAnimation[])
List.ToArray(typeof(ModelImporterClipAnimation));
EditorUtility.DisplayDialog("Imported animations",
"Number of imported clips: "
+ modelImporter.clipAnimations.GetLength(0).ToString(), "OK");
}
}
catch { }
// (Exception e) { EditorUtility.DisplayDialog("Imported animations", e.Message, "OK"); }
}
}
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^ ])",
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)
{
clip.loop = match.Groups["loop"].Value == "loop";
}
if (match.Groups["name"].Success)
{
clip.name = match.Groups["name"].Value;
}
List.Add(clip);
match = regexString.Match(sAnimList, match.Index + match.Length);
}
}
}
怎么使用呢?
在你的FBX同目录文件夹下,创建一个txt文件,名字与FBX文件同名即可。
txt内容,为 每个动画的起始帧和结束帧,是否循环播放,和帧名。
CowGirl_Ani.txt
[html] view
plain copy
print?
0-50 loop Move forward
100-190 die
把FBX文件拖入到Unity的资源中,可以看到弹出对话框。选择Import导入,就会自动弹出分割动画的数量。
若点击cancle ,就会直接导入动画,而不会分割动画。
相关文章推荐
- Unity导入FBX自动进行动画切分
- Unity导入FBX自动进行动画切分
- Unity导入FBX自动进行动画切分
- unity 3.5以上导入fbx无法播放动画
- Unity进阶 - 动画系统 - 人形动画的导入
- 3dsMax导出到unity的fbx文件丢失动画
- [导入]自定义IHttpHandlerFactory使用Unity对ASP.NET Webform页面进行依赖注入
- 【Unity&图片】图片自动批导入问题
- 在Unity中修改Fbx中的动画文件
- 【Unity】10.2 通用动画的导入和设置
- 【Unity】10.1 类人动画的导入和设置
- Unity进阶 - 动画系统 - 导入设置
- [Material Design] MaterialButton 效果进阶 动画自动移动进行对齐效果
- 关于在3dmax 导入再导出FBX到unity中的尺寸选项问题
- (插播)关于使用jenkins + unity +Xcode 来进行自动打包的处理。
- Unity导入FBX文件部分模型丢失材质的BUG
- 导入fbx人物动画read-only解决方案
- [Unity菜鸟] FBX模型动画提取
- 将ogre的模型导入到blender中, 导出为fbx,并导入到 unity 中
- Unity进阶 - 动画系统 - 导入设置