Unity AssetBundle批量打包、加载(场景、Prefab)完整流程

2023-11-02 22:47

本文主要是介绍Unity AssetBundle批量打包、加载(场景、Prefab)完整流程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

1、文章介绍

2、具体思路和写法

        (1)AB包的打包

        (2)AB包的加载

        (3)AB包卸载

3、结语

1、文章介绍

本篇博客主要起记录和学习作用,简单的介绍一下AB包批量的打包和加载AB包的方式,若各位同学有幸看到本篇博客,希望能够对你有所帮助。

2、具体的思路和写法

(1)AB包的打包

先介绍一下打ab包所要用到的api  BuildPipeline.BuildAssetBundle(string outputPath,AssetBundleBuild[] builds,BuildAssetBundleOptions assetBundleOptions,BuildTarget targetPlatform)

参数1:ab包输出路径  参数2:ab包信息(主要是assetBundleName=ab包名字,assetNames=资源名)   注意:assetNames一定是要Assets下的路径,不要使用windows路径,不然打包会报错!!!

    /// <summary>///   <para>Build AssetBundles from a building map.</para>/// </summary>/// <param name="outputPath">Output path for the AssetBundles.</param>/// <param name="builds">AssetBundle building map.</param>/// <param name="assetBundleOptions">AssetBundle building options.</param>/// <param name="targetPlatform">Target build platform.</param>/// <returns>///   <para>The manifest listing all AssetBundles included in this build.</para>/// </returns>public static AssetBundleManifest BuildAssetBundles(string outputPath,AssetBundleBuild[] builds,BuildAssetBundleOptions assetBundleOptions,BuildTarget targetPlatform){BuildTargetGroup buildTargetGroup = BuildPipeline.GetBuildTargetGroup(targetPlatform);return BuildPipeline.BuildAssetBundles(outputPath, builds, assetBundleOptions, buildTargetGroup, targetPlatform);}

打包之前的准备工作:

        一般ToB的小项目会有一些资源迭代的需求,所以场景资源单独放到文件夹中管理,每次有新的迭代时,只对最新版本中的场景资源进行增量打包。

        UI资源同样的道理,但是小项目UI资源不需要分版本管理,除非是企业级的项目需要热更或者需要版本管理,则分版本管理。

 

下面是具体代码:

        打包的时候需要注意的事项,打场景包一定不能压缩,否则会加载不出来要使用BuildAssetBundleOptions.None。打其他资源的时候可以选择LZ4压缩BuildAssetBundleOptions.ChunkBasedCompression。LZ4压缩是LZMA和不压缩之间的折中方案,构建的 AssetBundle 资源文件会略大于 LZMA 压缩,但是在加载资源时不需要将所有的资源都加载下来,所以速度会比 LZMA 快。建议项目中使用它。

