Table of contents
Simple loading of AB package resources
ab package dependency loading: (load internal resources of ab package containing dependencies)
Asynchronous loading: (implemented using coroutines, but essentially true asynchronous)
Preview and Review Materials
https://shenjun-coder.github.io/LuaBook/
AB bag
1. Introduction:
Definition of AssetBundle: It is a file that saves resource files together in a tight way. The AB package is a resource storage file independent of the main game package. It needs to be downloaded and loaded separately when used.
The difference between ab package and Resource:
In terms of storage: the internal resources of Resource are stored in the release package of the game, while the ab package is stored in a separate folder outside the main game package
In terms of loading: Resource can be loaded directly using Resource.Load()
AB package: the first step: first obtain the ab package file (download, decompress and copy to a writable directory)
Step 2: Load the file by file name or file path
Introduction of AB package and implementation of export tool
Export the ab package code:
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包导出成功");
}
}
Simple loading of AB package resources
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 package dependency loading: (load internal resources of ab package containing dependencies)
Relevant code:
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()
{
}
}
Asynchronous loading: (implemented using coroutines, but essentially true asynchronous)
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");
}
}
memory analysis
Uninstallation of ab package files
//参数表示,是否将场景中的通过AB包加载出来的资源,同AB包一起销毁
ab.Unload(false)
//将所有没有再使用的资源进行回收
Resource.UnloadUnusedAssets();