AB包的导出与加载

目录

预习以及复习资料

AB包

导出ab包代码:

AB包资源简单加载

ab包依赖加载:(加载包含依赖的ab包内部资源)

异步加载:(利用协程实现,但本质上是真正的异步)

内存分析

AB包文件管理器


预习以及复习资料

https://shenjun-coder.github.io/LuaBook/

AB包

一、简介:

AssetBundle的定义:是把资源文件以某种紧密的方式保存在一起的文件,AB包是独立于游戏主包之外的资源存储文件,在使用时需要单独的下载和加载。

ab包与Resource的区别:

        在存储方面:Resource内部资源,是存储于游戏的发布包中的,而ab包则是存储在游戏主包之外的独立文件夹中

        在加载方面:Resource可以使用Resource.Load()直接加载

                               AB包:第一步:先获取ab包文件(下载,解压streaing拷贝到可写目录)

                                          第二步:通过文件名加载文件或者文件路径加载文件

AB包的介绍及导出工具实现

导出ab包代码:

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using System.IO;

public class ExportAB {
    [MenuItem("AB包/导出")]
    public static void Export()
    {
        //项目的assets目录
        string path = Application.dataPath;
        Debug.Log(path);
        //Debug.LogError(path);
        path = path.Substring(0, path.Length - 6)+"ab";

        //防止路径不存在
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }

        //导出AB包的核心代码,生成ab包文件
        //参数一:导出ab包文件的存储路径
        //参数二:导出选项
        //参数三:导出到哪个平台
        BuildPipeline.BuildAssetBundles(path, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);

        Debug.Log("AB包导出成功");

    }

}

AB包资源简单加载

 void Start()
    {
        //第一步:加载ab包文件
        AssetBundle ab =AssetBundle.LoadFromFile(config.abPathConfig + "ab/ui");
        
        //第二步:根据文件名加载文件
        Sprite sp = ab.LoadAsset<Sprite>("Tex_bone_01");
        
        img.sprite = sp;
        ab.Unload(false);
    }

ab包依赖加载:(加载包含依赖的ab包内部资源)

 相关代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Load : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        //加载包含依赖的ab包内部资源
        //第一步:加载主AB包文件
        AssetBundle abmain = AssetBundle.LoadFromFile(config.abPathConfig + "/ab/ab");
        //第二步:加载主AB包文件的配置文件
        AssetBundleManifest bundleManifest = abmain.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
        //第三步:分析要加载的预制体所在的AB包,依赖哪些ab包
        string[] deps = bundleManifest.GetAllDependencies("test");
        //第四步:加载依赖的ab包文件
        for (int i = 0; i < deps.Length; i++)
        {
            AssetBundle.LoadFromFile(config.abPathConfig + "ab/" + deps[i]);

        }
        //第五步:加载预制体所在的ab包文件
        AssetBundle testAB = AssetBundle.LoadFromFile(config.abPathConfig + "ab/test");
        //第六步:加载预制体
        Object obj = testAB.LoadAsset("Tex_cloth_07_b");
        GameObject gobj = Instantiate(obj) as GameObject;
        gobj.transform.SetParent(GameObject.Find("/Canvas").transform);
        gobj.transform.localPosition = Vector3.zero;
        gobj.transform.localScale= Vector3.one;

    }

    // Update is called once per frame
    void Update()
    {

    }
}

异步加载:(利用协程实现,但本质上是真正的异步)

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class HotUpdate : MonoBehaviour
{
    public Image img;

    void Start()
    {
        StartCoroutine(LoadABAsync());
        
    }

    //异步加载
    IEnumerator LoadABAsync()
    {
        //异步加载
        //第一步:加载ab包
        AssetBundleCreateRequest ab = AssetBundle.LoadFromFileAsync(config.abPathConfig + "ab/ui");

        yield return ab;
        //异步加载文件
        img.sprite = ab.assetBundle.LoadAsset<Sprite>("Tex_bone_01");
    }

}

内存分析

ab包文件的卸载

//参数表示,是否将场景中的通过AB包加载出来的资源,同AB包一起销毁
ab.Unload(false)

//将所有没有再使用的资源进行回收
Resource.UnloadUnusedAssets();

AB包文件管理器

猜你喜欢

转载自blog.csdn.net/qq_53663718/article/details/128280852