(转)Unity3d使用心得(1):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 }

复制代码

转载于:https://www.cnblogs.com/hisiqi/p/3202686.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity使用代码实现点击按钮时,停止Animator的动画可以通过以下步骤实现: 1. 首先在Unity编辑器需要停止动画的对象,然后在Inspector面板找到Animator组件。 2. 在Animator组件,找到需要停止的动画片,然后记录下该动画片的名称。 3. 在代码获取需要停止动画的对象,可以使用GameObject.Find()或者是通过Inspector面板拖拽方式获取对象。 4. 在获取到对象之后,可以使用GetComponent<Animator>()方法获取Animator组件,然后使用SetTrigger()方法触发需要停止的动画片。 5. 在代码定义一个事件方法,用于处理按钮点击事件。在该方法,可以使用Animator.Stop()方法停止当前播放的动画。 下面是一个示例代码: ```csharp using UnityEngine; using UnityEngine.UI; public class StopAnimation : MonoBehaviour { public Button stopButton; public GameObject targetObject; public string animationName; private Animator animator; void Start() { animator = targetObject.GetComponent<Animator>(); stopButton.onClick.AddListener(OnStopButtonClick); } void OnStopButtonClick() { animator.SetTrigger(animationName); } void AnimationStop() { animator.Stop(); } } ``` 在这个示例,我们使用了一个Button组件来触发事件,获取了需要停止动画的对象和动画片名称。我们在Start()方法获取了Animator组件,并且在OnStopButtonClick()方法触发了需要停止的动画片。在AnimationStop()方法,我们使用了Animator.Stop()方法来停止当前播放的动画。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值