Version: 2023.2
Setting up Unity IAP
针对 Google Play 应用商店进行配置

针对 Apple App Store 和 Mac App Store 进行配置

简介

本指南介绍如何建立 Unity 游戏与应用内购 (IAP) 商店进行交互所需的数字记录和关系。本指南针对的是 Unity IAP 购买 API。

应用内购 (IAP) 是通过资金交易购买数字商品的过程。一个平台的应用商店会允许购买代表数字商品的产品。这些商品有一个标识符(通常是字符串数据类型)。商品的类型可表示其耐用性:_订阅消耗品_(可以多次购买)以及_非消耗品_(只能购买一次)是最常见的类型。

Apple App Store

准备开始

  1. Write a game implementing Unity IAP. Refer to Unity IAP Initialization.

  2. 准备好游戏的商品标识符以便稍后在 iTunes Connect 中使用。

注册应用程序

  1. Apple Developer Center 中,导航至相应的标识符 (Identifiers) 部分。

  2. 添加新的 App ID 以便向 Apple 创建基本应用程序实体。

    NOTE: Use an Explicit App ID. You cannot use Wildcard App IDs (com.example.*) for applications that use In-App Purchases.

    注意:在 Developer Center 中创建 App ID 后,即可在 iTunes Connect 中使用此 App ID。

  3. 导航至 iTunes Connect,然后创建一个应用程序,从而建立应用商店与您的游戏之间的关系。

  4. 将新创建的 App ID 用作该应用的 Bundle ID。

添加 IAP

  1. 选择 Features,然后使用加号 (“+”) 按钮添加新的应用内购 (In-App Purchase)。

  2. 选择商品类型

  3. 指定商品标识符 (Product Identifier),并根据要求填写其他字段。

    Note: The “Product ID” here is the same identifier used in the game source code, added to the Unity IAP ConfigurationBuilder instance by using AddProduct() or AddProducts().

    Note: When targeting multiple Apple device groups (for example, shipping on both iOS and Mac) Apple requires usage of different, unique product identifiers for each distinct device group. Use Unity IAP’s Purchasing.IDs class and define a one-to-many mapping Product IDs to the store-specific identifiers, and pass that mapping in when initializing IAP.

  4. 结果:

测试 IAP

  1. Create Sandbox Testers using iTunes Connect for use on your test device’s iTunes Account. To do this, go to iTunes Connect > Users and Roles, and choose the plus (“+”) button. You must review Apple’s Sandbox Tester documentation as there are several additional important usage notes, and you must use a real email address to create Testers.

    Note: For more information, refer to the iOS and Mac App Store guides.

    Tip: To simplify managing the email address, use an email service capable of sub-addressing (emailaccount+subaddress@example.com) such as Gmail, iCloud, and Outlook.com. This allows one email account to receive email for multiple sub-addresses.

  2. Complete the user creation wizard.

  3. Build the Xcode project for your game by using Unity.

    Note: Ensure the Bundle Identifier in Unity (Edit > Project Settings, then select the Other Settings category, and navigate to the Bundle Identifier section) matches that used in iTunes Connect.

  4. In your game’s Xcode project, ensure the Team (Project Navigator > your game Target > General > Identity > Team) is set to that of your Apple Developer account.

对于 iOS

  1. 使用目标 iOS 设备来注销任何现有的 Apple ID 帐户。稍后仅在应用程序提示时再以沙盒测试者 (Sandbox Tester) 身份登录。任何后续购买都通过 Apple 沙盒 (Apple Sandbox) 而不是生产商店 (Production Store) 进行。

  2. Build and run the game on your iOS device. UnityPurchasing.Initialize() succeeds if everything has been correctly configured.

  3. 在设备上通过游戏内进行购买来测试 IAP 是否成功。此过程会显示一个经过修改的购买对话框,其中说明正在沙盒环境中进行此购买。系统提示您购买时,请使用 Sandbox User Tester 密码。

    Warning: If the indicator is not present, then an account is charged real money for the product.

对于 Mac

  1. 发布到 Mac 台式机时,请在 Unity 的 Mac Player 设置中选择 Mac App Store Validation

  2. After you have built your App, update its info.plist file with your bundle identifier and version strings. Right-click the .app file and select Show Package Contents, locate the info.plist file and update the CFBundleIdentifier string to your application’s bundle identifier.

  3. 对应用程序进行签名、打包和安装操作。从 OSX 终端运行以下命令(填写相应的 “your.app” 和 “your.pkg”)。

    Tip: To sign the bundle, you might first need to remove the Contents.meta file if it exists: your.app/Contents/Plugins/unitypurchasing.bundle/Contents.meta

    1. codesign -f --deep -s "3rd Party Mac Developer Application: " your.app/Contents/Plugins/unitypurchasing.bundle

    2. codesign -f --deep -s "3rd Party Mac Developer Application: " your.app

    3. productbuild --component your.app /Applications --sign "3rd Party Mac Developer Installer: " your.pkg

  4. To install the package correctly, delete the unpackaged .app file before you run the newly created package and install it.

  5. Applications 文件夹启动该应用程序。首次启动应用程序时,系统会提示输入您的 iTunes 帐户详细信息(稍后可以使用该帐户在沙盒环境中进行购买测试)。

For more information on Apple App Store testing and signing, refer to iOS and Mac Extended Functionality and Building your macOS application.

Setting up Unity IAP
针对 Google Play 应用商店进行配置