xLua | xLua Framework | 1 打包

embedded/2024/10/22 16:37:44/

0. 热更新做什么

1、资源
热更资源和热更步骤(Bundle)

2、代码
Lua与C#的交互(xLua)

3、框架开发流程
第一阶段:Bundle处理
构建;加载;更新
第二阶段:C#调用Lua
Lua加载与管理
Lua绑定与执行
第三阶段:向Lua提供接口
第四阶段:完善与优化

1. 资源目录划分​

2. BundleBuildTool

2.1 AssetBundle & AssetBundleBuild

详见Unity | AssetBundle-CSDN博客

2.2 Bundle Build

查找BuildResources下的资源文件
使用Unity提供发BuildPipeline进行构建

注意:meta不需要打入Bundle

2.3 Bundle Build策略

按文件夹打包:最底层文件夹
优势:bundle数量少,小包模式:首次下载快
劣势:后期更新补丁大

按文件打包:每个文件单独打包
优势:后期更新补丁小
劣势:bundle数量多,小包模式:首次下载稍慢

2.4 程序实现
工具类

PathUtil:用于路径处理

路径字段

项目Assets完整路径;
等待打包的资源路径;
AssetBundle输出路径;
AssetBundle资源路径;

 处理路径方法

获取文件相对Unity/Assets文件夹的相对路径;
获取标准化路径;
获取各类资源的路径;

public class PathUtil
{//项目中Assets文件夹的完整路径public static readonly string AssetPath = Application.dataPath;//需要打Bundle的目录public static readonly string BuildResourcesPath = AssetPath + "/BuildResources/";//Bundle输出目录public static readonly string BundleOutPath = Application.streamingAssetsPath;/// <summary>/// 获取Unity的相对路径/// </summary>/// <param name="path">文件或文件夹的完整路径</param>/// <returns>相对于Assets文件夹的路径</returns>public static string GetUnityPath(string path){if (string.IsNullOrEmpty(path)) return string.Empty;return path.Substring(path.IndexOf("Assets"));}/// <summary>/// 获取标准路径/// </summary>/// <param name="path">文件或文件夹的路径</param>/// <returns>将反斜杠替换为正斜杠的标准化路径</returns>public static string GetStandardPath(string path){if (string.IsNullOrEmpty(path))return string.Empty;return path.Trim().Replace("\\", "/");}
}
BuildTool

在 Unity 编辑器中构建 AssetBundles

public class BuildTool : Editor
{[MenuItem("Tools/Build Bundle/Windows")]static void BundleWindowsBuild(){Build(BuildTarget.StandaloneWindows);}[MenuItem("Tools/Build Bundle/Android")]static void BundleAndroidBuild(){Build(BuildTarget.Android);}[MenuItem("Tools/Build Bundle/iOS")]static void BundleiOSBuild(){Build(BuildTarget.iOS);}static void Build(BuildTarget target){//创建AssetBundleBuild打包列表List<AssetBundleBuild> assetBundleBuilds = new List<AssetBundleBuild>();//填充列表string[] files = Directory.GetFiles(PathUtil.BuildResourcesPath, "*", SearchOption.AllDirectories); //获取BuildResourcesPath目录下所有文件for (int i = 0; i < files.Length; i++)                                                              //根据文件构建AssetBundleBuild对象{//跳过meta文件if (files[i].EndsWith(".meta"))continue;//处理要打包的文件,根据BundleResources中文件创建AssetBundleBuild对象AssetBundleBuild assetBundle = new AssetBundleBuild();                                          //创建一个新的AssetBundleBuild对象string fileName = PathUtil.GetStandardPath(files[i]);                                           //获取标准化后的文件路径Debug.Log("files:" + fileName);                                                                 //Logstring assetName = PathUtil.GetUnityPath(fileName);                                             //获取相对于Assets文件夹的路径assetBundle.assetNames = new string[] { assetName };                                            //设置assetBundle的资源名称string bundleName = fileName.Replace(PathUtil.BuildResourcesPath, "").ToLower();assetBundle.assetBundleName = bundleName + ".ab";                                               //设置assetBundle的名称//将AssetBundleBuild对象加入打包列表assetBundleBuilds.Add(assetBundle);}//检查并初始化输出文件夹if (Directory.Exists(PathUtil.BundleOutPath))Directory.Delete(PathUtil.BundleOutPath, true);Directory.CreateDirectory(PathUtil.BundleOutPath);//构建AssetBundlesBuildPipeline.BuildAssetBundles(PathUtil.BundleOutPath, assetBundleBuilds.ToArray(), BuildAssetBundleOptions.None, target);}
}

3 BuildTool 完善

3.1 在Unity中动态加载资源
3.2 完善bundlle

获取和打包依赖项,生成版本文件

版本文件:
版本号:1.0.1
文件信息:文件路径名 | bundle名 | 依赖文件列表

获取依赖文件方法
使用AssetDatabase.GetDependencies获取指定资源的依赖项存储在files数组中
使用LINQ查询过滤files数组,去除脚本文件和指定文件自身

/// <summary>
/// 获取依赖文件列表
/// </summary>
/// <param name="curFile"></param>
/// <returns></returns>
static List<string> GetDependence(string curFile)
{List<string> dependence = new List<string>();string[] files = AssetDatabase.GetDependencies(curFile);dependence = files.Where(file => !file.EndsWith(".cs") && !file.Equals(curFile)).ToList();return dependence;
}

Build方法循环体中增加

//添加该对象依赖信息
List<string> dependenceInfo = GetDependence(assetName);
string bundleInfo = assetName + "|" + bundleName + ".ab";if (dependenceInfo.Count > 0)bundleInfo = bundleInfo + "|" + string.Join("|", dependenceInfo);bundleInfos.Add(bundleInfo);

