Unity3d使用心得(1):ModelImporter的使用、在代码中添加动画片段。
2016-03-05 16:34
519 查看
http://www.cnblogs.com/dongliang/archive/2012/09/25/ModelImporter.html
在使用 Unity3d 倒入Fbx模型的时候,动画的动画片段需要自己手动添加模型多了以后会是一个不小的工作量。
Unity3d支持 编辑器脚本来控制资源导入的过程。添加一个 AssetPostprocessor 监听其中的 OnPreprocessModel 方法,在其中使用 ModelImporter 的 clipAnimations 属性来为导入的动画添加动画片段。
我的项目中美术给的模型中,按类型划分,每一个类型都有一套动画。我是采用的方法是 分别将不同类型的模型放置到不同的文件夹,通过路径来判断应该添加什么样的动画片段。这里如果你的项目中实现了Unity3d中读取策划填写的表格的话其实也是可以的。这里就不展开了。
ModelImporter 的 clipAnimations 属性 接收的是一个定长的数组。这里我封装了一个管理器类用于提供一个更简洁、代码更少的方法创建该数组。
完整代码如下:
1 using UnityEngine;
2using System.Collections;
3 using UnityEditor;
4 using System.Collections.Generic;
5
6 public class AnimModelSet : AssetPostprocessor
7 {
8 void OnPreprocessModel()
9 {
10
11 if (assetPath.Contains("FirstPlayers"))
12 {
13 ModelImporter textureImporter = assetImporter as ModelImporter;
14 editorImporterUtil.clipArrayListCreater creater = new editorImporterUtil.clipArrayListCreater();
15 creater.addClip("idle", 0, 50, true, WrapMode.Loop);
16 textureImporter.clipAnimations = creater.getArray();
17 }
18 }
19 }
20
21 namespace editorImporterUtil
22 {
23 public class clipArrayListCreater
24 {
25 private List<ModelImporterClipAnimation> clipList = new List<ModelImporterClipAnimation>();
26 public void addClip(string name, int firstFrame, int lastFrame, bool loop, WrapMode wrapMode)
27 {
28 ModelImporterClipAnimation tempClip = new ModelImporterClipAnimation();
29 tempClip.name = name;
30 tempClip.firstFrame = firstFrame;
31 tempClip.lastFrame = lastFrame;
32 tempClip.loop = loop;
33 tempClip.wrapMode = wrapMode;
34 clipList.Add(tempClip);
35 }
36
37 public ModelImporterClipAnimation[] getArray()
38 {
39 return clipList.ToArray();
40 }
41 }
42
43 }
在使用 Unity3d 倒入Fbx模型的时候,动画的动画片段需要自己手动添加模型多了以后会是一个不小的工作量。
Unity3d支持 编辑器脚本来控制资源导入的过程。添加一个 AssetPostprocessor 监听其中的 OnPreprocessModel 方法,在其中使用 ModelImporter 的 clipAnimations 属性来为导入的动画添加动画片段。
我的项目中美术给的模型中,按类型划分,每一个类型都有一套动画。我是采用的方法是 分别将不同类型的模型放置到不同的文件夹,通过路径来判断应该添加什么样的动画片段。这里如果你的项目中实现了Unity3d中读取策划填写的表格的话其实也是可以的。这里就不展开了。
ModelImporter 的 clipAnimations 属性 接收的是一个定长的数组。这里我封装了一个管理器类用于提供一个更简洁、代码更少的方法创建该数组。
完整代码如下:
1 using UnityEngine;
2using System.Collections;
3 using UnityEditor;
4 using System.Collections.Generic;
5
6 public class AnimModelSet : AssetPostprocessor
7 {
8 void OnPreprocessModel()
9 {
10
11 if (assetPath.Contains("FirstPlayers"))
12 {
13 ModelImporter textureImporter = assetImporter as ModelImporter;
14 editorImporterUtil.clipArrayListCreater creater = new editorImporterUtil.clipArrayListCreater();
15 creater.addClip("idle", 0, 50, true, WrapMode.Loop);
16 textureImporter.clipAnimations = creater.getArray();
17 }
18 }
19 }
20
21 namespace editorImporterUtil
22 {
23 public class clipArrayListCreater
24 {
25 private List<ModelImporterClipAnimation> clipList = new List<ModelImporterClipAnimation>();
26 public void addClip(string name, int firstFrame, int lastFrame, bool loop, WrapMode wrapMode)
27 {
28 ModelImporterClipAnimation tempClip = new ModelImporterClipAnimation();
29 tempClip.name = name;
30 tempClip.firstFrame = firstFrame;
31 tempClip.lastFrame = lastFrame;
32 tempClip.loop = loop;
33 tempClip.wrapMode = wrapMode;
34 clipList.Add(tempClip);
35 }
36
37 public ModelImporterClipAnimation[] getArray()
38 {
39 return clipList.ToArray();
40 }
41 }
42
43 }
相关文章推荐
- 整理Unity 编辑器的相关知识
- unity www缓存图片
- 菜鸟学习 - Unity中的热更新 - LuaInterface用户指南
- Unity3d AR 增强现实技术列表(2016年3月31日更新)
- 给unity3D中的GameObject挂载脚本代码
- unity3d事件函数整理,事件,回调函数,消息处理
- Unity 摄像机旋转初探
- [Unity热更新]更新lua脚本 (二)
- Unity-飞行射击实例的代码
- Unity协程(Coroutine)
- Unity多语言本地化改进版
- Unity多语言本地化改进版
- Unity Shader 学习开发
- Unity shader 官网文档全方位学习一
- 记一次基于Unity的Profiler性能分析
- Unity3D中实现帧同步 - Part 2
- unity, SkinnedMeshRenderer.bones[i]不能直接赋值
- Unity3D中实现帧同步 - Part 1
- unity3d触摸屏手势控制镜头旋转与缩放
- Unity安卓上播放视频的问题,暂时无解记录一下