一、什么是AssetBundle?
定义AssetBundle。
AssetBundle 是一个存档文件,包含可在运行时由 Unity 加载的特定于平台的非代码资源(比如模型、纹理、预制件、音频剪辑甚至整个场景)。AssetBundle 可以表示彼此之间的依赖关系;例如,一个 AssetBundle 中的材质可以引用另一个 AssetBundle 中的纹理。为了提高通过网络传输的效率,可以根据用例要求(LZMA 和 LZ4)选用内置算法选择来压缩 AssetBundle。
解释为什么使用AssetBundles
热更新,缩小包体积
二、导入必要的插件
Unity内置的AssetBundle支持
可以自己在这里设置资源包的所属包,包名不能是大写,大写会自动转为小写:
 
 
推荐第三方插件(如果有的话)
Asset Bundle Browser
可以从Github上下载,可以从package manager下载
https://github.com/Unity-Technologies/AssetBundles-Browser
下载好之后将Editor 文件夹重命名,并放到自己项目的Editor文件夹下。
 
UniTask Unity 异步插件
在unity中可以使用异步的插件,支持很多平台。
https://github.com/Cysharp/UniTask
将这个文件夹与自己的项目中的Editor文件夹合并。
 
插件导入完毕

三、创建AssetBundles
打开window - Asset Bundle Browser 窗口
 
 将需要打包的资源拖进去,Asset Bundle Browser 会自动分析依赖,倒入依赖资源
将需要打包的资源拖进去,Asset Bundle Browser 会自动分析依赖,倒入依赖资源
 示例:
 在场景创建一个image,并将给一个图片,将图片赋值给image,将image作成预制体,然后将image打包
 
 
切换到Build
 
 根据自己的实际需求设置。之后点击Build。

文件介绍:
| 文件名称 | 作用 | 
|---|---|
| image | 包,打包的AB包的包文件 | 
| image.manifest | image 包的依赖文件,让开发者看的 | 
| StandaloneWindows | 主包文件,这个项目的主包 | 
| StandaloneWindows.manifest | 主包的依赖文件,让开发者看的,记录全部ab包的依赖关系 | 


资源下载
这仅仅是示例,如果性能敏感,请参照文档末尾的:“下载方式教程” 中的教程进行更合适的下载方式
从本地(远程)下载AssetBundles
    /// <summary>
    /// 本地下载的链接_webRequest
    /// </summary>
    private string downFileFromStreamingAssetsUrl = "ABPackageFileResources/image";
    /// <summary>
    /// 加载的资源在ab包内的路径
    /// </summary>
    private string assetsFileInABPackagePath = "Assets/AssetsPackage/Image.prefab";
    void Start()
    {
        DownLoadABAssets();
    }
    /// <summary>
    /// 从SteamingAssets下载文件
    /// </summary>
    /// <returns></returns>
    private async UniTask DownLoadABAssets()
    {
        //异步_从AB包加载模型到内存
        var tmp_downFilePath = new System.Uri(System.IO.Path.Combine(
            Application.streamingAssetsPath, downFileFromStreamingAssetsUrl));
        //等待下载资源
        AssetBundle tmp_ABPackage = await DownLoadAssetBundle(tmp_downFilePath.ToString());
        //资源解析
        var tmp_image =
            tmp_ABPackage.LoadAsset<GameObject>(assetsFileInABPackagePath);
        //资源创建
        GameObject tmp_go_Image = GameObject.Instantiate(tmp_image);
        GameObject tmp_go_Canvas = new GameObject("tmp_go_Canvas");
        tmp_go_Canvas.AddComponent<Canvas>();
        tmp_go_Image.name = nameof(tmp_go_Image);
        tmp_go_Image.transform.parent = tmp_go_Canvas.transform;
        //AB包释放资源
        tmp_ABPackage.Unload(false);
    }
    /// <summary>
    /// 下载AB包
    /// </summary>
    /// <param name="assetBundleUrl">ab包路径</param>
    /// <returns></returns>
    private async UniTask<AssetBundle> DownLoadAssetBundle(string assetBundleUrl)
    {
        // 创建UnityWebRequest
        using (UnityWebRequest webRequest = UnityWebRequestAssetBundle.GetAssetBundle(assetBundleUrl))
        {
            // 发送请求并等待完成
            await webRequest.SendWebRequest();
            if (webRequest.result != UnityWebRequest.Result.Success)
            {
                Debug.LogError($"Failed to download AssetBundle: {webRequest.error}");
                return null;
            }
            // 获取AssetBundle
            AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(webRequest);
            if (bundle == null)
            {
                Debug.LogError("Failed to load AssetBundle!");
                return null;
            } // 卸载AssetBundle
            return bundle;
        }
    }
