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 }


浙公网安备 33010602011771号