首页 技术 正文
技术 2022年11月14日
0 收藏 839 点赞 2,482 浏览 2705 个字

先介绍一种常用的加载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日

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289