您的位置:首页 > 移动开发 > Unity3D

unity使用AssetBundle使用记录四--加载与卸载(1)

2016-11-23 14:34 513 查看
使用的unity版本是5.3.5f。

前一篇文章中,说到了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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: