Unity3D之Mecanim动画系统学习笔记(十):Mecanim动画的资源加载相关
资源加载是必备的知识点,这里就说说Mecanim动画的资源如何打包及加载。
注意,Unity4.x和Unity5.x的AssetBundle打包策略不一样,本笔记是基于Unity4.x的AssetBundle进行打包的。
我们一般使用FBX类型的模型及动画文件,而动画文件的储存一般有两种情况,一是所有的动画和模型都一起存放到一个文件中,还有一种情况是模型单独一个文件而动画单独一个文件。这里我们就两种情况都看一下。
使用的资源是Unity3D自带的以及从一本教材中取出的两种类型的动画资源,同时需要对其动画创建对应的Animator Controller。
模型动画都存放在一个文件中的情况
一个FBX文件保存了模型、骨骼和动画,如下图:

下面是配置的Animator Controller:

需要注意的是,官方并没有提供为Animator设置Animator Controller的接口,所以我们必须将配置好的GameObject制作为一个预制件进行加载。
Resources加载
using UnityEngine;
using System.Collections; public class AllInOneResourcesLoad : MonoBehaviour
{
private Animator _animator; void Start()
{
GameObject go = Resources.Load<GameObject>("AllInOne/ConstructorPrefab"); GameObject man = Instantiate(go) as GameObject;
_animator = man.GetComponent<Animator>();
} void OnGUI()
{
if(GUI.Button(new Rect(, , , ), "idle"))
{
_animator.SetBool("walk", false);
_animator.SetBool("run", false);
}
if(GUI.Button(new Rect(, , , ), "walk"))
{
_animator.SetBool("walk", true);
_animator.SetBool("run", false);
}
if(GUI.Button(new Rect(, , , ), "run"))
{
_animator.SetBool("walk", false);
_animator.SetBool("run", true);
}
if(GUI.Button(new Rect(, , , ), "jump"))
{
_animator.SetTrigger("jump");
}
}
}
AssetBundle加载
打包
using UnityEngine;
using UnityEditor; public class CreateAllInOneAB
{
[MenuItem("Tool/CreateAllInOneAB")]
private static void Create()
{
BuildPipeline.BuildAssetBundle(null, new[]
{
AssetDatabase.LoadAssetAtPath("Assets/Resources/AllInOne/ConstructorPrefab.prefab", typeof(GameObject))
},
Application.streamingAssetsPath + "/AllInOne.assetbundle",
BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.UncompressedAssetBundle,
BuildTarget.StandaloneWindows64);
}
}
加载
using UnityEngine;
using System.Collections; public class AllInOneAssetBundleLoad : MonoBehaviour
{
private Animator _animator; void Start()
{
AssetBundle assetBundle = AssetBundle.CreateFromFile(Application.streamingAssetsPath + "/AllInOne.assetbundle"); GameObject go = assetBundle.Load("ConstructorPrefab", typeof(GameObject)) as GameObject; GameObject man = Instantiate(go) as GameObject;
_animator = man.GetComponent<Animator>();
} void OnGUI()
{
if(GUI.Button(new Rect(, , , ), "idle"))
{
_animator.SetBool("walk", false);
_animator.SetBool("run", false);
}
if (GUI.Button(new Rect(, , , ), "walk"))
{
_animator.SetBool("walk", true);
_animator.SetBool("run", false);
}
if (GUI.Button(new Rect(, , , ), "run"))
{
_animator.SetBool("walk", false);
_animator.SetBool("run", true);
}
if (GUI.Button(new Rect(, , , ), "jump"))
{
_animator.SetTrigger("jump");
}
}
}
模型动画分开存放的情况
还有一种情况是模型和动画是分为多个FBX文件存放的,比如下面是模型文件:

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

下面是配置的Animator Controller:

