• QQ
  • nahooten@sina.com
  • 常州市九洲新世界花苑15-2

游戏开发

常州手游开发-ModelImporter在代码中添加动画片段

原创内容,转载请注明原文网址:http://homeqin.cn/a/wenzhangboke/jishutiandi/youxikaifa/2019/0129/364.html

Unity3D使用心得:ModelImporter的使用、在代码中添加动画片段。

 

在使用 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 }



上篇:上一篇:常州微信小程序开发培训:canvas手绘雷达图
下篇:下一篇:常州微信游戏开发-U3d 动态下载动画资源Animatio