跳转至

Unity macOS

将集成 Hive SDK 的 Unity 项目构建为 macOS 应用,并根据分发方式签名。

要求

  • macOS 15 或更高版本
  • 仅支持 Apple Silicon(arm64)构建。
  • 请使用 macOS 版 Unity 编辑器构建。Windows 版 Unity 编辑器不会将 SDK 框架复制到应用包中。

构建前的设置

构建前,请在 Unity 编辑器的 File > Build Settings... 中检查目标平台和播放器设置。

  1. 配置构建。
    • 目标平台:macOS
    • 架构:Apple Silicon
    • Create Xcode Project:保持未选中。
  2. 点击 Build Settings... 窗口左下角的 Player Settings... 按钮,或在 Inspector 窗口中选择 Player,配置播放器。
    • 脚本后端:IL2CPP
    • Bundle Identifier:在 Hive 控制台中注册的应用的捆绑包 ID
  3. 准备 hive_config.xml 文件。在 Unity Config Editor 中选择 macOS,即可生成 Assets/Plugins/macOS/res/hive_config.xml。
Warning

macOS 版 Hive SDK Unity 不支持 Create Xcode Project。如果选中此选项,构建后自动处理中列出的 SDK 文件不会包含在 Xcode 项目中,导致 Xcode 构建的应用无法加载 SDK。请保留此选项为未选中状态,直接构建应用包(.app)。

构建后自动处理

Unity 完成构建后,Assets/Hive_SDK_v4/Editor/PostProcess/HivePostprocess.cs 将以下文件复制到应用包(.app)中。

源文件 应用包内的位置 备注
Assets/Plugins/macOS/res/hive_config.xml Contents/Resources/hive_config.xml
Assets/Hive_SDK_v4/Plugins/desktop/hive_string Contents/Resources/hive_string
Assets/Hive_SDK_v4/Plugins/macOS/libHIVE_SERVICE.dylib Contents/Frameworks/libHIVE_SERVICE.dylib 必需。缺少此文件时,SDK 无法加载。
Assets/Hive_SDK_v4/Plugins/macOS/HIVEUIFramework.framework Contents/Frameworks/HIVEUIFramework.framework 必需。缺少此框架时,SDK 无法加载。
Assets/Hive_SDK_v4/Plugins/macOS/AppleFramework.framework Contents/Frameworks/AppleFramework.framework 用于通过 Apple 登录、Mac App Store 购买和 Age Range。缺少此框架时,仅这些功能无法运行。
Warning

SDK 插件(HIVE_PLUGIN.bundle)在 Contents/Frameworks 目录中查找这些文件。请勿移动复制后的文件。如果缺少必需文件,Unity 控制台会记录错误。遇到此情况时,请重新安装 Hive SDK macOS 软件包。

配置权限

Unity 和 Hive SDK 的构建后处理不会为 macOS 应用创建权限(entitlements)文件。请为应用签名创建权限文件,并根据分发方式和使用的功能添加以下键。Developer ID 分发仅使用该方式支持的键。

  • 文件格式:plist 格式的文本文件,扩展名为 .entitlements。
  • 文件位置:不限。例如,在 Unity 项目目录中保存为 macOS/<APP_NAME>.entitlements。请避免保存在构建输出目录中,以免在下次构建时被覆盖。
  • 用法:按照签名与分发中的说明,使用 codesign 命令的 --entitlements 选项指定此文件。
键 适用功能 Mac App Store、TestFlight Developer ID
com.apple.developer.applesignin 通过 Apple 登录 使用此功能时添加。须在 Apple Developer 网站中为 App ID 启用 Sign in with Apple。 请勿添加。添加后应用将无法启动。
com.apple.developer.declared-age-range Age Range 使用此功能时添加。预置描述文件必须允许此权限。 请勿添加。Age Range 适用于通过应用商店分发的应用,Developer ID 分发不支持 Mac App Store 购买(In-App Purchase)。
com.apple.security.app-sandbox App Sandbox 必需 可选
com.apple.security.network.client Hive SDK 与服务器通信 必需 使用 App Sandbox 时必需
com.apple.security.network.server Google 和 X 登录、身份验证、成人验证、监护人同意 必需 使用 App Sandbox 时必需
com.apple.security.files.user-selected.read-only 选择个人资料图片、在 WebView 中添加附件 必需 使用 App Sandbox 时必需
  • com.apple.security.network.client:缺少此键时,SDK 初始化、登录等操作的服务器通信将失败。
  • com.apple.security.network.server:应用通过本地端口(127.0.0.1)接收外部浏览器的处理结果。缺少此键时,浏览器可能无法打开,或应用可能无法接收结果。
  • com.apple.security.files.user-selected.read-only:缺少此键时,文件选择窗口无法打开。
  • 关于 Mac App Store 分发需额外添加的 com.apple.application-identifier 和 com.apple.developer.team-identifier,请参阅 Mac App Store、TestFlight。Developer ID 分发请勿添加这些键。
Note

各分发方式支持的功能请参阅 Apple 文档 Supported capabilities (macOS)。Developer ID 列显示 Sign in with Apple、In-App Purchase 和 Game Center 均不受支持。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>com.apple.developer.applesignin</key>
    <array>
        <string>Default</string>
    </array>
    <key>com.apple.developer.declared-age-range</key>
    <true/>
    <key>com.apple.security.app-sandbox</key>
    <true/>
    <key>com.apple.security.network.client</key>
    <true/>
    <key>com.apple.security.network.server</key>
    <true/>
    <key>com.apple.security.files.user-selected.read-only</key>
    <true/>
</dict>
</plist>

此处省略了 com.apple.developer.applesignin 和 com.apple.developer.declared-age-range。如果不使用 App Sandbox,也可省略所有 com.apple.security.* 键。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>com.apple.security.app-sandbox</key>
    <true/>
    <key>com.apple.security.network.client</key>
    <true/>
    <key>com.apple.security.network.server</key>
    <true/>
    <key>com.apple.security.files.user-selected.read-only</key>
    <true/>
</dict>
</plist>
Note

Unity 构建的应用未应用 App Sandbox,因此本地运行时可能无法发现缺失的权限。为 Mac App Store 或 TestFlight 启用 App Sandbox 并签名后,SDK 与服务器的通信以及 Google 或 X 登录等功能可能失败。分发前,请使用以下命令确认已签名应用包含 com.apple.security.app-sandbox、com.apple.security.network.client 和 com.apple.security.network.server。

使用以下命令检查权限。

codesign -d --entitlements - <APP_NAME>.app

签名与分发

根据分发方式,在终端中为构建后的应用签名。以下示例使用 --deep 选项,同时为应用包中 Contents/Frameworks 和 Contents/PlugIns 内的框架及库签名。

Mac App Store, TestFlight

准备工作

项目 说明
Apple Distribution 证书 用于为应用签名。请安装到钥匙串中。
Mac Installer Distribution 证书 用于为 .pkg 文件签名。在钥匙串中显示为 3rd Party Mac Developer Installer。
Mac App Store 分发用预置描述文件 在 Apple Developer 网站中使用应用的捆绑包 ID 创建的 .provisionprofile 文件。
权限文件 在配置权限中创建的文件。

使用以下命令查看已安装的证书。

security find-identity -v -p codesigning   # Apple Distribution
security find-identity -v                  # 3rd Party Mac Developer Installer

需要在权限文件中添加的键

为 Mac App Store 分发签名时,除了配置权限中列出的键,还需在权限文件中添加以下两个键。值必须与预置描述文件中的值一致。

    <key>com.apple.application-identifier</key>
    <string><TEAM_ID>.<BUNDLE_ID></string>
    <key>com.apple.developer.team-identifier</key>
    <string><TEAM_ID></string>

签名并创建 .pkg 文件

  1. 确认应用版本。Mac App Store 允许 CFBundleShortVersionString 最多包含三段数字。将 Unity Player Settings 中的 Version 设置为 1.0.0 这样的格式,或使用以下命令修改。

    /usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString 1.0.0" <APP_NAME>.app/Contents/Info.plist
    
  2. 将预置描述文件加入应用包。

    cp <PROFILE_FILE>.provisionprofile <APP_NAME>.app/Contents/embedded.provisionprofile
    
  3. 为应用签名。

    codesign --force --deep --timestamp \
        --sign "Apple Distribution: <TEAM_NAME> (<TEAM_ID>)" \
        --entitlements macOS/<APP_NAME>.entitlements \
        <APP_NAME>.app
    

    如果钥匙串中有两个或更多同名证书,命令将返回 ambiguous 错误。此时请将 security find-identity -v -p codesigning 返回的 40 字符哈希值传给 --sign。

  4. 创建 .pkg 文件。

    productbuild --component <APP_NAME>.app /Applications \
        --sign "3rd Party Mac Developer Installer: <TEAM_NAME> (<TEAM_ID>)" \
        <APP_NAME>.pkg
    
  5. 验证签名。

    codesign --verify --deep --strict --verbose=2 <APP_NAME>.app
    codesign -d --entitlements - <APP_NAME>.app
    pkgutil --check-signature <APP_NAME>.pkg
    
  6. 使用 Transporter 将创建的 .pkg 文件上传到 App Store Connect。

在 Mac App Store 之外分发(Developer ID)

如果通过网站等渠道直接分发应用,请将用 Developer ID 签名的应用提交给 Apple 公证。用户尝试打开未经公证的应用时,Gatekeeper 会阻止启动。

不支持的功能

Apple 不支持通过 Developer ID 分发的 macOS 应用使用以下功能。请查阅 Apple 文档 Supported capabilities (macOS) 中的 Developer ID 列。如果需要这些功能,请通过 Mac App Store、TestFlight 分发。

功能 备注
通过 Apple 登录(Sign in with Apple) Developer ID 预置描述文件不包含 com.apple.developer.applesignin 权限。
Mac App Store 购买(In-App Purchase) 此方式不使用 Mac App Store 购买,因此适用于商店分发应用的 Age Range 也不适用。
Game Center
Warning

如果权限文件包含 com.apple.developer.applesignin 或 com.apple.developer.declared-age-range,使用 Developer ID 签名后应用将无法启动。请从用于 Developer ID 分发的权限文件中删除这些键。

准备工作

项目 说明
Developer ID Application 证书 用于为应用和 .dmg 文件签名。请安装到钥匙串中。无需单独的公证证书。
App Store Connect API 密钥 用于提交公证。需要 App Store Connect 的 用户和访问 > 集成 > App Store Connect API 中创建的 .p8 文件、密钥 ID 和签发者(Issuer)ID。
权限文件 在配置权限中创建的文件。请勿添加 com.apple.developer.applesignin 和 com.apple.developer.declared-age-range。

Developer ID 分发无需预置描述文件。

签名与公证

  1. 为应用签名。缺少 Hardened Runtime(--options runtime)和安全时间戳(--timestamp)时,Apple 会拒绝公证。

    codesign --force --deep --options runtime --timestamp \
        --sign "Developer ID Application: <TEAM_NAME> (<TEAM_ID>)" \
        --entitlements macOS/<APP_NAME>.entitlements \
        <APP_NAME>.app
    
  2. 创建并签名 .dmg 文件。

    mkdir dmg_stage
    ditto <APP_NAME>.app dmg_stage/<APP_NAME>.app
    ln -s /Applications dmg_stage/Applications
    hdiutil create -volname <APP_NAME> -srcfolder dmg_stage -ov -format UDZO <APP_NAME>.dmg
    codesign --force --timestamp --sign "Developer ID Application: <TEAM_NAME> (<TEAM_ID>)" <APP_NAME>.dmg
    
  3. 提交应用进行公证。使用 --wait 选项可等待结果,通常需要几分钟。

    xcrun notarytool submit <APP_NAME>.dmg \
        --key AuthKey_<KEY_ID>.p8 --key-id <KEY_ID> --issuer <ISSUER_ID> \
        --wait
    

    如果结果为 Invalid,请使用以下命令查看拒绝原因。

    xcrun notarytool log <SUBMISSION_ID> --key AuthKey_<KEY_ID>.p8 --key-id <KEY_ID> --issuer <ISSUER_ID>
    
  4. 将公证票据附加到 .dmg 文件,使用户在离线时也能启动应用。

    xcrun stapler staple <APP_NAME>.dmg
    
  5. 验证签名和公证。

    codesign --verify --deep --strict --verbose=2 <APP_NAME>.app
    xcrun stapler validate <APP_NAME>.dmg
    spctl -a -t open --context context:primary-signature -vv <APP_NAME>.dmg
    

    当 spctl 输出 accepted 和 source=Notarized Developer ID 时,即可分发应用。