Unity Animator Controller相关脚本集

这篇博客介绍了在Unity中使用Animator Controller进行动画管理的一些实用脚本,包括自动拆分FBX动画、导出独立Animation Clip以及动态创建和修改Animator Controller。通过脚本自动化处理,提高了美术资源管理的效率和预览的便利性。
摘要由CSDN通过智能技术生成

新项目中使用了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
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值