根据项目规模,为 Android 构建应用程序可能会花费相当长的时间。为了在开发过程中更快地进行迭代,您可以选择为应用程序包打补丁,而不是对其进行重新构建。为某个构建版本打补丁后,仅会将脚本相关文件发送到设备。
必须首先构建应用程序,将其安装在设备上,然后才可以为此应用程序打补丁。有关更多信息,请参阅构建 Android 应用程序。
要为应用程序打补丁,请执行以下步骤:
更新脚本文件。
注意:如果您更改附加到某个游戏对象的脚本的布局(例如新增公共变量),则“仅限脚本构建”(Script Only Build) 和打补丁过程会失败。
在 Editor 中,单击 File > Build Settings。
从 Build Type 下拉菜单中,选择 Development。
如果使用的是 IL2CPP 脚本后端(否则,请转到下一步):
选中 Scripts Only Build 复选框。
单击 Patch 或 Patch & Run。
构建更新的脚本文件后,Unity 会将更新的文件发送到 Run Device 字段中指定的设备。
如果您已实施自己的构建管线,可以使用脚本 API,通过将 BuildOptions.BuildScriptsOnly
和 BuildOptions.PatchPackage
选项传递到 BuildPipeline.BuildPlayer
方法来为您的应用程序打补丁。
例如:
BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions();
buildPlayerOptions.scenes = new[] { "Assets/Scene1.unity"};
buildPlayerOptions.target = BuildTarget.Android;
// 正常构建并运行游戏,这会在 Android 设备上安装应用程序
buildPlayerOptions.options = BuildOptions.AutoRunPlayer
BuildPipeline.BuildPlayer(buildPlayerOptions);
// 修改 Unity 项目中的某些脚本
// 为应用程序打补丁并运行此应用程序
//(Unity 仅会重新编译脚本文件,并仅将必要文件推送到 Android 设备)
buildPlayerOptions.options = BuildOptions.BuildScriptsOnly | BuildOptions.PatchPackage | BuildOptions.AutoRunPlayer;
BuildPipeline.BuildPlayer(buildPlayerOptions);
Unity 将包含更新脚本的文件发送到应用程序的缓存文件夹。应用程序启动后,会先检查缓存文件夹,然后加载文件。如果应用程序找到所需文件,会加载缓存文件夹(而不是应用程序的数据文件夹)中的文件。
Unity 发送的文件取决于您使用的脚本后端:
从项目脚本文件、软件包脚本文件或 asmdef 文件编译的托管程序集将发送到 /storage/emulated/0/Android/data/<PackageName>/cache/ScriptOnly/<UnityVersion>/mono/Managed
libil2cpp.so 将发送到 /data/data/<PackageName>/cache/ScriptOnly/<UnityVersion>/il2cpp
__注意:__这是内部缓存。由于 Android 7.0 中引入了安全性增强功能,因此无法从模拟存储中加载动态库。有关其他信息,请参阅 Android 开发者网站上的 Android 7.0 行为变化 (Android 7.0 Behavior Changes)。
IL2CPP 资源文件将发送到 /storage/emulated/0/Android/data/<PackageName>/cache/ScriptOnly/<UnityVersion>/il2cpp
可以使用 Android 存储设置来清除应用程序的缓存,从而删除“仅限脚本构建”(Script Only build) 过程安装的补丁文件。要清除设备上的缓存,请转到设置,然后打开应用程序列表。通常,有一个选项可用于清除存储的数据和/或缓存。在 Android 操作系统的某些实现中,可能需要向下深入到存储选项才能找到可清除缓存的选项。
2018–11–13 页面已发布
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.