当前位置: 首页 > news >正文

Unity3D之Mecanim动画系统学习笔记(十):Mecanim动画的资源加载相关

资源加载是必备的知识点,这里就说说Mecanim动画的资源如何打包及加载。

注意,Unity4.x和Unity5.x的AssetBundle打包策略不一样,本笔记是基于Unity4.x的AssetBundle进行打包的。

我们一般使用FBX类型的模型及动画文件,而动画文件的储存一般有两种情况,一是所有的动画和模型都一起存放到一个文件中,还有一种情况是模型单独一个文件而动画单独一个文件。这里我们就两种情况都看一下。

使用的资源是Unity3D自带的以及从一本教材中取出的两种类型的动画资源,同时需要对其动画创建对应的Animator Controller。

模型动画都存放在一个文件中的情况

一个FBX文件保存了模型、骨骼和动画,如下图:

下面是配置的Animator Controller:

需要注意的是,官方并没有提供为Animator设置Animator Controller的接口,所以我们必须将配置好的GameObject制作为一个预制件进行加载。

Resources加载

复制代码
 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class AllInOneResourcesLoad : MonoBehaviour
 5 {
 6     private Animator _animator;
 7 
 8     void Start()
 9     {
10         GameObject go = Resources.Load<GameObject>("AllInOne/ConstructorPrefab");
11 
12         GameObject man = Instantiate(go) as GameObject;
13         _animator = man.GetComponent<Animator>();
14     }
15     
16     void OnGUI()
17     {
18         if(GUI.Button(new Rect(0, 0, 100, 30), "idle"))
19         {
20             _animator.SetBool("walk", false);
21             _animator.SetBool("run", false);
22         }
23         if(GUI.Button(new Rect(100, 0, 100, 30), "walk"))
24         {
25             _animator.SetBool("walk", true);
26             _animator.SetBool("run", false);
27         }
28         if(GUI.Button(new Rect(200, 0, 100, 30), "run"))
29         {
30             _animator.SetBool("walk", false);
31             _animator.SetBool("run", true);
32         }
33         if(GUI.Button(new Rect(300, 0, 100, 30), "jump"))
34         {
35             _animator.SetTrigger("jump");
36         }
37     }
38 }
复制代码

AssetBundle加载

打包

复制代码
 1 using UnityEngine;
 2 using UnityEditor;
 3 
 4 public class CreateAllInOneAB
 5 {
 6     [MenuItem("Tool/CreateAllInOneAB")]
 7     private static void Create()
 8     {
 9         BuildPipeline.BuildAssetBundle(null, new[]
10             {
11                 AssetDatabase.LoadAssetAtPath("Assets/Resources/AllInOne/ConstructorPrefab.prefab", typeof(GameObject))
12             },
13             Application.streamingAssetsPath + "/AllInOne.assetbundle",
14             BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.UncompressedAssetBundle,
15             BuildTarget.StandaloneWindows64);
16     }
17 }
复制代码

加载

复制代码
 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class AllInOneAssetBundleLoad : MonoBehaviour
 5 {
 6     private Animator _animator;
 7 
 8     void Start()
 9     {
10         AssetBundle assetBundle = AssetBundle.CreateFromFile(Application.streamingAssetsPath + "/AllInOne.assetbundle");
11 
12         GameObject go = assetBundle.Load("ConstructorPrefab", typeof(GameObject)) as GameObject;
13 
14         GameObject man = Instantiate(go) as GameObject;
15         _animator = man.GetComponent<Animator>();
16     }
17 
18     void OnGUI()
19     {
20         if(GUI.Button(new Rect(0, 0, 100, 30), "idle"))
21         {
22             _animator.SetBool("walk", false);
23             _animator.SetBool("run", false);
24         }
25         if (GUI.Button(new Rect(100, 0, 100, 30), "walk"))
26         {
27             _animator.SetBool("walk", true);
28             _animator.SetBool("run", false);
29         }
30         if (GUI.Button(new Rect(200, 0, 100, 30), "run"))
31         {
32             _animator.SetBool("walk", false);
33             _animator.SetBool("run", true);
34         }
35         if (GUI.Button(new Rect(300, 0, 100, 30), "jump"))
36         {
37             _animator.SetTrigger("jump");
38         }
39     }
40 }
复制代码

模型动画分开存放的情况

还有一种情况是模型和动画是分为多个FBX文件存放的,比如下面是模型文件:

虽然有一个Take 001的动画,但是实际上我们并不使用该动画,而是使用下面仅保存了动画的FBX文件:

下面是配置的Animator Controller:

除了没有提供设置Animator Controller的接口,也无法在运行时对动画剪辑进行增加删除的操作,所以我们一般打包时就收集所有的依赖项一起打包,归根结底还是只需要一个制作好的预制件即可。

从这个角度看,其实是否将动画进行拆分最终的使用方式都是一样的。

Resources加载

复制代码
 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class ResourcesLoad : MonoBehaviour
 5 {
 6     private Animator _animator;
 7 
 8     void Start()
 9     {
10         GameObject go = Resources.Load<GameObject>("ZombieNurse/ZombieNursePrefab");
11 
12         GameObject man = Instantiate(go) as GameObject;
13         _animator = man.GetComponent<Animator>();
14     }
15 
16     void OnGUI()
17     {
18         if(GUI.Button(new Rect(0, 0, 100, 30), "idle"))
19         {
20             _animator.SetBool("run", false);
21         }
22         if(GUI.Button(new Rect(100, 0, 100, 30), "run"))
23         {
24             _animator.SetBool("run", true);
25         }
26         if(GUI.Button(new Rect(200, 0, 100, 30), "attack"))
27         {
28             _animator.SetTrigger("attack");
29         }
30         if(GUI.Button(new Rect(300, 0, 100, 30), "dead"))
31         {
32             _animator.SetTrigger("dead");
33         }
34     }
35 }
复制代码

