先介紹一種常用的加載AssetBundle方法
using UnityEngine; using System.Collections; using System.IO; public class LoadUnity3d : MonoBehaviour { // Use this for initialization void Start() { StartCoroutine(LoadScene()); } // Update is called once per frame void Update()
{
} IEnumerator LoadScene() { //文件路徑,也就是我們打包的那個 WWW www = new WWW("file:///" + Application.dataPath + "/Bundles/My Prefab.unity3d"); yield return www; Instantiate(www.assetBundle.mainAsset); } }
第二種,將www文件讀成字節進行同步加載
1 using UnityEngine; 2 using System.Collections; 3 4 public class ExampleClass : MonoBehaviour 5 { 6 /// <summary> 7 /// 返回字節數組 8 /// </summary> 9 /// <param name="binary"></param> 10 /// <returns></returns> 11 byte[] MyDecription(byte[] binary) 12 { 13 byte[] decrypted; 14 return decrypted; 15 } 16 IEnumerator Start() 17 { 18 WWW www = new WWW("http://myserver/myBundle.unity3d"); //帶有后綴的文件路徑,可以是網絡也可以是本地 19 yield return www; 20 byte[] decryptedBytes = MyDecription(www.bytes); //返回字節數組 21 AssetBundle assetBundle = AssetBundle.LoadFromMemory(decryptedBytes); //從內存中同步加載資源資源包 22 yield return assetBundle; 23 if (assetBundle != null) //如果資源包不為空 24 { 25 Instantiate(assetBundle.LoadAsset<GameObject>("myBundle"); //根據名字從資源包中加載對應資源 26 } 27 } 28 }
第三種 異步
1 using UnityEngine; 2 using System.Collections; 3 4 public class ExampleClass : MonoBehaviour 5 { 6 /// <summary> 7 /// 返回字節數組 8 /// </summary> 9 /// <param name="binary"></param> 10 /// <returns></returns> 11 byte[] MyDecription(byte[] binary) 12 { 13 byte[] decrypted; 14 return decrypted; 15 } 16 IEnumerator Start() 17 { 18 WWW www = new WWW("http://myserver/myBundle.unity3d"); //帶有后綴的文件路徑,可以是網絡也可以是本地 19 yield return www; 20 byte[] decryptedBytes = MyDecription(www.bytes); //返回字節數組 21 AssetBundleCreateRequest bundle = AssetBundle.LoadFromMemoryAsync(decryptedBytes);//從內存中異步加載資源資源包 22 yield return bundle; 23 if (bundle != null && bundle.assetBundle != null) 24 { 25 Instantiate(bundle.assetBundle.LoadAsset<GameObject>("myBundle"));//根據名字從資源包中加載對應資源 26 } 27 } 28 }
下面貼上項目中使用的方法 和異步加載很像,只是有些簡單的實現進行了封裝
1 private IEnumerator LoadBundle(ModelGouJian _model) 2 { 3 string filePath = string.Format("{0}/_lesson/ModelFbx/{1}/{2}.unity3d", PublicJs.Datapath, _model.JieDianID, _model.ModelFbx); 4 byte[] decryptedData = FileHelp.Instance.GetFileTextByteEsc(filePath); //返回字節數組 5 AssetBundleCreateRequest bundle = AssetBundle.LoadFromMemoryAsync(decryptedData); //從內存中加載資源包 6 yield return bundle; 7 if (bundle != null && bundle.assetBundle != null) 8 { 9 _model.ModelObj = Instantiate(bundle.assetBundle.LoadAsset<GameObject>(_model.ModelFbx)); //根據資源名加載資源包中資源。 10 _model.ModelObj.name = _model.ModelFbx; 11 _model.ModelObj.transform.localPosition = new Vector3(0, 0, 0); 12 if (_model.ModelObj.name != gouJianData.CurSelectGouJian.ModelFbx) { _model.ModelObj.SetActive(false); } 13 bundle.assetBundle.Unload(false); 14 bundle = null; 15 } 16 else 17 { 18 LogHelp.Write("模型下載錯誤:{0}", filePath); 19 } 20 yield return new WaitForSeconds(0.5f); 21 iNum++; 22 //判斷模型是否下載完成 23 if (IsLoadModel == true && iNum == iCount) 24 { 25 IsLoadModel = false; 26 this.LoadModelEnd(); 27 } 28 }
對加載AssetBundle做一小結,以后補充。
歡迎廣大Unity興趣愛好者加群學習165628892(進群備注:博客) 隨時提出問題解決問題!
樹欲靜而風不止,子欲養而親不待!
2016年12月22日