加载AssetBundle方法

时间:2023-03-08 18:33:34

先介绍一种常用的加载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文件读成字节进行同步加载

 using UnityEngine;
using System.Collections; public class ExampleClass : MonoBehaviour
{
/// <summary>
/// 返回字节数组
/// </summary>
/// <param name="binary"></param>
/// <returns></returns>
byte[] MyDecription(byte[] binary)
{
byte[] decrypted;
return decrypted;
}
IEnumerator Start()
{
WWW www = new WWW("http://myserver/myBundle.unity3d"); //带有后缀的文件路径,可以是网络也可以是本地
yield return www;
byte[] decryptedBytes = MyDecription(www.bytes); //返回字节数组
AssetBundle assetBundle = AssetBundle.LoadFromMemory(decryptedBytes); //从内存中同步加载资源资源包
yield return assetBundle;
if (assetBundle != null) //如果资源包不为空
{
Instantiate(assetBundle.LoadAsset<GameObject>("myBundle"); //根据名字从资源包中加载对应资源
}
}
}

第三种 异步

 using UnityEngine;
using System.Collections; public class ExampleClass : MonoBehaviour
{
/// <summary>
/// 返回字节数组
/// </summary>
/// <param name="binary"></param>
/// <returns></returns>
byte[] MyDecription(byte[] binary)
{
byte[] decrypted;
return decrypted;
}
IEnumerator Start()
{
WWW www = new WWW("http://myserver/myBundle.unity3d"); //带有后缀的文件路径,可以是网络也可以是本地
yield return www;
byte[] decryptedBytes = MyDecription(www.bytes); //返回字节数组
AssetBundleCreateRequest bundle = AssetBundle.LoadFromMemoryAsync(decryptedBytes);//从内存中异步加载资源资源包
yield return bundle;
if (bundle != null && bundle.assetBundle != null)
{
Instantiate(bundle.assetBundle.LoadAsset<GameObject>("myBundle"));//根据名字从资源包中加载对应资源
}
}
}

下面贴上项目中使用的方法 和异步加载很像,只是有些简单的实现进行了封装

   private IEnumerator LoadBundle(ModelGouJian _model)
{
string filePath = string.Format("{0}/_lesson/ModelFbx/{1}/{2}.unity3d", PublicJs.Datapath, _model.JieDianID, _model.ModelFbx);
byte[] decryptedData = FileHelp.Instance.GetFileTextByteEsc(filePath); //返回字节数组
AssetBundleCreateRequest bundle = AssetBundle.LoadFromMemoryAsync(decryptedData); //从内存中加载资源包
yield return bundle;
if (bundle != null && bundle.assetBundle != null)
{
_model.ModelObj = Instantiate(bundle.assetBundle.LoadAsset<GameObject>(_model.ModelFbx)); //根据资源名加载资源包中资源。
_model.ModelObj.name = _model.ModelFbx;
_model.ModelObj.transform.localPosition = new Vector3(, , );
if (_model.ModelObj.name != gouJianData.CurSelectGouJian.ModelFbx) { _model.ModelObj.SetActive(false); }
bundle.assetBundle.Unload(false);
bundle = null;
}
else
{
LogHelp.Write("模型下载错误:{0}", filePath);
}
yield return new WaitForSeconds(0.5f);
iNum++;
//判断模型是否下载完成
if (IsLoadModel == true && iNum == iCount)
{
IsLoadModel = false;
this.LoadModelEnd();
}
}

对加载AssetBundle做一小结,以后补充。

欢迎广大Unity兴趣爱好者加群学习165628892(进群备注:博客)  随时提出问题解决问题!

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

2016年12月22日