从本地加载AssetBundles
本地的添加一些错误处理。
    /// <summary>
    /// 本地下载的链接_webRequest
    /// </summary>
    private string downFileFromStreamingAssetsUrl = "ABPackageFileResources/image";
    /// <summary>
    /// 加载的资源在ab包内的路径
    /// </summary>
    private string assetsFileInABPackagePath = "Assets/AssetsPackage/Image.prefab";
    void Start()
    {
      //  _ = DownLoadABAssets();
        _ = LoadFromLoadTest();
    }
 
    /// <summary>
    /// 从本地加载文件
    /// </summary>
    private async UniTask LoadFromLoadTest()
    {
        AssetBundleCreateRequest tmp_assetBundleCreateRequest = await LoadFileFromLocal(downFileFromStreamingAssetsUrl);
        if (tmp_assetBundleCreateRequest == null || !tmp_assetBundleCreateRequest.isDone)
        {
            Debug.LogError("Failed to load asset bundle from local path.");
            return;
        }
        AssetBundle tmp_assetBundle = tmp_assetBundleCreateRequest.assetBundle;
        if (tmp_assetBundle == null)
        {
            Debug.LogError("Failed to get the asset bundle.");
            return;
        }
        GameObject tmp_loadedGameObject;
        try
        {
            tmp_loadedGameObject = tmp_assetBundle.LoadAsset<GameObject>(assetsFileInABPackagePath);
            if (tmp_loadedGameObject == null)
            {
                Debug.LogError($"Failed to load asset '{assetsFileInABPackagePath}' from the asset bundle.");
            }
            else
            {
                GameObject tmp_go_Image = GameObject.Instantiate(tmp_loadedGameObject);
                GameObject tmp_go_Canvas = new GameObject("tmp_go_Canvas");
                tmp_go_Canvas.AddComponent<Canvas>();
                tmp_go_Image.name = nameof(tmp_go_Image);
                tmp_go_Image.transform.parent = tmp_go_Canvas.transform;
                
            }
        }
        catch (Exception e)
        {
            Debug.LogError($"Error loading asset from asset bundle: {e.Message}");
        }
        
    }
    /// <summary>
    /// 本地加载
    /// </summary>
    /// <param name="_pathInStreamingAssets"></param>
    /// <returns></returns>
    private async UniTask<AssetBundleCreateRequest> LoadFileFromLocal(string _pathInStreamingAssets)
    {
        string tmp_filePath = Path.Combine(Application.streamingAssetsPath, _pathInStreamingAssets);
        var tmp_assetBundle = AssetBundle.LoadFromFileAsync(tmp_filePath);
        if (tmp_assetBundle == null)
        {
            Debug.LogError($"Failed to load asset bundle from local path: {_pathInStreamingAssets}");
        }
        return tmp_assetBundle;
    }
    
    
大文件下载
没搞明白
获取进度
    private IEnumerator LoadFromLocal()
    {
        string tmp_filePath = Path.Combine(Application.streamingAssetsPath, downFileFromStreamingAssetsUrl);
        AssetBundleCreateRequest tmp_abcr = AssetBundle.LoadFromFileAsync(tmp_filePath);
        while (tmp_abcr.progress <= 0.9f)
        {
            UnityEngine.Debug.Log(tmp_abcr.progress);
            yield return null;
        }
        tmp_abcr.assetBundle.Unload(false);
        yield return null;
    }
UI将TMP字体打包进去解决办法

暂时没有办法[/摊手]
参照文档
Unity 用户手册
https://docs.unity.cn/cn/2021.1/Manual/AssetBundlesIntro.html
https://docs.unity3d.com/cn/2020.1/Manual/AssetBundles-Native.html
下载方式教程
https://docs.unity.cn/cn/2021.1/Manual/AssetBundles-Native.html
Unity Asset Bundle Browser 工具
https://docs.unity.cn/cn/2021.1/Manual/AssetBundles-Browser.html



















