前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Unity-资源打包-AssetBundle

Unity-资源打包-AssetBundle

作者头像
祝你万事顺利
发布2019-05-29 15:59:44
2.6K0
发布2019-05-29 15:59:44
举报
文章被收录于专栏:Unity游戏开发Unity游戏开发
AB包定义作用

1.压缩大小 2.热更新使用

使用流程

对要打包的资源进行设置

AB.PNG

C#脚本不能打成AB包,因为C#在Unity中会编译成中间文件。

参数相关

Build的路径(随意只要是在硬盘上都可以的) BuildAssetBundleOptions BuildAssetBundleOptions.None:使用LZMA算法压缩,压缩的包更小,但是加载时间更长。使用 之前需要整体解压。一旦被解压,这个包会使用LZ4重新压缩。使用资源的时候不需要整体解压。 在下载的时候可以使用LZMA算法,一旦它被下载了之后,它会使用LZ4算法保存到本地上。 BuildAssetBundleOptions.UncompressedAssetBundle:不压缩,包大,加载快 BuildAssetBundleOptions.ChunkBasedCompression:使用LZ4压缩,压缩率没有LZMA高,但是 我们可以加载指定资源而不用解压全部注意使用LZ4压缩,可以获得可以跟不压缩想媲美的加载速度,而且比不压缩文件要小。 BuildTarget选择build出来的AB包要使用的平台

创建AB包 创建AB包,使用BuildPipeline类中的方法

代码语言:javascript
复制
    private static readonly string AbPath = Application.streamingAssetsPath;
    [MenuItem("Tools/CreateAB")]
    static void CreateAbPack()
    {
        BuildPipeline.BuildAssetBundles(AbPath, BuildAssetBundleOptions.UncompressedAssetBundle, BuildTarget.Android);
        AssetDatabase.Refresh();
    }

清除文件中的AB包

代码语言:javascript
复制
[MenuItem("Tools/ClearABFile")]
    static void ClearAbPack()
    {
        DirectoryInfo directoryInfo = new DirectoryInfo(AbPath);
        FileSystemInfo[] fileSystemInfo = directoryInfo.GetFileSystemInfos();
        foreach (FileSystemInfo item in fileSystemInfo)
        {
            if(item is DirectoryInfo)
            {
                DirectoryInfo d = new DirectoryInfo(item.FullName);
                d.Delete(true);
            }
            else
            {
                item.Delete();
            }
        }
        AssetDatabase.Refresh();
    }

读取AB包 1.直接记载一个单独的物体(无依赖) 通过AssetBundle的LoadFromFile方法获取AB包,在通过LoadAsset的泛型方法获得GameObject

代码语言:javascript
复制
    private string path;
    void Start () {
        path = Application.streamingAssetsPath;
        AssetBundle ab = AssetBundle.LoadFromFile(path + "/prefab");
        //print(ab);
        GameObject go = ab.LoadAsset<GameObject>("Sphere");
        Instantiate(go);
    }

2.通过依赖加载,从本地进行加载

代码语言:javascript
复制
            path = Application.streamingAssetsPath;
            //从文件中加载StreamingAssets文件
            AssetBundle assetBundle = AssetBundle.LoadFromFile(path + "/StreamingAssets");
            //从StreamingAssets文件中获得AssetBundleManifest对象
            AssetBundleManifest assetBundleManifest = assetBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
            //从AssetBundleManifest中加载一个AssetBundle对象的全部依赖
            string[] dps = assetBundleManifest.GetAllDependencies("prefab");
            //遍历依赖,加载所有的依赖
            for (int i = 0; i < dps.Length; i++)
            {
                AssetBundle ab1 = AssetBundle.LoadFromFile(path + "/" + dps[i]);
            }
            AssetBundle ab = AssetBundle.LoadFromFile(path + "/prefab");
            GameObject go = ab.LoadAsset<GameObject>("Sphere");
            Instantiate(go);

3.异步加载从文件中

代码语言:javascript
复制
if (GUILayout.Button("异步加载"))
        {
            StartCoroutine(LoadAB(AbLoadBP));
        }
代码语言:javascript
复制
IEnumerator LoadAB(Action action)
    {
        path = Application.streamingAssetsPath;
        AssetBundleCreateRequest assetBundle = AssetBundle.LoadFromFileAsync(path + "/prefab");
        yield return assetBundle;
        action();
    }

4.从内存中加载 异步:

代码语言:javascript
复制
    IEnumerator LoadFromMemoryAsync()
    {
        string path = Application.streamingAssetsPath + "/" + "prefab";
        //byte by = byte.Parse(path);
        AssetBundleCreateRequest createRequest = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path));
        yield return createRequest;
        AssetBundle bundle = createRequest.assetBundle;

        GameObject prefab = bundle.LoadAsset<GameObject>("Cube");
        Instantiate(prefab);
    }

同步:

代码语言:javascript
复制
private void LoadFromMemory()
    {
        string path = Application.streamingAssetsPath + "/" + "prefab";
        AssetBundle bundle = AssetBundle.LoadFromMemory(File.ReadAllBytes(path));
        GameObject prefab = bundle.LoadAsset<GameObject>("Cube");
        Instantiate(prefab);
    }

5.从本地获得网络加载WWW.LoadFromCacheOrDownload 通过URL进行加载

代码语言:javascript
复制
IEnumerator LoadFromCacheOrDownLoadExample ()
    {
        while (!Caching.ready)
        {
            yield return null;
        }
        var www = WWW.LoadFromCacheOrDownload("http://192.168.0.250/prefab.unity3d", 5);
        //var www = WWW.LoadFromCacheOrDownload(@"D:\the9git\the9_09\UnityProject\UnityBase04\Assets\StreamingAssets\prefab", 5);
        yield return www;
        if (!string.IsNullOrEmpty(www.error))
        {
            Debug.Log(www.error);
            yield return null;
        }
        AssetBundle myLoadedAB = www.assetBundle;
        GameObject prefab = myLoadedAB.LoadAsset<GameObject>("Cube");
        Instantiate(prefab);
        //var asset = myLoadedAB.mainAsset;
    }

6.通过UnityWebRequest加载AB包

代码语言:javascript
复制
    IEnumerator InstantiateObject()
    {
        //string uri = "file:///" + Application.streamingAssetsPath + "/prefab";
        string uri = "http://192.168.0.250/StreamingAssets/perfab.unity3d";
        using (UnityWebRequest webRequest = UnityWebRequest.GetAssetBundle(uri))
        {
            yield return webRequest.SendWebRequest();
            AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(webRequest);
            GameObject cube = bundle.LoadAsset<GameObject>("Cube");
            Instantiate(cube);
        }
        string uri2 = "http://192.168.0.250/StreamingAssets/StreamingAssets";
        using (UnityWebRequest webRequest2 = UnityWebRequest.GetAssetBundle(uri2))
        {
            yield return webRequest2.SendWebRequest();
            AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(webRequest2);
            AssetBundleManifest assetBundleManifes = bundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
            string[] dps = assetBundleManifes.GetAllDependencies("perfab.unity3d");
            for (int i = 0; i < dps.Length; i++)
            {
                string uri3 = "http://192.168.0.250/StreamingAssets/" + dps[i];
                using (UnityWebRequest webRequest3 = UnityWebRequest.GetAssetBundle(uri3))
                {
                    yield return webRequest3.SendWebRequest();
                    AssetBundle ab = DownloadHandlerAssetBundle.GetContent(webRequest3);
                }
            }
        }
    }

卸载有两个方面 1,减少内存使用 2,有可能导致丢失 所以什么时候去卸载资源 AssetBundle.Unload(true)卸载所有资源,即使有资源被使用着 (1,在关切切换、场景切换2,资源没被用的时候 调用) AssetBundle.Unload(false)卸载所有没用被使用的资源 个别资源怎么卸载1,通过 Resources.UnloadUnusedAssets. 2,场景切换的时候 AssetBundle卸载

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.05.21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • AB包定义作用
  • 使用流程
相关产品与服务
文件存储
文件存储(Cloud File Storage,CFS)为您提供安全可靠、可扩展的共享文件存储服务。文件存储可与腾讯云服务器、容器服务、批量计算等服务搭配使用,为多个计算节点提供容量和性能可弹性扩展的高性能共享存储。腾讯云文件存储的管理界面简单、易使用,可实现对现有应用的无缝集成;按实际用量付费,为您节约成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档