AssetBundle(Unity)

举报
爱上游戏开发 发表于 2022/07/01 23:41:08 2022/07/01
【摘要】 推荐阅读:  我的CSDN 我的博客园 QQ群:704621321 1.使用AB包的原因 (1)减小资源大小 (2)方便更新资源 2.AB...

推荐阅读:

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);
    }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

(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);
        
    }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

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)

    }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

(2)AssetBundle.LoadFromFile

//第二种加载AB的方式 LoadFromFile
        AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(path);
        yield return request;
        AssetBundle ab = request.assetBundle;

        //使用里面的资源
     GameObject wallPrefab = ab.LoadAsset<GameObject>("CubeWall");
        Instantiate(wallPrefab)

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

(3)WWW.LoadFromCacheOrDownload

//第三种加载AB的方式 WWW
        while (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)

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

(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)

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

AssetBundle资源卸载:

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

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

2。资源没被用的时候
(2)AssetBundle.Unload(false):卸载所有没用被使用的资源
个别资源怎么卸载:

1。通过 Resources.UnloadUnusedAssets.

2。场景切换的时候

文章来源: unity3d.blog.csdn.net,作者:爱上游戏开发,版权归原作者所有,如需转载,请联系作者。

原文链接:unity3d.blog.csdn.net/article/details/84973093

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。