新项目中使用了unity的动画控制器animator,写了以下几个小脚本。
1.导入fbx并拆分其中的动画,修改fbx导入设置。
2.导出fbx中的动画到指定目录,生成独立的Animation Clip.
3.动态创建及修改Animator Controller.
1.美术把fbx提交svn的同时配上一个txt,里面指定哪一帧到哪一帧为哪个动作。脚本根据这个配置去拆分动作,并保存fbx.这样就不需要美术同学手动去拆分动作,而且可以在unity中方便地预览指定的动作。
code:
fbx_bone_1001.txt
idle 80 140 loop
fire 330 340 loop
walk 1850 1886 loop
run 430 446 loop
dead 680 724
attack 1290 1320
FbxAnimListPostprocessor.cs
// 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:
// idle 80 140 loop
// dead 680 724
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.IO;
using System.Text.RegularExpressions;
using System;
using UnityEditor.Animations;
public class FbxAnimListPostprocessor : AssetPostprocessor
{
public void OnPreprocessModel()
{
if (Path.GetExtension(assetPath).ToLower() == ".fbx"
&& !assetPath.Contains("@"))
{
try
{
// Remove 6 chars because dataPath and assetPath both contain "assets" directory
string fileAnim = Application.dataPath + Path.ChangeExtension(assetPath, ".txt").Substring(6);
StreamReader file = new StreamReader(fileAnim);
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.splitAnimations = true;
modelImporter.clipAnimations = (ModelImporterClipAnimation[])
List.ToArray(typeof(ModelImporterClipAnimation));
// 根据项目需要可选
/* modelImporter.motionNodeName = "<Root Transform>";
modelImporter.importMaterials = false;
modelImporter.animationRotationError = 0.5f;
modelImporter.animationPositionError = 0.1f;
modelImporter.animationScaleError = 0