资源加载是必备的知识点,这里就说说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(0, 0, 100, 30), "idle"))
{
_animator.SetBool("walk", false);
_animator.SetBool("run", false);
}
if(GUI.Button(new Rect(100, 0, 100, 30), "walk"))
{
_animator.SetBool("walk", true);
_animator.SetBool("run", false);
}
if(GUI.Button(new Rect(200, 0, 100, 30), "run"))
{
_animator.SetBool("walk", false);
_animator.SetBool("run", true);
}
if(GUI.Button(new Rect(300, 0, 100, 30), "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);
}
}
加载
模型动画分开存放的情况
还有一种情况是模型和动画是分为多个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(0, 0, 100, 30), "idle"))
{
_animator.SetBool("run", false);
}
if(GUI.Button(new Rect(100, 0, 100, 30), "run"))
{
_animator.SetBool("run", true);
}
if(GUI.Button(new Rect(200, 0, 100, 30), "attack"))
{
_animator.SetTrigger("attack");
}
if(GUI.Button(new Rect(300, 0, 100, 30), "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(0, 0, 100, 30), "idle"))
{
_animator.SetBool("run", false);
}
if(GUI.Button(new Rect(100, 0, 100, 30), "run"))
{
_animator.SetBool("run", true);
}
if(GUI.Button(new Rect(200, 0, 100, 30), "attack"))
{
_animator.SetTrigger("attack");
}
if(GUI.Button(new Rect(300, 0, 100, 30), "dead"))
{
_animator.SetTrigger("dead");
}
}
}
原文地址:http://www.cnblogs.com/hammerc/p/4832650.html