除了没有提供设置Animator Controller的接口,也无法在运行时对动画剪辑进行增加删除的操作,所以我们一般打包时就收集所有的依赖项一起打包,归根结底还是只需要一个制作好的预制件即可。
从这个角度看,其实是否将动画进行拆分最终的使用方式都是一样的。
Resources加载
using UnityEngine;
using System.Collections; public class ResourcesLoad : MonoBehaviour
{
private Animator _animator; void Start()
{
GameObject go = Resources.Load<GameObject>("ZombieNurse/ZombieNursePrefab"); GameObject man = Instantiate(go) as GameObject;
_animator = man.GetComponent<Animator>();
} void OnGUI()
{
if(GUI.Button(new Rect(, , , ), "idle"))
{
_animator.SetBool("run", false);
}
if(GUI.Button(new Rect(, , , ), "run"))
{
_animator.SetBool("run", true);
}
if(GUI.Button(new Rect(, , , ), "attack"))
{
_animator.SetTrigger("attack");
}
if(GUI.Button(new Rect(, , , ), "dead"))
{
_animator.SetTrigger("dead");
}
}
}
AssetBundle加载
打包
using UnityEditor;
using UnityEngine; public class CreateAB
{
[MenuItem("Tool/CreateAB")]
private static void Create()
{
BuildPipeline.BuildAssetBundle(null, new[]
{
AssetDatabase.LoadAssetAtPath("Assets/Resources/ZombieNurse/ZombieNursePrefab.prefab", typeof(GameObject))
},
Application.streamingAssetsPath + "/AB.assetbundle",
BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.UncompressedAssetBundle,
BuildTarget.StandaloneWindows64);
}
}
加载
using UnityEngine;
using System.Collections; public class AssetBundleLoad : MonoBehaviour
{
private Animator _animator; void Start()
{
AssetBundle assetBundle = AssetBundle.CreateFromFile(Application.streamingAssetsPath + "/AB.assetbundle"); GameObject go = assetBundle.Load("ZombieNursePrefab", typeof(GameObject)) as GameObject; GameObject man = Instantiate(go) as GameObject;
_animator = man.GetComponent<Animator>();
} void OnGUI()
{
if(GUI.Button(new Rect(, , , ), "idle"))
{
_animator.SetBool("run", false);
}
if(GUI.Button(new Rect(, , , ), "run"))
{
_animator.SetBool("run", true);
}
if(GUI.Button(new Rect(, , , ), "attack"))
{
_animator.SetTrigger("attack");
}
if(GUI.Button(new Rect(, , , ), "dead"))
{
_animator.SetTrigger("dead");
}
}
}
Unity3D之Mecanim动画系统学习笔记(十):Mecanim动画的资源加载相关的更多相关文章
- 驱动开发学习笔记. 0.07 Uboot链接地址 加载地址 和 链接脚本地址
驱动开发学习笔记. 0.07 Uboot链接地址 加载地址 和 链接脚本地址 最近重新看了乾龙_Heron的<ARM 上电启动及 Uboot 代码分析>(下简称<代码分析>) ...
- Unity3D之Legacy动画系统学习笔记
Unity3D的Mecanim动画系统是非常强大的,而且作为Unity推荐的动画系统,其未来会完全代替老的一套动画系统,即Legacy动画系统.目前的情况是Mecanim与Legacy两套动画系统同时 ...
- Unity3D之Mecanim动画系统学习笔记(一):认识Mecanim动画系统
Mecanim简介 Mecanim动画系统是Unity3D4.0开始引入的一套全新的动画系统,主要提供了下面4个方面的功能: 针对人形角色提供一套特殊的工作流. 动画重定向的能力,可以非常方便的把动画 ...
- Unity3D之Mecanim动画系统学习笔记(十一):高级功能应用
动作游戏 还记得读书的时候熬夜打<波斯王子>的时光,我们的王子通过跳跃穿过墙壁的小洞.在高层建筑上进行攀爬和跳跃,还有在操作失误掉下高楼和触发必死机关后使用时之沙的时光倒流功能回归死亡之前 ...
- Unity3D之Mecanim动画系统学习笔记(九):Blend Tree(混合树)
认识Blend Tree 我们在Animator Controller中除了可以创建一个State外还可以创建一个Blend Tree,如下: 那么我们看下新创建的Blend Tree和State有什 ...
- Unity3D之Mecanim动画系统学习笔记(二):模型导入
我们要在Unity3D中使用上模型和动画,需要经过下面几个阶段的制作,下面以一个人形的模型开发为准来介绍. 模型制作 模型建模(Modelling) 我们的美术在建模时一般会制作一个称为T-Pose( ...
- Unity3D之Mecanim动画系统学习笔记(四):Animation State
动画的设置 我们先看看Animation Clip的一些设置: Loop time:动画是否循环播放. 下面出现了3个大致一样的选项: Root Transform Rotation:表示为播放动画的 ...
- Unity3D之Mecanim动画系统学习笔记(八):Animator Layers(动画分层)
解决什么问题? 动画分层可以用来解决什么样的问题呢?试想一下如果你要开发一款第三人称的射击游戏,那么肯定是希望身体的动画分为上下两部分,上方根据瞄准的位置和是否射击进行动画播放,下方根据移动播放动画. ...
- Entity Framework学习笔记(五)----Linq查询(2)---贪婪加载
请注明转载地址:http://www.cnblogs.com/arhat 在上一章中,我们使用了Linq对Entity Framework进行了一个查询,但是通过学习我们却发现了懒加载给我来的性能上的 ...
随机推荐
- bzoj3632
裸的最大团,随机化大法好 多次随机出一个选择顺序然后贪心即可 ..,..] of boolean; a:..] of longint; v:..] of boolean; n,m,i,j,x,y,an ...
- Qt之QTableView添加复选框(QAbstractTableModel)
简述 使用QTableView,经常会遇到复选框,要实现一个好的复选框,除了常规的功能外,还应注意以下几点: 三态:不选/半选/全选 自定义风格(样式) 下面我们介绍一下常见的实现方式: 编辑委托. ...
- UVa 580 (递推) Critical Mass
题意: 有两种盒子分别装有铀(U)和铅(L),现在把n个盒子排成一列(两种盒子均足够多),而且要求至少有3个铀放在一起,问有多少种排放方法. 分析: n个盒子排成一列,共有2n中方案,设其中符合要求的 ...
- kendo grid输入框验证方法
$("#grid").kendoGrid({ dataSource: dataSrc, //toolbar: ["save", "取消"], ...
- Squid故障
1.COSS will not function without large file support (off_t is 4 bytes long. Please reconsider recomp ...
- angular+rails集成实战
http://start.jcolemorrison.com/setting-up-an-angularjs-and-rails-4-1-project/ 1. 添加gemgem 'sprockets ...
- .NET之美——C# 中的委托和事件
C# 中的委托和事件 文中代码在VS2005下通过,由于VS2003(.Net Framework 1.1)不支持隐式的委托变量,所以如果在一个接受委托类型的位置直接赋予方法名,在VS2003下会报错 ...
- HDU 5379 Mahjong tree
题意:在一棵有n个节点的树上放编号从1到n的麻将,要求每个点的儿子节点之间的编号连续,每棵子树内的编号连续. 解法:手推一组样例之后就可以得到如下结论然后从根节点一边讨论一边搜就好了. 当一个节点只有 ...
- ajax-Ajax试题
ylbtech-doc:ajax-Ajax试题 Ajax 1.A,Ajax试题返回顶部 001.{Ajax题目}使用Ajax可带来便捷有()(选择3项) A)减轻服务器的负担 B) ...
- Calling C++ code from C# z
http://blogs.msdn.com/b/borisj/archive/2006/09/28/769708.aspx I apologize for the long delay for thi ...