using System;
using System.Collections.Generic;
using System.IO;
using NWH.Common.AssetInfo;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;namespace editor.AssetBundle
{public class BuildAssetBundle : Editor{/// <summary>/// 场景资源路径/// </summary>private static string _scenePath = $"{Application.dataPath}/AssetBundle/";/// <summary>/// UI资源路径/// </summary>private static string _uiPath = $"{Application.dataPath}/AssetBundle/Resources/";/// <summary>/// 最终场景包输出目录/// </summary>public static string SceneOutPutPath = $"{Application.persistentDataPath}/assetbundle_orgin";/// <summary>/// 最终prefab包输出目录/// </summary>public static string UiOutputPath = $"{Application.persistentDataPath}/assetbundle_uiorgin";[MenuItem("UnityTools/打包资源")]public static void BuildAssetsBundle(){BuildAllScenes();BuildAllPrefabs();//刷新文件AssetDatabase.Refresh();}private static void BuildAllScenes(){var directorys = Directory.GetDirectories(_scenePath, "V*");var folder = directorys[directorys.Length - 1];//获取指定文件夹下所有的.unity文件var sceneFiles = Directory.GetFiles(folder + $"/Scenes/", $"*.unity",SearchOption.AllDirectories);for (int i = 0; i < sceneFiles.Length; i++){//打包进度EditorUtility.DisplayProgressBar($"正在打包场景中...", sceneFiles[i], 1.0f);if (!Directory.Exists(SceneOutPutPath)){Directory.CreateDirectory(SceneOutPutPath);}//批量打包所有的.unity文件  设置输出路径和输出windows平台AssetBundleBuild buildPacket = new AssetBundleBuild();buildPacket.assetBundleName = $"{Path.GetFileNameWithoutExtension(sceneFiles[i]).ToLower()}.unity3d";buildPacket.assetNames = new string[] { sceneFiles[i].Substring(sceneFiles[i].IndexOf("Assets/")) };var abManifest = BuildPipeline.BuildAssetBundles(SceneOutPutPath,new AssetBundleBuild[]{buildPacket},BuildAssetBundleOptions.None,BuildTarget.StandaloneWindows64);}EditorUtility.ClearProgressBar();}private static void BuildAllPrefabs(){//获取指定文件夹下所有的.prefab文件var uiFiles = Directory.GetFiles(_uiPath, $"*.prefab",SearchOption.AllDirectories);if (!Directory.Exists(UiOutputPath)){Directory.CreateDirectory(UiOutputPath);}List<AssetBundleBuild> buildInfoList = new List<AssetBundleBuild>();for (int i = 0; i < uiFiles.Length; i++){//打包进度EditorUtility.DisplayProgressBar($"正在打包预设中...", uiFiles[i], 1.0f);AssetBundleBuild buildInfo = new AssetBundleBuild();buildInfo.assetBundleName = $"{Path.GetFileNameWithoutExtension(uiFiles[i]).ToLower()}.unity3d";buildInfo.assetNames = new string[] { uiFiles[i].Substring(uiFiles[i].IndexOf("Assets/")) };buildInfoList.Add(buildInfo);AssetBundleManifest buildManifest = BuildPipeline.BuildAssetBundles(UiOutputPath, buildInfoList.ToArray(), BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.StandaloneWindows64);}EditorUtility.ClearProgressBar();}}
}

 打包完成后输出的文件:

(2)AB包的加载

        加载场景和UI资源我使用的是同步加载,需要用到异步加载或者网络加载的同学可以去看看其他的文章介绍。      

AssetBundle.LoadFromFile 从磁盘上的文件同步加载 AssetBundle。该函数支持任意压缩类型的捆绑包。 如果是 lzma 压缩,则将数据解压缩到内存。可以从磁盘直接读取未压缩和使用块压缩的捆绑包。

与 LoadFromFileAsync 相比,该版本是同步的,将等待 AssetBundle 对象创建完毕才返回。

这是加载 AssetBundle 的最快方法。

using System.Collections;
using System.Collections.Generic;
using UnityEditor.VersionControl;
using UnityEngine;
using utils;public class ABMgr : IMgr<ABMgr>
{/// <summary>/// 包路径/// </summary>private string packagePath;/// <summary>/// ab包缓存/// </summary>private Dictionary<string, AssetBundle> abCache;/// <summary>/// 主包/// </summary>private AssetBundle mainAB = null;/// <summary>/// 主包中的配置文件---->用来获取依赖包/// </summary>private AssetBundleManifest manifest = null;protected override void Init(){base.Init();abCache = new Dictionary<string, AssetBundle>();packagePath = $"{Application.persistentDataPath}/assetbundle_uiorgin/";}private AssetBundle LoadABPackage(string abName){AssetBundle ab;if (mainAB == null){mainAB = AssetBundle.LoadFromFile(packagePath + "assetbundle_uiorgin");manifest = mainAB.LoadAsset<AssetBundleManifest>("AssetBundleManifest");}var dependencies = manifest.GetAllDependencies(abName);for (int i = 0; i < dependencies.Length; i++){if (!abCache.ContainsKey(dependencies[i])){ab = AssetBundle.LoadFromFile(packagePath + dependencies[i]);abCache.Add(dependencies[i], ab);}}if (abCache.ContainsKey(abName)) return abCache[abName];else{ab = AssetBundle.LoadFromFile(packagePath + abName);abCache.Add(abName, ab);return ab;}}public T LoadResources<T>(string abName, string resName) where T : Object{AssetBundle ab = LoadABPackage(abName);return ab.LoadAsset<T>(resName);}
}

 ab包加载单例基类:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;namespace utils
{public class IMgr<T> : MonoBehaviour where T: IMgr<T>{private static T instance;public static T Instance{get{if (instance != null) return instance;instance = FindObjectOfType<T>();//防止脚本还未挂到物体上,找不到的异常情况,自行创建空物体挂上去if (instance == null){new GameObject("IMgrTo" +typeof(T)).AddComponent<T>();}else instance.Init(); //保证Init只执行一次return instance;}}private void Awake(){instance = this as T;Init();}protected virtual void Init(){}}
}

 下面随便写一个例子看看加载出来的效果:

 

    // Update is called once per framevoid Update(){if (Input.GetKeyDown(KeyCode.Space)){OnLoadScene();}if (Input.GetKeyDown(KeyCode.A)){var go = GameObject.Find("Canvas")?.gameObject;//加载资源var testui = Instantiate(ABMgr.Instance.LoadResources<GameObject>("testui.unity3d", "testui"));if (testui != null && go != null){testui.transform.SetParent(go.transform);testui.transform.localPosition = Vector3.zero;testui.transform.localScale = Vector3.one;}}}private void OnLoadScene(){var ab = AssetBundle.LoadFromFile($"{Application.persistentDataPath}/assetbundle_orgin/scene1.unity3d");Debug.LogError(ab.name);SceneManager.LoadScene("scene1", LoadSceneMode.Additive);}
(3)AB包的卸载

        在AssetBundle的资源使用完成后,需要对其进行卸载,以释放其占用的内存空间。AssetBundle的卸载主要靠AssetBundle.Unload这个API实现。该方法需要传入一个bool类型的参数,如果传入的是true,则会卸载AssetBundle本身及从AssetBundle加载的全部资源。如果传入的是false,则会保留已经加载的资源。
在大多数情况下都推荐使用AssetBundle.Unload(true),因为如果传入false会
造成内存资源的浪费。

如果不得不使用AssetBundle.Unload(false),则只能用以下两种方式卸载单个对象:

在场景和代码中消除对不需要的对象的所有引用。完成此操作后,调用Resources.UnloadUnusedAssets。
以非附加方式加载场景。这样会销毁当前场景中的所有对象并自动调用Resources.UnloadUnusedAssets。

// 1.解除引用后调用
Resources.UnloadUnusedAssets();
// 2.上文提到的,卸载ab所加载的所有asset
ab.Unload(true);

3、结语

        这篇文章到这里就结束了,主要是记录一下自己在项目中使用到的对场景和UI打AB包用法,后续还会进行更深入的研究资源加密、解密、分类管理等等。希望这篇文章对你有帮助,喜欢的朋友点个赞吧。谢谢。

这篇关于Unity AssetBundle批量打包、加载(场景、Prefab)完整流程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/333820

相关文章

Kotlin运算符重载函数及作用场景

《Kotlin运算符重载函数及作用场景》在Kotlin里,运算符重载函数允许为自定义类型重新定义现有的运算符(如+-…)行为,从而让自定义类型能像内置类型那样使用运算符,本文给大家介绍Kotlin运算... 目录基本语法作用场景类对象数据类型接口注意事项在 Kotlin 里,运算符重载函数允许为自定义类型重

Python datetime 模块概述及应用场景

《Pythondatetime模块概述及应用场景》Python的datetime模块是标准库中用于处理日期和时间的核心模块,本文给大家介绍Pythondatetime模块概述及应用场景,感兴趣的朋... 目录一、python datetime 模块概述二、datetime 模块核心类解析三、日期时间格式化与

使用Python创建一个功能完整的Windows风格计算器程序

《使用Python创建一个功能完整的Windows风格计算器程序》:本文主要介绍如何使用Python和Tkinter创建一个功能完整的Windows风格计算器程序,包括基本运算、高级科学计算(如三... 目录python实现Windows系统计算器程序(含高级功能)1. 使用Tkinter实现基础计算器2.

SpringBoot中四种AOP实战应用场景及代码实现

《SpringBoot中四种AOP实战应用场景及代码实现》面向切面编程(AOP)是Spring框架的核心功能之一,它通过预编译和运行期动态代理实现程序功能的统一维护,在SpringBoot应用中,AO... 目录引言场景一:日志记录与性能监控业务需求实现方案使用示例扩展:MDC实现请求跟踪场景二:权限控制与

SpringBoot整合OpenFeign的完整指南

《SpringBoot整合OpenFeign的完整指南》OpenFeign是由Netflix开发的一个声明式Web服务客户端,它使得编写HTTP客户端变得更加简单,本文为大家介绍了SpringBoot... 目录什么是OpenFeign环境准备创建 Spring Boot 项目添加依赖启用 OpenFeig

Java Spring 中 @PostConstruct 注解使用原理及常见场景

《JavaSpring中@PostConstruct注解使用原理及常见场景》在JavaSpring中,@PostConstruct注解是一个非常实用的功能,它允许开发者在Spring容器完全初... 目录一、@PostConstruct 注解概述二、@PostConstruct 注解的基本使用2.1 基本代

SpringBoot整合mybatisPlus实现批量插入并获取ID详解

《SpringBoot整合mybatisPlus实现批量插入并获取ID详解》这篇文章主要为大家详细介绍了SpringBoot如何整合mybatisPlus实现批量插入并获取ID,文中的示例代码讲解详细... 目录【1】saveBATch(一万条数据总耗时:2478ms)【2】集合方式foreach(一万条数

SpringBoot中配置文件的加载顺序解读

《SpringBoot中配置文件的加载顺序解读》:本文主要介绍SpringBoot中配置文件的加载顺序,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot配置文件的加载顺序1、命令⾏参数2、Java系统属性3、操作系统环境变量5、项目【外部】的ap

SpringBoot多数据源配置完整指南

《SpringBoot多数据源配置完整指南》在复杂的企业应用中,经常需要连接多个数据库,SpringBoot提供了灵活的多数据源配置方式,以下是详细的实现方案,需要的朋友可以参考下... 目录一、基础多数据源配置1. 添加依赖2. 配置多个数据源3. 配置数据源Bean二、JPA多数据源配置1. 配置主数据

将Java程序打包成EXE文件的实现方式

《将Java程序打包成EXE文件的实现方式》:本文主要介绍将Java程序打包成EXE文件的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录如何将Java程序编程打包成EXE文件1.准备Java程序2.生成JAR包3.选择并安装打包工具4.配置Launch4