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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM