U
章 05导出到手机7 课 — 点击打开
05 / 07 · 10 分钟
导出到手机

构建、安装并在真机上调试

到目前为止的项目

Assets/

Scripts/

MobileBoot.csSaveSystem.csScreenLogger.cs

Settings/

URP mobile asset

Assets/ 之外

signed .apk / .aab

项目设置

keystore

你已经有的这一课新增

配置都已完成,Knight 也学会保存进度了。接下来按下那个按钮,把文件装进手机,再学会在它不听话的时候读懂日志。

动态图解

把场景和资源放进去,出来一个可安装的文件包。

构建并运行

  1. 1

    File▸Build Profiles▸Android。还在测试阶段时,先把 Development Build 勾上。

  2. 2

    手机插好之后点 Build And Run,Unity 一步就把构建、安装和启动全做完。

  3. 3

    输出保存成 Builds/game-dev.apk。千万别放在 Assets/ 里面 —— 那样 Unity 会去 import 你自己的构建产物。

  4. 4

    第一次构建要 10–20 分钟。之后设置不变的话只要 1–3 分钟。

签名——人人都忘的那一段

调试构建用的是临时密钥。正式构建必须有你自己的 keystore,没有它你就没法给更新签名。Google Play 可以帮你重置丢失的 upload key,但要提工单、等上好几天——大多数其他商店根本帮不上忙。

  1. 1

    Project Settings▸Player▸Publishing Settings▸Keystore Manager▸Create New,新建一个 keystore。

  2. 2

    把它存在项目文件夹外面,并且在两个地方各留一份备份。这个文件丢了就再也找不回来。

  3. 3

    永远不要把 .keystore 或它的密码提交到 git。现在就往 .gitignore 里加一条 *.keystore。

  4. 4

    要上商店时:取消勾选 Development Build,勾上 Build App Bundle (Google Play),再点 Build。

用 adb 手动安装

terminal
# install, replacing an existing copy
adb install -r Builds/game-dev.apk

# uninstall when the signature changed
adb uninstall com.yourstudio.yourgame

# launch it without touching the phone
adb shell monkey -p com.yourstudio.yourgame 1

# copy a build to the phone's Downloads folder
adb push Builds/game-dev.apk /sdcard/Download/

读设备的日志

游戏在手机上崩了、在编辑器里却跑得好好的 —— 只有 logcat 能告诉你原因是什么。

terminal
# only Unity's messages — this is the one you want
adb logcat -s Unity

# clear the old log first so you only see this run
adb logcat -c && adb logcat -s Unity

# save it to a file to read carefully
adb logcat -s Unity > crash.txt
  • 你写的每一条 Debug.Log 都会出现在这里,在真机上、实时地。

  • 找以 NullReferenceException 开头的行 —— 它下面的堆栈跟踪会写明是哪个脚本、哪一行。

  • 完全没有 Unity 报错信息的崩溃通常是原生层的问题:内存耗尽,或者缺了 ARM64 的库。

给手边没有数据线的人用的屏内日志

ScreenLogger.cs
using System.Collections.Generic;
using UnityEngine;

public class ScreenLogger : MonoBehaviour
{
    private readonly List<string> lines = new List<string>();
    [SerializeField] private int maxLines = 14;

    void OnEnable()  => Application.logMessageReceived += Handle;
    void OnDisable() => Application.logMessageReceived -= Handle;

    void Handle(string message, string stack, LogType type)
    {
        lines.Add($"[{type}] {message}");
        if (lines.Count > maxLines) lines.RemoveAt(0);
    }

    void OnGUI()
    {
        GUI.skin.label.fontSize = 26;
        GUI.Label(new Rect(20, 20, Screen.width - 40, Screen.height - 40),
                  string.Join("\n", lines));
    }
}

上传到商店之前

  • 取消勾选 Development Build —— 开发构建更慢,而且不允许发布。

  • 至少在一台低端机上测一遍。给你写评价的人用的都是便宜手机。

  • 在有刘海的手机和平板上都看一遍 —— UI 锚定的 bug 就是在那儿冒出来的。

  • Play Console 的要求:必须是 AAB、target API level 不能老过一年、要有隐私政策链接、还要一个 512×512 的图标。