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

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 }

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: