您的位置:首页 > 移动开发 > Unity3D

Unity 读取配置文件 自动切割 Animations 分段

2016-09-26 19:05 513 查看
网上有些资源,是没有分割动画的。就是整的一个 Take01 转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn


我们可以点 小加号 ,来增加一个动画,并设置起始帧 和 结束帧。
但是这样做,数据是保存在 Library 文件夹的,而我们一般都是不把 Library 上传到 SVN的。

所以这个数据就会丢失。
转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn
那我们就把这个数据写到 配置文件中,然后在代码中读取出来,自动切割动画。

下面是脚本:
/************************************************
* 文件名:FBXPostprocessor.cs
* 描述:FBX处理 自动分割
* 创建人:陈鹏
* 创建日期:20160926
* http://blog.csdn.net/huutu/article/details/52672911 * ************************************************/

using UnityEditor;
using UnityEngine;
using System.IO;
using System.Collections.Generic;
using System;

class FBXPostprocessor : AssetPostprocessor
{
// This method is called just before importing an FBX.
void OnPreprocessModel()
{
Debug.Log("OnPreprocessModel " + assetPath);

ModelImporter mi = (ModelImporter) assetImporter;

//clips txt
TextAsset clipsAsset = (TextAsset) AssetDatabase.LoadAssetAtPath(assetPath.Substring(0, assetPath.LastIndexOf('.')) + "_clips.txt", typeof(TextAsset));
if(clipsAsset != null)
{
List<ModelImporterClipAnimation> anims = new List<ModelImporterClipAnimation>();

string[] clipsText = clipsAsset.text.Replace("\r\n", "\n").Split('\n');
foreach(string c in clipsText)
{
string[] vs = c.Split(' ');
if(vs.Length >= 3)
{
string name = vs[0];
string begin = vs[1];
string end = vs[2];

ModelImporterClipAnimation clip = new ModelImporterClipAnimation();
clip.name = name;
clip.firstFrame = System.Convert.ToInt32(begin);
clip.lastFrame = System.Convert.ToInt32(end);
if(name == "idle" || name == "walk" || name == "run")
{
clip.loop = true;
clip.loopPose = true;
}

anims.Add(clip);
}
}

mi.clipAnimations = anims.ToArray();
}
}
}
放到 Editor 目录,然后编写配置文件如下:



Reimport Fbx 文件即可



重新导入完成后,就自动切割了动画转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn


示例工程: http://pan.baidu.com/s/1hsrz5Z2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: