Article directory
AB bag
- After the AssetBundle is loaded into the game, the green area is loaded and referenced
- The AB package should be a separate file with the game
The difference between AB package and Resources
- The AB package cannot contain C# files, and AssetBunble can cooperate with Lua to implement resource and game updates
AB package creation
- Multiple selection material, or atlas
- In the lower right corner, new is a name, and these materials are summarized into an AB package
- Need to open gallery
export
- Find an AB directory outside Asset, and then export
- Do not place resources in Resources when making AB packages
/****************************************************
文件:ExportAB.cs
作者:别离或雪
邮箱: [email protected]
日期:#CreateTime#
功能:导出AB
*****************************************************/
using UnityEngine;
using UnityEditor;
using System.IO;
public class ExportAB : MonoBehaviour
{
[MenuItem("AB包/导出")]
public static void Export(BuildTarget platform) {
//项目Assets路径
string path = Application.dataPath;
//C:/Users/sonw/Desktop/unity/Astar RandomTileMap/ab
path = path.Substring(0,path.Length-6)+"ab";//不要Assets
if (!Directory.Exists(path)){
Directory.CreateDirectory(path);
}
Debug.Log(path);
//导出核心代码
//参数一:ab包文件存储路径
//参数二:导出选项
//参数三:平台,不同平台AB包不一样的
BuildPipeline.BuildAssetBundles(
path,
BuildAssetBundleOptions.None,
BuildTarget.StandaloneWindows
);
}
}
load resources
/****************************************************
文件:SimpLoad.cs
作者:别离或雪
邮箱: [email protected]
日期:#CreateTime#
功能:加载AB包
*****************************************************/
using UnityEngine;
public class SimpLoad : MonoBehaviour
{
private void Start() {
//第一步:加载AB文件
AssetBundle ab= AssetBundle.LoadFromFile(Config.ABpath + "/img");
//第二步:加载资源
Sprite sp=ab.LoadAsset<Sprite>("BG 1");
transform.GetComponent<SpriteRenderer>().sprite = sp;
ab.Unload(false);//释放AB包
}
}
public class Config {
//AB包存储路径
public static string ABpath = Application.dataPath.Substring(0, Application.dataPath.Length - 6) + "ab";
}
replace
- After replacing the resource with the original name, package it again
- Two versions of resources are not placed in one package
- reload with new path
- For resources imported into AB, there is no directory inside the package
compression
- no compression
- LZMA compression
- LZ4 compression
dependencies
-
The first package depends on something in another package
-
Example: One package is pictures, and the second package is a prefab that references one package of pictures, which is the dependency
-
Right-click to edit to see dependencies
-
If you want to handle dependencies, you must loadMain AB package, because the dependencies are stored in the main AB package configuration file
export options
multi-platform
[MenuItem("AB包导出/iOS")]
public static void ForiOS() {
Export(BuildTarget.iOS);
}
[MenuItem("AB包导出/Andorid")]
public static void ForAndorid() {
Export(BuildTarget.Android);
}
[MenuItem("AB包导出/Mac")]
public static void ForMac() {
Export(BuildTarget.StandaloneOSX);
}
Dependency loading
-
Load the main AB package
-
Get the configuration file in the main AB package
-
Analyze the AB package where the prefab is located and which AB packages it depends on
-
Loaded AB package
-
Load the AB package where the prefab is located
-
load prefab
-
The same ab package cannot be loaded twice, it needs to be uninstalled and loaded again
public class Config {
//AB包存储路径
public static string ABpath = Application.dataPath.Substring(0, Application.dataPath.Length - 6) + "ab";
}
public class Load:MonoBehaviour {
private void Start() {
//- 加载主AB包
AssetBundle main = AssetBundle.LoadFromFile(Config.ABpath + "ab");
//- 获取主AB包中的配置文件
AssetBundleManifest manifest = main.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
//- 分析预制体所在AB包,依赖哪些AB包
//deps存储所有依赖ab包的名字
string[] deps = manifest.GetAllDependencies("test2");
//- 加载的AB包
for (int i = 0; i < deps.Length; i++) {
AssetBundle.LoadFromFile(Config.ABpath + "/" + deps[i]);
}
//- 加载预制体所在的AB包
AssetBundle test2 = AssetBundle.LoadFromFile(Config.ABpath + "/test2");
//- 加载预制体
Object prefabe = test2.LoadAsset("Image");
GameObject img = Instantiate(prefabe) as GameObject;
}
}
Resources are loaded asynchronously
//异步加载
public class AsyncLoad:MonoBehaviour {
private void Start() {
//同步
GameObject.Find("Img").GetComponent<Image>().sprite = Resources.Load<Sprite>("");
//异步,通过协同,开启一个异步Resource加载资源
StartCoroutine(LoadImage());
//ab包同步
AssetBundle ab = AssetBundle.LoadFromFile(Config.ABpath + "/big");
GameObject.Find("Img").GetComponent<Image>().sprite = ab.LoadAsset<Sprite>("参考图");
//ab包异步
StartCoroutine(LoadAB((AssetBundleCreateRequest abcr) => {
GameObject.Find("Img").GetComponent<Image>().sprite = abcr.assetBundle.LoadAsset<Sprite>("参考图");
}));
}
IEnumerator LoadImage() {
//开启一个异步加载
ResourceRequest rr = Resources.LoadAsync<Sprite>("参考图");
//协同会在加载资源成功后,继续执行(第层呢个封装了线程加载资源)
yield return rr;
//将加载成功的资源,显示出来
GameObject.Find("Img").GetComponent<Image>().sprite = rr.asset as Sprite;
}
IEnumerator LoadAB(Action<AssetBundleCreateRequest> cb) {
AssetBundleCreateRequest abcr = AssetBundle.LoadFromFileAsync(Config.ABpath + "/big");
yield return abcr;
cb(abcr);
}
}
memory analysis
- profiler memory tool
- It will take up memory when loaded into the scene, the file is not counted (not loaded into memory)
- File loading will not take up memory, loading resources will take up memory
- Only releasing resources will reduce memory usage,
- Need to recycle
- Destory does not reclaim memory, Unity is lazy recycling
public void RecyCle() {
//将所有没有在使用的资源回收,回收游离资源,尽量在场景切换时调用
Resources.UnloadUnusedAssets();
}