 Build方法增加生成版本文件

//构建AssetBundles
BuildPipeline.BuildAssetBundles(PathUtil.BundleOutPath, assetBundleBuilds.ToArray(), BuildAssetBundleOptions.None, target);File.WriteAllLines(PathUtil.BundleOutPath + "/" + AppConst.FileListName, bundleInfos);AssetDatabase.Refresh();

 版本文件belike

Assets/BuildResources/UI/Prefab/testUI.prefab|ui/prefab/testui.prefab.ab|Assets/BuildResources/UI/Res/background.png|Assets/BuildResources/UI/Res/button_150.png
Assets/BuildResources/UI/Res/background.png|ui/res/background.png.ab
Assets/BuildResources/UI/Res/button_150.png|ui/res/button_150.png.ab
Assets/BuildResources/UI/Res/radio_100.png|ui/res/radio_100.png.ab
Assets/BuildResources/UI/Res/slider_handle.png|ui/res/slider_handle.png.ab
Assets/BuildResources/UI/Res/universal_panel_40.png|ui/res/universal_panel_40.png.ab

http://www.ppmy.cn/embedded/86704.html

相关文章

JMeter接口测试:测试中奖概率!

介绍 Apache JMeter 是 Apache 组织基于 Java 开发的压力测试工具&#xff0c;用于对软件做压力测试。JMeter 最初被设计用于 Web 应用测试&#xff0c;但后来扩展到了其他测试领域&#xff0c;可用于测试静态和动态资源&#xff0c;如静态文件、Java 小服务程序、CGI 脚本、J…

linux mysql 添加环境变量

要在Linux上添加MySQL的环境变量&#xff0c;可以按照以下步骤进行操作&#xff1a;打开终端窗口。使用文本编辑器&#xff08;如vi或nano&#xff09;打开~/.bashrc文件&#xff1a;vi ~/.bashrc或nano ~/.bashrc在文件的末尾添加以下内容&#xff1a;export PATH$PATH:/path/…

Spring Boot 项目中使用事件发布和监听来实现消息推送和处理

对于在 Spring Boot 项目内部不同函数间或不同线程间的消息推送和处理机制&#xff0c;您可以使用 Spring 内置的事件发布/订阅机制。这种机制允许不同组件之间异步通信&#xff0c;而不需要它们彼此直接依赖。 以下是一个示例&#xff0c;展示了如何在 Spring Boot 项目中使用…

elementplus菜单组件的那些事

在使用 elementplus 的菜单组件时&#xff0c;我发现有很多东西是官方没有提到但是需要注意的点 1. 菜单组件右侧会有一个边框 设置css .el-menu {border: 0 !important; } 2. 使用其他的 icon 文字内容一定要写在 这个 名字为 title 的插槽中 <el-menu-itemv-for"it…

Common instructions of git(git常见指令)

git add . add your codes to computer staging area 提交你的代码到暂存区 git status view the status of added files in computer staging area 查看暂存区已提交文件状态 git commit -m "message" add your codes to your local branch 添加你的代码到你的本地…

【2024最新华为OD-C/D卷试题汇总】[支持在线评测] 开源项目热度排行榜(100分) - 三语言AC题解(Python/Java/Cpp)

🍭 大家好这里是清隆Coding ,一枚热爱算法的程序员 ✨ 本系列打算持续跟新华为OD-C/D卷的三语言AC题解 👏 感谢大家的订阅➕ 和 喜欢💗 🍿 最新华为OD机试D卷目录,全、新、准,题目覆盖率达 95% 以上,支持题目在线评测,专栏文章质量平均 93 分 最新华为OD机试目录…

国科大作业考试资料-人工智能原理与算法-2024新编-第十二次作业整理

袋子里面有3个有偏差的硬币a、b和c,抛掷硬币正面朝上的概率分别是20%、60%和80%。从袋 子里随机取出一个硬币(3个硬币被取出的概率是相等的),并把取出的硬币抛掷3次,得到抛掷结 果依次是X1 , X2和 X3。 a. 画出对应的贝叶斯网络并定义必要的CPT表。 b. 如果抛掷结果是…

Angular由一个bug说起之八:实践中遇到的一个数据颗粒度的问题

互联网产品离不开数据处理&#xff0c;数据处理有一些基本的原则包括&#xff1a;准确性、‌完整性、‌一致性、‌保密性、‌及时性。‌ 准确性&#xff1a;是数据处理的首要目标&#xff0c;‌确保数据的真实性和可靠性。‌准确的数据是进行分析和决策的基础&#xff0c;‌因此…