AssetBundle加载

打包

复制代码
 1 using UnityEditor;
 2 using UnityEngine;
 3 
 4 public class CreateAB
 5 {
 6     [MenuItem("Tool/CreateAB")]
 7     private static void Create()
 8     {
 9         BuildPipeline.BuildAssetBundle(null, new[]
10             {
11                 AssetDatabase.LoadAssetAtPath("Assets/Resources/ZombieNurse/ZombieNursePrefab.prefab", typeof(GameObject))
12             },
13             Application.streamingAssetsPath + "/AB.assetbundle",
14             BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.UncompressedAssetBundle,
15             BuildTarget.StandaloneWindows64);
16     }
17 }
复制代码

加载

复制代码
 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class AssetBundleLoad : MonoBehaviour
 5 {
 6     private Animator _animator;
 7 
 8     void Start()
 9     {
10         AssetBundle assetBundle = AssetBundle.CreateFromFile(Application.streamingAssetsPath + "/AB.assetbundle");
11 
12         GameObject go = assetBundle.Load("ZombieNursePrefab", typeof(GameObject)) as GameObject;
13 
14         GameObject man = Instantiate(go) as GameObject;
15         _animator = man.GetComponent<Animator>();
16     }
17 
18     void OnGUI()
19     {
20         if(GUI.Button(new Rect(0, 0, 100, 30), "idle"))
21         {
22             _animator.SetBool("run", false);
23         }
24         if(GUI.Button(new Rect(100, 0, 100, 30), "run"))
25         {
26             _animator.SetBool("run", true);
27         }
28         if(GUI.Button(new Rect(200, 0, 100, 30), "attack"))
29         {
30             _animator.SetTrigger("attack");
31         }
32         if(GUI.Button(new Rect(300, 0, 100, 30), "dead"))
33         {
34             _animator.SetTrigger("dead");
35         }
36     }
37 }
复制代码

转载于:https://www.cnblogs.com/lancidie/p/7345157.html

相关文章:

  • Android零基础入门第11节:简单几步带你飞,运行Android Studio工程
  • java中你确定用对单例了吗?
  • 深入分析Sleep(0)与Sleep(1)的区别
  • swift3.0常用操作包含删除字符串(string),更换字符串,插入字符串
  • 第21章 RTX 低功耗之睡眠模式
  • Spring思维导图(AOP篇)
  • layer close 关闭层IE9-浏览器崩溃问题解决
  • Java-static
  • 克隆用户过狗提权
  • Linux中如何查看显卡硬件信息
  • 搭建本地yum源服务器
  • Linux下NMAP常用扫描简介(一)
  • 利用Sympy计算sin1°的最小多项式
  • sql server2008 在window2012 R2安装集群注意事项
  • IBM“绿色地平线”为中网加油 大数据技术助力全民健身
  • 03Go 类型总结
  • HTTP请求重发
  • JavaScript 一些 DOM 的知识点
  • Javascript 原型链
  • js写一个简单的选项卡
  • text-decoration与color属性
  • Zepto.js源码学习之二
  • 初识 webpack
  • 搞机器学习要哪些技能
  • 全栈开发——Linux
  • 如何在 Tornado 中实现 Middleware
  • 找一份好的前端工作,起点很重要
  • ###51单片机学习(2)-----如何通过C语言运用延时函数设计LED流水灯
  • (2)STM32单片机上位机
  • (Matlab)使用竞争神经网络实现数据聚类
  • (安全基本功)磁盘MBR,分区表,活动分区,引导扇区。。。详解与区别
  • (动手学习深度学习)第13章 计算机视觉---微调
  • (四) Graphivz 颜色选择
  • (一)基于IDEA的JAVA基础1
  • (转)JAVA中的堆栈
  • .NET 中什么样的类是可使用 await 异步等待的?
  • .Net+SQL Server企业应用性能优化笔记4——精确查找瓶颈
  • .net和php怎么连接,php和apache之间如何连接
  • .NET学习教程二——.net基础定义+VS常用设置
  • @angular/cli项目构建--http(2)
  • @entity 不限字节长度的类型_一文读懂Redis常见对象类型的底层数据结构
  • @NestedConfigurationProperty 注解用法
  • @Pointcut 使用
  • @transaction 提交事务_【读源码】剖析TCCTransaction事务提交实现细节
  • [20171113]修改表结构删除列相关问题4.txt
  • [C++]18:set和map的使用
  • [C++基础]-入门知识
  • [CTSC2014]企鹅QQ
  • [Docker]三.Docker 部署nginx,以及映射端口,挂载数据卷
  • [FZSZOJ 1223] 上海红茶馆
  • [GXYCTF2019]BabySQli1
  • [hdu 3065] 病毒侵袭持续中 [AC自动机] [病毒特征码匹配]
  • [JavaScript]_[初级]_[关于forin或for...in循环语句的用法]
  • [JS] 常用正则表达式集(一)
  • [Linux] - 定时任务crontab