首页 > 代码库 > 加载AssetBundle方法

加载AssetBundle方法

先介绍一种常用的加载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 = http://www.mamicode.com/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做一小结,以后补充。

 

树欲静而风不止,子欲养而亲不待!

2016年12月22日

加载AssetBundle方法