unity使用AssetBundle使用记录四--加载与卸载(1)
2016-11-23 14:34
513 查看
使用的unity版本是5.3.5f。
在前一篇文章中,说到了AssetBundle资源的存储。当AssetBundle资源下载到本地后,需要加载到内存中,并且创建出AssetBundle文件的内存对象。
今天的测试用了unity中的三种方法来加载。
之后代码中获得路径的函数代码:
方法一:WWW.assetbundle
直接上测试的代码:
这里有www.assetbundle的解释。http://www.ceeger.com/Script/WWW/WWW.assetBundle.html。
方法二:AssetBundle.LoadFromFile(string path)
以前是使用AssetBundle.CreateFromFile(string path)。现在使用会报错,提示已经过时了。现在使用的是AssetBundle.LoadFromFile(string path)。该接口可以从磁盘文件创建一个AssetBundle内存对象,不过这个方法官方介绍是只能支持非压缩格式的AssetBundle,就是创建的时候,压缩格式选择BuildAssetBundleOptions.UncompressedAssetBundle.但是本人测试的时候压缩格式是BuildAssetBundleOptions.None测试也能正常加载出来(win平台测试,安卓和苹果平台还未测试)。
测试的代码:
上面的代码中的第16行测试是不能正确读取路径文件的,而17行,18行都可以正确读取。这点本人目前还没弄明白,有明白的大神请赐教!
因为16行的代码取得的路径用在WWW方式读取没问题的。
方法三:AssetBundle.LoadFromMemory(byte[] binary)
以前是使用的AssetBundle.CreateFromMemory(byte[] binary),现在已经弃用了。
如果在生成AssetBundle资源的时候,有加密的处理,那么就需要先调用解密算法,再返回解密后的数据流。最后再用AssetBundle.LoadFromMemory(byte[] binary)接口从数据流中创建AssetBundle对象。官方说的这种方式是创建一个异步的AssetBundle内存区域。但是,现在的接口改变之后,发现有专门用于异步的接口,例如AssetBundle.LoadFromFileAsync(string
path)和LoadFromMemoryAsync(byte[] binary)等等。所以,现在这种说法得打一个问号了。如果有清楚的大神,请赐教啊!
前面说到部分原有接口已经弃用了,弃用的还有AssetBundle.CreateFromMemoryImmediate(byte[] binary)等等。这里不再多说,因为使用的时候,弃用的都会提示的。
在加载AssetBundle的时候,一定要注意,如果AssetBundle之间存在依赖关系,要先加载总的Manifest文件,通过这个文件加载对应的依赖文件,然后再加载要加载的AssetBundle。
关于AssetBundle这里还有借鉴的文章:
http://blog.csdn.net/llj1985/article/details/51314384
http://blog.csdn.net/crazygougou/article/details/51317554
在前一篇文章中,说到了AssetBundle资源的存储。当AssetBundle资源下载到本地后,需要加载到内存中,并且创建出AssetBundle文件的内存对象。
今天的测试用了unity中的三种方法来加载。
之后代码中获得路径的函数代码:
string GetPath() { 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/"; #else string.Empty; #endif return PathURL; }
方法一:WWW.assetbundle
直接上测试的代码:
//WWW加载AssetBundle资源 IEnumerator LoadAssetBundleFile() { //工程中的路径 WWW www = new WWW(GetPath() + "obj2.ab"); //打包好的AssetBundle资源路径和名称 if (!www.isDone) yield return 0; yield return www; AssetBundle ab = www.assetBundle; //通过www.assetBundle属性创建一个AssetBundle文件的内存对象 GameObject g = ab.LoadAsset("Quad") as GameObject; Instantiate(g); //网络路径 WWW www2 = new WWW("http://127.0.0.1:80/abc/obj1.ab"); if (!www2.isDone) yield return 0; yield return www2; AssetBundle ab2 = www2.assetBundle; GameObject g2 = ab2.LoadAsset("Cube (1)") as GameObject; Instantiate(g2); }
这里有www.assetbundle的解释。http://www.ceeger.com/Script/WWW/WWW.assetBundle.html。
方法二:AssetBundle.LoadFromFile(string path)
以前是使用AssetBundle.CreateFromFile(string path)。现在使用会报错,提示已经过时了。现在使用的是AssetBundle.LoadFromFile(string path)。该接口可以从磁盘文件创建一个AssetBundle内存对象,不过这个方法官方介绍是只能支持非压缩格式的AssetBundle,就是创建的时候,压缩格式选择BuildAssetBundleOptions.UncompressedAssetBundle.但是本人测试的时候压缩格式是BuildAssetBundleOptions.None测试也能正常加载出来(win平台测试,安卓和苹果平台还未测试)。
测试的代码:
IEnumerator LoadAssetBundle() { //测试正确加载 WWW www = new WWW(GetPath() + "obj1.ab"); //本地工程中的路径资源 if (!www.isDone) yield return 0; yield return www; AssetBundle ab = www.assetBundle; GameObject g = ab.LoadAsset("cubeasset") as GameObject; Instantiate(g); //AssetBundle ab1 = AssetBundle.CreateFromFile(""); //过时,不能用 //GetPath() + "obj1.ab" 测试不能加载(完整路径file://E:/unity project/AssetBundle_Test/Assets/StreamingAssets/obj1.ab)为什么这种路径是错的? //"Assets/StreamingAssets/obj2.ab" 测试能加载 //"E:/unity project/AssetBundle_Test/Assets/StreamingAssets/obj2.ab" 测试能加载 AssetBundle ab2 = AssetBundle.LoadFromFile("Assets/StreamingAssets/obj2.ab"); GameObject g2 = ab2.LoadAsset("Quad") as GameObject; Instantiate(g2); }
上面的代码中的第16行测试是不能正确读取路径文件的,而17行,18行都可以正确读取。这点本人目前还没弄明白,有明白的大神请赐教!
因为16行的代码取得的路径用在WWW方式读取没问题的。
方法三:AssetBundle.LoadFromMemory(byte[] binary)
以前是使用的AssetBundle.CreateFromMemory(byte[] binary),现在已经弃用了。
如果在生成AssetBundle资源的时候,有加密的处理,那么就需要先调用解密算法,再返回解密后的数据流。最后再用AssetBundle.LoadFromMemory(byte[] binary)接口从数据流中创建AssetBundle对象。官方说的这种方式是创建一个异步的AssetBundle内存区域。但是,现在的接口改变之后,发现有专门用于异步的接口,例如AssetBundle.LoadFromFileAsync(string
path)和LoadFromMemoryAsync(byte[] binary)等等。所以,现在这种说法得打一个问号了。如果有清楚的大神,请赐教啊!
IEnumerator LoadAssetBundle_LoadFromMemory() { //开始下载 WWW www = new WWW("http://127.0.0.1:80/abc/obj1.ab"); yield return www; //AssetBundleCreateRequest abcr = AssetBundle.CreateFromMemory(www.bytes); //过时,不能用 AssetBundle ab = AssetBundle.LoadFromMemory(www.bytes); GameObject obj = ab.LoadAsset("cubeasset") as GameObject; Instantiate(obj); Debug.Log("加载完成"); }
前面说到部分原有接口已经弃用了,弃用的还有AssetBundle.CreateFromMemoryImmediate(byte[] binary)等等。这里不再多说,因为使用的时候,弃用的都会提示的。
在加载AssetBundle的时候,一定要注意,如果AssetBundle之间存在依赖关系,要先加载总的Manifest文件,通过这个文件加载对应的依赖文件,然后再加载要加载的AssetBundle。
关于AssetBundle这里还有借鉴的文章:
http://blog.csdn.net/llj1985/article/details/51314384
http://blog.csdn.net/crazygougou/article/details/51317554
相关文章推荐
- unity使用AssetBundle使用记录四--加载与卸载(2)
- Unity5 AssetBundle系列——资源加载卸载以及AssetBundleManifest的使用
- unity使用AssetBundle使用记录一(续)--场景打包成AssetBundle并加载
- Unity游戏开发使用Assetbundle加载场景实战
- unity使用AssetBundle使用记录五)--AssetBundle内存管理
- unity使用AssetBundle使用记录一--打包
- Unity中如何从AssetBundle中加载和卸载对象
- Unity游戏开发使用Assetbundle加载场景实战
- Unity游戏开发使用Assetbundle加载场景实战
- unity使用AssetBundle使用记录三--存储
- Unity游戏开发使用Assetbundle加载场景的原理
- Unity游戏开发使用Assetbundle加载场景的实战
- Unity(五) Unity3D使用Assetbundle打包加载(Prefab、场景)转载原博客
- 【unity】关于AssetBundle的加载与卸载
- 关于unity 中使用AssetBundle加载资源,shader偶尔会丢失的问题解决办法
- Unity游戏开发使用Assetbundle加载场景的原理
- Unity游戏开发使用Assetbundle加载场景实战
- 项目记录05--客unity,服erlang--另外一种AssetBundle打包方式
- unity动态加载之AssetBundle原理
- Unity Assetbundle的加载方式的效率和内存占用