007热更新之AssetBundle二
今天我们就可以将AssetBundle结束掉,AssetBundle是unity官方提提供的打包工具,在前面我们已经说过了如何去打包,就下来我们就来说说打包的压缩方式。
BuildAssetBundleOptions.None:压缩更小,时间更小。
BuildAssetBundleOptions.UncomprecessdAssetBundle:不压缩,包大。
BuildAssetBundleOptions.ChunkBasedCompression:中等
我们可以把共享的东西也设置AssetBundle属性,这样系统在打包的时候就不会重复打包了,这样内存就会很小。如下图写代码,把公共的部分写写出来。![007热更新之AssetBundle二 007热更新之AssetBundle二]()
这样写的话就不会出现材质丢失的情况了。
接下来就资源的加载方式了一共四种方式。
AssetBundle.LoadFromMemoryAsync//从内存加载方式
AssetBundle.LoadFromFile//从文件地址加载方式
WWW.LoadFromCacheOrDownload//可以下载,也可以从文件加载,官方会逐渐放弃,不推荐
UnityWebRequest//从网络获取
AssetBundle.LoadFromMemoryAsync//从内存加载方式
//第一种加载ab的方式
//string path = "AssetBundles/cubewall.unity3d";
//AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path));
//yield return request;
//AssetBundle ab = request.assetBundle;
AssetBundle.LoadFromFile//从文件地址加载方式
//第二种加载ab的方式
//string path = "AssetBundles/cubewall.unity3d";
//AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(path);
//yield return request;
//AssetBundle ab = request.assetBundle;
WWW.LoadFromCacheOrDownload//可以下载,也可以从文件加载,官方会逐渐放弃,不推荐
//第三种加载ab的方式
//while (Caching.ready == false)
//{
// yield return null;
//}
////WWW www = WWW.LoadFromCacheOrDownload(@"file:///D:\unity 3d\lesson1\AssetBundlesProject\AssetBundles\cubewall.unity3d", 1);
//WWW www = WWW.LoadFromCacheOrDownload(@"http://localhost/AssetBundles\cubewall.unity3d", 1);
//yield return www;
//if (string.IsNullOrEmpty(www.error) == false)
//{
// Debug.Log(www.error);
// yield break;
//}
//AssetBundle ab = www.assetBundle;
UnityWebRequest//从网络获取
//第四种加载ab的方式 unityrequset
string uri = @"file:///D:\unity 3d\lesson1\AssetBundlesProject\AssetBundles\cubewall.unity3d";
UnityWebRequest request = UnityWebRequest.GetAssetBundle(uri);
yield return request.SendWebRequest();
//AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
以上就是四种加载方式,其实系统已经给我们封装好了,我们在知道它的用途后,就可以直接使用了
AssetBundle manifestab = AssetBundle.LoadFromFile("AssetBundles/AssetBundles");
AssetBundleManifest manifest = manifestab.LoadAsset("AssetBundleManifest");
string[] strs = manifest.GetAllDependencies("cubewall.unity3d");
foreach (string name in strs)
{
print(name);
AssetBundle.LoadFromFile("AssetBundles/" + name);
}
GameObject wallPrefab = ab.LoadAsset("cubewall");
Instantiate(wallPrefab);
好了今天的内容就是这样了,AseetBundle是告诉我们更新的第一步打包是什么样子,接下来就是lua语言部分了,让我们拭目以待吧。

浙公网安备 33010602011771号