AssetBundle分组策略

1、逻辑实体分组
  a、一个UI界面或者所有UI界面一个包(这个界面里面的贴图和布局信息一个包)
  b、一个角色或者所有角色一个包(这个角色里面的模型和动画一个包)
  c、所有的场景所共享的部分一个包(包括贴图和模型)
2、按照类型分组
  所有声音资源打成一个包,所有shader打成一个包,所有模型打成一个包,所有材质打成一个包
3、按照使用分组
  把在某一时间内使用的所有资源打成一个包。可以按照关卡分,一个关卡所需要的所有资源包括角色、贴图、声音等打成一个包。也可以按照场景分,一个场景所需要的资源一个包

AssetBundle分组策略 - 总结

1、把经常更新的资源放在一个单独的包里面,跟不经常更新的包分离
2、把需要同时加载的资源放在一个包里面
3、可以把其他包共享的资源放在一个单独的包里面
4、把一些需要同时加载的小资源打包成一个包
5、如果对于一个同一个资源有两个版本,可以考虑通过后缀来区分 v1 v2 v3 unity3dv1 unity3dv2

创建AseetsBundle包

1、设置支援AB包名

unity2021 build 图标不正常_包名

2、打包方法

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

public class CreatAseetsBundle :MonoBehaviour
{
    static string path = Application.dataPath + "/ABFile";
    [MenuItem("AseetsBundle/Android")]
	static void BuildAseetsBundleForAndroid()
    {
        Debug.LogError("CC ——————> path: " + path);
        //如果文件不存在
        if (!Directory.Exists(path))
        {
            //创建文件夹
            Directory.CreateDirectory(path);
        }

        BuildPipeline.BuildAssetBundles(path, BuildAssetBundleOptions.None, BuildTarget.Android);
    }
}

加载AssetBundle包

using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;

public class LoadAssetBundle : MonoBehaviour
{
    string resPath = "";
    string path = "";
    void Start ()
    {
        resPath = Application.dataPath + "/ABFile/materials/material.ab";
        Debug.LogError("YC ——————> resPath: " + resPath);
        path = Application.dataPath + "/ABFile/prefabs/obj.u3d";
        Debug.LogError("YC ——————> path: " + path);

        //加载Manifest文件 获取依赖
        LoadAssetBundleManifest();

        //异步本地加载AB
        //ABLoadFromFileAsync();
        //同步本地加载AB
        //LoadFromFile();

        //异步内存加载AB
        //ABLoadFromMemoryAsync();
        //同步内存加载AB
        //LoadFromMemory();

        //www缓存加载
        //ABLoadFromCacheOrDownload();

        //UnityWebRequest加载
        ABUnityWebRequest();
    }


    #region 加载Manifest文件
    /// <summary>
    /// 加载Manifest文件
    /// 获取当前AB包所依赖的AB包
    /// </summary>
    void LoadAssetBundleManifest()
    {
        string manifestPath = Application.dataPath + "/ABFile/ABFile";

        //1. 先加载AssetBundle
        AssetBundle manifestAB = AssetBundle.LoadFromFile(manifestPath);
        //2. 通过AssetBundle加载自身的manifest文件
        AssetBundleManifest manifest = manifestAB.LoadAsset<AssetBundleManifest>("AssetBundleManifest");

        //获取所有的AB包名
        foreach (string name in manifest.GetAllAssetBundles())
        {
            Debug.LogError("CC ——————> AB包名: " + name);//输出AB包名
        }

        //3. 通过AB包包名 获得所有依赖的AB包名
        string[] strs = manifest.GetAllDependencies("prefabs/obj.u3d");
        Debug.LogError("CC ——————> GetAllDependencies: " + strs.ToString());
        foreach (string name in strs)
        {
            Debug.LogError("CC ——————> 加载依赖包: " + name);
            //通过包名 加载AB包
            //解决加载依赖的问题
            AssetBundle.LoadFromFile(Application.dataPath + "/ABFile/" + name);
        }
    }
    #endregion




    #region LoadFromFileAsync 异步加载本地
    private void ABLoadFromFileAsync()
    {
        StartCoroutine(LoadFromFileAsync());
    }

    IEnumerator LoadFromFileAsync()
    {
        //加载AB资源
        //创建AB的请求
        AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(path);
        //使用协程的方法,等待请求完成
        yield return request;
        AssetBundle ab = request.assetBundle;

        foreach (var item in ab.LoadAllAssets())
        {
            Debug.LogError("CC ——————> LoadFromFileAsync 对象: " + item.name);
        }

        //使用AB里面的资源
        //加载资源中名字是Cube对象
        GameObject wallPrefab = ab.LoadAsset<GameObject>("Cube");
        //实例化
        Instantiate(wallPrefab);
    }
    #endregion



