AssetBundle LoadFromStream的bundle honey select导入场景景出错,如何解决?

扫一扫,访问微社区
后使用快捷导航没有帐号?
签到成功!您今天第{todayrank}个签到,签到排名竞争激烈,记得每天都来签到哦!已连续签到:{constant}天,累计签到:{days}天
当前位置: &
查看: 657|回复: 4
关于ios加载assetbundle,真机测试奔溃问题及解决办法(当然不是绝对)
2143/150排名<font color="#FF昨日变化7主题帖子积分
初来乍到, 积分 143, 距离下一级还需 7 积分
初来乍到, 积分 143, 距离下一级还需 7 积分
蛮牛币2546
在线时间75 小时
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
才可以下载或查看,没有帐号?
如题本帖主要是分享ios真机加载assetbundle过程中出现的,首先说下我测试中遇到的问题
1、assetbundle打包(老生常谈)
QQ截图04.png (44.94 KB, 下载次数: 4)
assetbundle打包新
13:56 上传
QQ截图04.png (51.76 KB, 下载次数: 0)
assetbundle打包旧版
13:54 上传
2、www加载assetbundle和文件流加载assetbundle,路径为对应的StreamingAssets,
QQ截图04.png (73.69 KB, 下载次数: 8)
13:59 上传
3、测试结果&&iPhone6上测试崩溃
77C4}(`_4O%T~R01JBP%S%L.jpg (155.43 KB, 下载次数: 3)
14:02 上传
测试结果为www路径正确,实例化对象时(克隆bundle时)错误
4、解决办法,修改ios平台下playersetting
QQ截图35.png (3.1 KB, 下载次数: 0)
14:05 上传
strip engine code 禁用即可(查了好多资料才找到,醉醉的),端默认关闭 ios端默认开启
5、待解决问题 www加载时 按上文的加载路径测试通过,文件流加载时错误(读取ios的绝对路径测试通过
这里是我的文件流加载assetbundle 希望看过帖子的各位大神可以给些提示或解决办法(在此跪谢各位大神了)。
[AppleScript] 纯文本查看 复制代码IEnumerator LoadAssetBundles(string CompletePath)
AssetBundleCreateRequest asset =
{ FileStream AssetIO = new FileStream(CompletePath, FileMode.Open, FileAccess.ReadWrite); //创建文件流(对象现含有中文)
byte[] assetbytes = new byte[AssetIO.Length];
AssetIO.Read(assetbytes, 0, (int)AssetIO.Length);
AssetIO.Close();
asset = AssetBundle.LoadFromMemoryAsync(assetbytes);//从内存中创建资源
catch (Exception e)
Debug.Log(e.Message);
AssetBundle LoadAsset = asset.assetB//
if (asset.isDone)
GameObject gameObj =
gameObj = Instantiate(LoadAsset.mainAsset) as GameO
PositionManage.instance.lobj.Add(gameObj);
PositionManage.instance.cidDic.Add(CompletePath, gameObj);
LoadAsset.Unload(false);
catch (Exception e)
Debug.Log(e.Message);
每日推荐:
74031/5000排名<font color="#FF昨日变化主题帖子积分
日久生情, 积分 4031, 距离下一级还需 969 积分
日久生情, 积分 4031, 距离下一级还需 969 积分
蛮牛币4888
在线时间519 小时
学习了,先收藏日后肯定用的到
每日推荐:
72531/5000排名<font color="#FF昨日变化主题帖子积分
日久生情, 积分 2531, 距离下一级还需 2469 积分
日久生情, 积分 2531, 距离下一级还需 2469 积分
蛮牛币10191
在线时间359 小时
来自Mobile---
每日推荐:
4336/500排名<font color="#FF昨日变化4主题帖子积分
四处流浪, 积分 336, 距离下一级还需 164 积分
四处流浪, 积分 336, 距离下一级还需 164 积分
在线时间127 小时
发帖时在路边捡到 2
蛮牛币,偷偷放进了口袋.
每日推荐:
5687/1000排名<font color="#FF昨日变化主题帖子积分
熟悉之中, 积分 687, 距离下一级还需 313 积分
熟悉之中, 积分 687, 距离下一级还需 313 积分
蛮牛币1561
在线时间206 小时
多谢多谢,卡这半天了{:104:}
每日推荐:先介绍一种常用的加载AssetBundle方法
using UnityE
using System.C
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
Instantiate(www.assetBundle.mainAsset);
第二种,将www文件读成字节进行同步加载
1 using UnityE
2 using System.C
4 public class ExampleClass : MonoBehaviour
/// &summary&
/// 返回字节数组
/// &/summary&
/// &param name="binary"&&/param&
<span style="color: #
/// &returns&&/returns&
<span style="color: #
byte[] MyDecription(byte[] binary)
<span style="color: #
<span style="color: #
<span style="color: #
<span style="color: #
<span style="color: #
IEnumerator Start()
<span style="color: #
<span style="color: #
WWW www = new WWW("http://myserver/myBundle.unity3d");
//带有后缀的文件路径,可以是网络也可以是本地
<span style="color: #
yield return
<span style="color: #
byte[] decryptedBytes = MyDecription(www.bytes);
//返回字节数组
<span style="color: #
AssetBundle assetBundle = AssetBundle.LoadFromMemory(decryptedBytes);
//从内存中同步加载资源资源包
<span style="color: #
yield return assetB
<span style="color: #
if (assetBundle != null)
//如果资源包不为空
<span style="color: #
<span style="color: #
Instantiate(assetBundle.LoadAsset&GameObject&("myBundle");
//根据名字从资源包中加载对应资源
<span style="color: #
<span style="color: #
<span style="color: # }
第三种 异步
1 using UnityE
2 using System.C
4 public class ExampleClass : MonoBehaviour
/// &summary&
/// 返回字节数组
/// &/summary&
/// &param name="binary"&&/param&
<span style="color: #
/// &returns&&/returns&
<span style="color: #
byte[] MyDecription(byte[] binary)
<span style="color: #
<span style="color: #
<span style="color: #
<span style="color: #
<span style="color: #
IEnumerator Start()
<span style="color: #
<span style="color: #
WWW www = new WWW("http://myserver/myBundle.unity3d");
//带有后缀的文件路径,可以是网络也可以是本地
<span style="color: #
yield return
<span style="color: #
byte[] decryptedBytes = MyDecription(www.bytes);
//返回字节数组
<span style="color: #
AssetBundleCreateRequest bundle = AssetBundle.LoadFromMemoryAsync(decryptedBytes);//从内存中异步加载资源资源包
<span style="color: #
yield return
<span style="color: #
if (bundle != null && bundle.assetBundle != null)
<span style="color: #
<span style="color: #
Instantiate(bundle.assetBundle.LoadAsset&GameObject&("myBundle"));//根据名字从资源包中加载对应资源
<span style="color: #
<span style="color: #
<span style="color: # }
下面贴上项目中使用的方法 和异步加载很像,只是有些简单的实现进行了封装
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
if (bundle != null && bundle.assetBundle != null)
_model.ModelObj = Instantiate(bundle.assetBundle.LoadAsset&GameObject&(_model.ModelFbx));
//根据资源名加载资源包中资源。
<span style="color: #
_model.ModelObj.name = _model.ModelF
<span style="color: #
_model.ModelObj.transform.localPosition = new Vector3(<span style="color: #, <span style="color: #, <span style="color: #);
<span style="color: #
if (_model.ModelObj.name != gouJianData.CurSelectGouJian.ModelFbx) { _model.ModelObj.SetActive(false); }
<span style="color: #
bundle.assetBundle.Unload(false);
<span style="color: #
bundle = null;
<span style="color: #
<span style="color: #
<span style="color: #
<span style="color: #
LogHelp.Write("模型下载错误:{0}", filePath);
<span style="color: #
<span style="color: #
yield return new WaitForSeconds(<span style="color: #.5f);
<span style="color: #
<span style="color: #
//判断模型是否下载完成
<span style="color: #
if (IsLoadModel == true && iNum == iCount)
<span style="color: #
<span style="color: #
IsLoadModel = false;
<span style="color: #
this.LoadModelEnd();
<span style="color: #
<span style="color: #
对加载AssetBundle做一小结,以后补充。
欢迎广大Unity兴趣爱好者加群学习(进群备注:博客) &随时提出问题解决问题!
树欲静而风不止,子欲养而亲不待!
我参与的团队
积分与排名
评论排行榜
阅读排行榜[Unity3d]打包Assetbundle并加载
由于我们要将模型资源放在远程的服务器端,但如果直接放fbx模型是不可以加载的,所以我们可以将fbx做成预设或者是直接将其打包成assetbundle&#26684;式的,然后通过www来加载获取。
1.首先要讲一下不同平台下的一个StreamingAssets路径,这是不同的。
//不同平台下StreamingAssets的路径是不同的,这里需要注意一下。
public static readonly string PathURL =
#if UNITY_ANDROID
"jar:file://" + Application.dataPath + "!/assets/";
#elif UNITY_IPHONE
Application.dataPath + "/Raw/";
#elif UNITY_STANDALONE_WIN || UNITY_EDITOR
//windows平台和web平台
"file://" + Application.dataPath + "/StreamingAssets/";
这就获取到了不同平台的一个路径,我们可以将打包的文件放在这些路径下,然后再从这路径去读取资源。
2.关于打包assetbundle的脚本
using UnityE
using System.C
using UnityE
public class Test : Editor
//打包单个
[MenuItem("Custom Editor/Create AssetBunldes Main")]
static void CreateAssetBunldesMain ()
//获取在Project视图中选择的所有游戏对象
Object[] SelectedAsset = Selection.GetFiltered (typeof(Object), SelectionMode.DeepAssets);
//遍历所有的游戏对象
foreach (Object obj in SelectedAsset)
//本地测试:建议最后将Assetbundle放在StreamingAssets文件夹下,如果没有就创建一个,因为移动平台下只能读取这个路径
//StreamingAssets是只读路径,不能写入
//服务器下载:就不需要放在这里,服务器上客户端用www类进行下载。
string targetPath = Application.dataPath + "/StreamingAssets/" + obj.name + ".assetbundle";
if (BuildPipeline.BuildAssetBundle (obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies)) {
Debug.Log(obj.name +"资源打包成功");
Debug.Log(obj.name +"资源打包失败");
//刷新编辑器
AssetDatabase.Refresh ();
[MenuItem("Custom Editor/Create AssetBunldes ALL")]
static void CreateAssetBunldesALL ()
Caching.CleanCache ();
string Path = Application.dataPath + "/StreamingAssets/ALL.assetbundle";
Object[] SelectedAsset = Selection.GetFiltered (typeof(Object), SelectionMode.DeepAssets);
foreach (Object obj in SelectedAsset)
Debug.Log ("Create AssetBunldes name :" + obj);
//这里注意第二个参数就行
if (BuildPipeline.BuildAssetBundle (null, SelectedAsset, Path, BuildAssetBundleOptions.CollectDependencies)) {
AssetDatabase.Refresh ();
[MenuItem("Custom Editor/Create Scene")]
static void CreateSceneALL ()
//清空一下缓存
Caching.CleanCache();
string Path = Application.dataPath + "/MyScene.unity3d";
[]levels = {"Assets/Level.unity"};
//打包场景
BuildPipeline.BuildPlayer( levels, Path,BuildTarget.WebPlayer, BuildOptions.BuildAdditionalStreamedScenes);
AssetDatabase.Refresh ();
前提要新手动创建一个StreamingAssets文件夹
3.读取资源,这里只举例从本地读取,跟从网络读取是一样的,可以参考官方文档:
https://game.ceeger.com/Script/WWW/WWW.html
using UnityE
using System.C
public class RunScript : MonoBehaviour
//不同平台下StreamingAssets的路径是不同的,这里需要注意一下。
public static readonly string PathURL =
#if UNITY_ANDROID
"jar:file://" + Application.dataPath + "!/assets/";
#elif UNITY_IPHONE
Application.dataPath + "/Raw/";
#elif UNITY_STANDALONE_WIN || UNITY_EDITOR
"file://" + Application.dataPath + "/StreamingAssets/";
void OnGUI()
if(GUILayout.Button("Main Assetbundle"))
//StartCoroutine(LoadMainGameObject(PathURL + "Prefab0.assetbundle"));
//StartCoroutine(LoadMainGameObject(PathURL +
"Prefab1.assetbundle"));
StartCoroutine(LoadMainCacheGameObject(PathURL + "Prefab0.assetbundle"));
StartCoroutine(LoadMainCacheGameObject(PathURL +
"Prefab1.assetbundle"));
if(GUILayout.Button("ALL Assetbundle"))
StartCoroutine(LoadALLGameObject(PathURL + "ALL.assetbundle"));
if(GUILayout.Button("Open Scene"))
StartCoroutine(LoadScene());
//读取一个资源
private IEnumerator LoadMainGameObject(string path)
WWW bundle = new WWW(path);
//加载到游戏中
yield return Instantiate(bundle.assetBundle.mainAsset);
bundle.assetBundle.Unload(false);
//读取全部资源
private IEnumerator LoadALLGameObject(string path)
WWW bundle = new WWW(path);
//通过Prefab的名称把他们都读取出来
bundle.assetBundle.Load("Prefab0");
bundle.assetBundle.Load("Prefab1");
//加载到游戏中
yield return Instantiate(obj0);
yield return Instantiate(obj1);
bundle.assetBundle.Unload(false);
private IEnumerator LoadMainCacheGameObject(string path)
WWW bundle = WWW.LoadFromCacheOrDownload(path,5);
//加载到游戏中
yield return Instantiate(bundle.assetBundle.mainAsset);
bundle.assetBundle.Unload(false);
private IEnumerator LoadScene()
WWW download = WWW.LoadFromCacheOrDownload ("file://"+Application.dataPath + "/MyScene.unity3d", 1);
var bundle = download.assetB
Application.LoadLevel ("Level");

我要回帖

更多关于 unity导入assetbundle 的文章

 

随机推荐