失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > [转]AssetBundle(Unity)

[转]AssetBundle(Unity)

时间:2019-09-14 00:09:31

相关推荐

[转]AssetBundle(Unity)

1.使用AB包的原因

(1)减小资源大小

(2)方便更新资源

2.AB使用流程

(1)制定资源的AB属性

(2)构建AB包

(3)上传AB包

(4)加载AB包和包里面的资源

实际操作步骤:

(1)取名

(2)编辑器扩展,方便打包

using UnityEditor;using System.IO;public class CreateAB{[MenuItem("Assets/Build AB")]static void BuildAllAB(){string dir = "AssetBundles";if (Directory.Exists(dir) == false){Directory.CreateDirectory(dir);}BuildPipeline.BuildAssetBundles(dir, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);}}

(3)点击Build AB,打包成AB包

在文件夹中可看到打包后的资源

BuildAssetBundleOptions的几个重要属性:

(1)None:使用LZMA算法,对资源整体打包。体积小,加载时间长。

(2)ChunkBasedCompression:使用LZ4算法,对资源分块打包,体积较小,加载时间较短.

(3)UncompressedAssetBundle:不打包,体积较大,加载时间短。

manifest文件

两个重要属性:

(1)Assets:资源路径

(2)Dependencies:依赖

在场景中创建材质并取名为red,材质包名为red.unity3d,创建cube作为预制体,资源包名为cube.unity3d。

添加如下脚本加载资源:

using System.Collections;using System.Collections.Generic;using UnityEngine;public class Text : MonoBehaviour {void Start () {AssetBundle ab1 = AssetBundle.LoadFromFile("AssetsBundles/red.unity.3d");AssetBundle ab = AssetBundle.LoadFromFile("AssetsBundles/cube.unity.3d");GameObject cube = ab.LoadAsset<GameObject>("Cube");Instantiate(cube);}}

AssetBundle的几个重要加载方式:

(1)AssetBundle.LoadFromMemoryAsync

using System.Collections;using System.Collections.Generic;using UnityEngine;using System.IO;public class Text : MonoBehaviour {IEnumerator Start () {string path="AssetBundles/red.unity3d";AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path));yield return request;AssetBundle bundle = createRequest.assetBundle//使用里面的资源GameObject wallPrefab = bundle .LoadAsset<GameObject>("CubeWall");Instantiate(wallPrefab)}}

(2)AssetBundle.LoadFromFile

//第二种加载AB的方式 LoadFromFileAssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(path);yield return request;AssetBundle ab = request.assetBundle;//使用里面的资源GameObject wallPrefab = ab.LoadAsset<GameObject>("CubeWall");Instantiate(wallPrefab)

(3)WWW.LoadFromCacheOrDownload

//第三种加载AB的方式 WWWwhile (Caching.ready == false){yield return null;}file:// file:///WWW www = WWW.LoadFromCacheOrDownload(@"file:/E:\Unity Project Workspace\AssetBundleProject\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;//使用里面的资源GameObject wallPrefab = ab.LoadAsset<GameObject>("CubeWall");Instantiate(wallPrefab)

(4)UnityWebRequest

//第四种方式 使用UnityWebRequest//string uri = @"file:///E:\Unity Project Workspace\AssetBundleProject\AssetBundles\cubewall.unity3d";string uri = @"http://localhost/AssetBundles/cubewall.unity3d";UnityWebRequest request = UnityWebRequest.GetAssetBundle(uri);yield return request.Send();//AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;//使用里面的资源GameObject wallPrefab = ab.LoadAsset<GameObject>("CubeWall");Instantiate(wallPrefab)

AssetBundle资源卸载:

(1)AssetBundle.Unload(true:)卸载所有资源,即使有资源被使用着。适用于:

1。在关切切换、场景切换

2。资源没被用的时候

(2)AssetBundle.Unload(false):卸载所有没用被使用的资源

个别资源怎么卸载:

1。通过 Resources.UnloadUnusedAssets.

2。场景切换的时候

如果觉得《[转]AssetBundle(Unity)》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。