    #region LoadFromFileAsync 同步加载本地
    //本地文件加载 本地加载推荐使用
    private void LoadFromFile()
    {
        //AssetBundle res = AssetBundle.LoadFromFile(resPath); //资源依赖的需要 先加载出来
        AssetBundle ab = AssetBundle.LoadFromFile(path);

        foreach (var item in ab.LoadAllAssets())
        {
            Debug.LogError("CC ——————> LoadFromFile 对象: " + item.name);
        }

        GameObject cub = ab.LoadAsset<GameObject>("Cube");
        Instantiate(cub);
    }
    #endregion



    #region LoadFromMemoryAsync 异步加载内存
    //异步加载内存
    private void ABLoadFromMemoryAsync()
    {
        StartCoroutine(LoadFromFileAsync());
    }

    IEnumerator LoadFromMemoryAsync()
    {
        //创建AB的请求
        AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path));
        //使用协程的方法,等待请求完成
        yield return request;
        //得到加载的AB对象
        AssetBundle ab = request.assetBundle;

        foreach (var item in ab.LoadAllAssets())
        {
            Debug.LogError("CC ——————> LoadFromMemoryAsync 对象: " + item.name);
        }

        //使用AB里面的资源
        //加载资源中名字是Cube对象
        GameObject cub = ab.LoadAsset<GameObject>("Cube");
        //实例化
        Instantiate(cub);
    }
    #endregion



    #region LoadFromMemory 同步加载内存
    //同步加载内存
    private void LoadFromMemory()
    {
        //加载AB资源
        //同步加载内存 ,之间返回AB对象
        AssetBundle ab = AssetBundle.LoadFromMemory(File.ReadAllBytes(path)); ;

        foreach (var item in ab.LoadAllAssets())
        {
            Debug.LogError("CC ——————> LoadFromMemory 对象: " + item.name);
        }

        //使用AB里面的资源
        //加载资源中名字是Cube对象
        GameObject cub = ab.LoadAsset<GameObject>("Cube");
        //实例化
        Instantiate(cub);
    }
    #endregion



    #region LoadFromCacheOrDownload www缓存加载
    private void ABLoadFromCacheOrDownload()
    {
        StartCoroutine(LoadFromCacheOrDownload());
    }

    //缓存加载 可本地、可服务器加载
    IEnumerator LoadFromCacheOrDownload()
    {
        //Caching.ready 缓存是否完成
        while (Caching.ready == false)
        {
            yield return null;
        }
        
        //使用本地下载需要file:///前缀
        string uri = @"file:///" + Application.dataPath + "/ABFile/prefabs/obj.u3d";
        WWW www = WWW.LoadFromCacheOrDownload(uri, 1);
        print(uri);

        //服务器加载
        //WWW www = WWW.LoadFromCacheOrDownload(@"http://localhost/ABFile/prefabs/obj.u3d", 1);//下载地址,版本
        //等待加载完成
        yield return www;
        //下载的是否有错误
        if (!string.IsNullOrEmpty(www.error))
        {
            Debug.LogError(www.error);
            yield break;//退出 不在执行下面代码
        }

        AssetBundle ab = www.assetBundle;
        foreach (var item in ab.LoadAllAssets())
        {
            Debug.LogError("CC ——————> LoadFromCacheOrDownload 对象: " + item.name);
        }

        GameObject cub = ab.LoadAsset<GameObject>("Cube");
        Instantiate(cub);
    }
    #endregion



    #region UnityWebRequest 服务器加载
    private void ABUnityWebRequest()
    {
        StartCoroutine(UnityWebRequest());
    }

    //从服务器加载  服务器加载推荐使用 
    IEnumerator UnityWebRequest()
    {
        //本地加载路径file:///前缀
        //string uri = @"file:///" + Application.dataPath + "/ABFile/prefabs/obj.u3d";

        // 服务器路径
        // uri 统一资源定位
        string uri = @"http://localhost/ABFile/prefabs/obj.u3d";

        print(uri);

        // 网络服务器 AB加载请求
        UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(uri);
        // 等待加载完成
        yield return request.SendWebRequest();

        //获取AB文件
        //方式一
        AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
        //方式二
        //AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;

        foreach (var item in ab.LoadAllAssets())
        {
            Debug.LogError("CC ——————> UnityWebRequest 对象: " + item.name);
        }

        //同步加载
        //GameObject cub = ab.LoadAsset<GameObject>("Cube");

        //异步加载
        AssetBundleRequest requestAB = ab.LoadAssetAsync<GameObject>("Cube");
        yield return requestAB;
        GameObject cub = requestAB.asset as GameObject;

        Instantiate(cub);
    }
    #endregion
}