Unity IAP provides a streamlined channel for developers outside of China to publish apps in the Chinese market. This guide introduces the Unity Channel SDK, and describes the end-to-end process for a developer publishing IAP content to the Xiaomi Mi Game Center platform.
Publishing a game to the Mi Game Center is a 3-step process:
The Mi Game Center is Xiaomi’s official Android store. It allows users to search, browse and purchase products for the Xiaomi platform using a secure payment portal. For more information, visit Unity’s Xiaomi partner site.
Unity Channel is an internal component of Unity IAP that helps developers outside of China access the Chinese app store market by facilitating user login, payment management, and Chinese Government app distribution regulatory approval.
Because of Unity Channel, Xiaomi Mi Pay integration differs from Google Play and iTunes in the following ways:
This section describes the process of configuring your game through the Unity Editor using the Unity IAP SDK:
Xiaomi only supports Android builds. Configuring your Project for Android is a 4-step process.
In the Editor, enable Unity IAP from the Services window (Window > Services; see documentation on Setting up Unity IAP). Be sure to Import the IAP Asset package when prompted (see image below). As of version 1.13.0, the Unity IAP Asset package includes the Unity Channel and Xiaomi SDKs.
If you wish to publish your app to the Mi Game Center without in-app purchasing, install the Xiaomi Unity Channel standalone SDK from the Build Settings window (File > Build Settings) by selecting Android from the Platform menu and selecting Add from the Xiaomi Game Center menu option that appears.
Unity Channel includes an Asset that provides an Editor interface for managing app store credentials and test mode functionality. This AppStoreSettings Asset installs to Assets/Plugins/UnityChannel/XiaomiSupport/Resources when you import the Unity IAP Asset package. You can also create it manually in the Editor by selecting Assets > Create > App Store Settings. Access the interface by selecting the Asset and viewing the Inspector.
Note: The AppStoreSettings Asset is only available in Unity 5.6+.
In order to communicate, the Unity and Xiaomi servers both require unique identifiers for your game. Note that you can retrieve your Unity Client credentials here by pasting your Project ID into the search field. Unity 5.3 or 5.4 users must retrieve and set credentials this way, as they cannot access the AppStoreSettings Asset. The settings depicted in the image above are described below:
You need these credentials and the test mode toggle at various points of the integration process. Note that App Store Settings data updates server-side, as opposed to saving to your client.
If you enabled Unity IAP to import the Xiaomi Asset package, no action is required. Otherwise, see documentation on Setting up Unity IAP to enable the service.
You can also install the latest Unity IAP plugin through the Unity Asset Store.
The Xiaomi client SDK built into Unity IAP depends on Product metadata being available in the client. As such, you must define your Products through the Editor’s IAP Catalog GUI (Window > Unity IAP > IAP Catalog).
Populating the Catalog The IAP Catalog GUI defines Products along with their metadata for the game client’s runtime. Documentation on Codeless IAP contains information on configuring Products through the IAP Catalog. The following Product attributes warrant attention for Xiaomi specifically:
For more information about Product attributes and their parameters, see documentation on Defining Products.
In the Editor, in the IAP Catalog window, export the Catalog by selecting App Store Export > Xiaomi Mi Pay Catalog.
Export the MiGameProductCatalog.prop file to your location of choice. Import your Product catalog on the Xiaomi developer portal by navigating to your Project’s IAP Configuration tab and selecting Import (see section on Importing your IAP Product catalog, below).
Exporting the IAP Catalog also writes a copy of the MiGameProductCatalog.prop file to your Project’s Assets/Plugins/Android/assets/ directory (see image below). Unity IAP uses this file in the IAP Catalog editor and at runtime. It also serves as a default Product catalog for your app if no catalog file is explicitly imported through the Xiaomi developer portal.
Unity IAP typically requires a configuration builder that consumes IAP Catalog data to be parsed for the destination store, followed by the Unity IAP Initialize()
API (see documentation on Initializing IAP).
Xiaomi games require an extra step prior to initializing IAP, because the Mi Game Center requires apps to share their credentials through the login API at launch.
The modified initialization process becomes:
initialize()
API.Execute these steps early in the game’s runtime lifecycle, preferably at launch. You can implement them in the same script.
The following example illustrates how to modify initialization using the Unity Channel SDK to call the login API:
using AppStoreSupport;
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Purchasing;
using UnityEngine.Store;
// Run the script once, at game launch.
public class UnityChannelSample : MonoBehaviour
{
// Xiaomi login requires a login listener.
// Create one by implementing the ILoginListener abstract class included in the Unity Channel SDK.
class SampleLoginListener : ILoginListener
{
public Action initializeIAP;
// Step 1 succeeded; call step 2
public void OnInitialized()
{
Debug.Log("Initialization succeeded.");
UnityEngine.Store.StoreService.Login(this); // If initialization succeeds, initiate Xiaomi login
}
// Step 1 failed; display error message and recourses
public void OnInitializeFailed(string message)
{
Debug.Log("Initialization failed.");
}
// Step 2 completed; call step 3
public void OnLogin(UserInfo userInfo)
{
Debug.Log(string.Format("Login successful: userId {0}, userLoginToken {1}, channel {2}", userInfo.userId, userInfo.userLoginToken, userInfo.channel));
// When login succeeds, proceed to initializing IAP
initializeIAP();
}
// Step 2 failed; display error message and recourses
public void OnLoginFailed(string message)
{
Debug.Log("Login failed.");
}
}
}
Note: You must use all of the functions in the above sample code, which account for all possible steps of the login process. However, you can choose the actions called within each function. In this example, debug text appears in place of action calls. In this example, the OnLogin() method initiates a Unity IAP initialization function.
Next, initialize the Xiaomi API using a login listener and the app credentials stored in your AppStoreSettings Asset:
void Awake()
{
// Create a login listener based on the SamleLoginListener class
SampleLoginListener loginListener = new SampleLoginListener();
// Tie the initializeIAP Action (from the SampleLoginListener class) to a function (defined in the next step)
loginListener.initializeIAP = ConfigureIAP;
// Access the AppStoreSettings Asset to generate AppInfo with your stored credentials. The AppStoreSettings class is part of the AppStoreSupport library.
AppStoreSettings appStoreSettings = Resources.Load<AppStoreSettings>("AppStoreSettings");
// Initialize Xiaomi using the credentials and login listener
UnityEngine.Store.StoreService.Initialize(appStoreSettings.getAppInfo(), loginListener);
}
Upon loading your Project’s AppStoreSettings Asset, the getAppInfo()
function returns credential data for the Mi Game Center store initialization process.
Finally, use a configuration builder to translate Product data from your IAP Catalog for Xiaomi, then initialize Unity IAP:
// The configuration builder requires a store listener.
// Create one by implementing the IStoreListener abstract class.
class StoreListener : IStoreListener
{
private IStoreController controller;
private IExtensionProvider extensions;
// Called when Unity IAP is ready to make purchases.
public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
{
this.controller = controller;
this.extensions = extensions;
}
// Note that this will not be called if Internet is unavailable;
// Unity IAP will attempt initialization until it becomes available.
public void OnInitializeFailed(InitializationFailureReason error)
{
}
// Called when a purchase completes, or Unity IAP encounters an unrecoverable initialization error;
// may be called at any time after OnInitialized().
public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e)
{
return PurchaseProcessingResult.Complete;
}
// Called when a purchase fails.
public void OnPurchaseFailed(Product i, PurchaseFailureReason p)
{
}
}
// Run a configuration builder to define your Products
public void ConfigureIAP()
{
// Load Products from the IAP Catalog
var module = StandardPurchasingModule.Instance();
var builder = ConfigurationBuilder.Instance(module);
var catalog = ProductCatalog.LoadDefaultCatalog();
// Loop through Products in Catalog, extracting metadata to the builder
foreach (var product in catalog.allProducts)
{
if (product.allStoreIDs.Count > 0)
{
var ids = new IDs();
foreach (var storeID in product.allStoreIDs)
{
ids.Add(storeID.id, storeID.store);
}
builder.AddProduct(product.id, product.type, ids);
}
else
{
builder.AddProduct(product.id, product.type);
}
}
// Create a store listener based on the StoreListener class
StoreListener storeListener = new StoreListener();
// Initialize Unity IAP
UnityPurchasing.Initialize(storeListener, builder);
}
The configuration builder references Product IDs and Product Types to automate population of Price, Type, Title, and Description metadata. It uses your IAP Catalog’s IAPProductCatalog.json file and the builder
object instantiated during Unity IAP initialization.
For information on methods for in-game purchase implementation, see documentation on the following:
Test your game’s purchase flows on a non-Xiaomi Android device by toggling test mode in the AppStoreSettings Asset interface. Unity Channel also provides an API for toggling test mode:
AppInfo.debug = true;
The Unity Channel wrapper includes the AppInfo
class, and passes data to the Xiaomi SDK. To submit your app to Xiaomi, you must enable test mode. Test mode also bypasses the credit card requirement for testing purposes. After enabling test mode, build your Project and launch the resulting APK file from an Android device.
To test real purchasing, set test mode to false and test on a Xiaomi device. Please note that real purchasing requires a Xiaomi Mi Pay Account and appropriate currency such as Chinese Yuan.
This section describes the process of submitting your game through the Unity Editor using the Unity IAP SDK:
In the Unity Editor, set Unity IAP to target Mi Game Pay by selecting Window > Unity IAP > Android > Target Xiaomi Mi Game Pay. This enables Xiaomi for the next build of the game’s APK. It also creates the configuration file that informs Unity IAP at runtime to use the Xiaomi Mi Pay native billing API.
Before pushing to Xiaomi, test building your game, either locally or by configuring the Project with an Android build target in Unity Cloud Build (see documentation on Cloud Build).
In the Editor, enable Cloud Build through the Unity Services window (see documentation on Cloud Build implementation).
Upload your build to the build history for your project, using one of two methods:
From the Editor:
From the Unity Cloud Build Developer Dashboard:
Push the the hosted build to Xiaomi’s developer portal, using one of two methods:
From the Editor:
In the Cloud Build Services window, locate the desired build from the build history timeline and select Push to Xiaomi. Verify that you want to push, and that the action completes.
From the Unity Cloud Build Developer Dashboard:
Note: Only the owner of the organization that your project is under will be able to deploy to Xiaomi.
Your UDN credentials grant access to your uploaded Projects in the Xiaomi Unity developer portal. The first time you log in, you must acknowledge Xiaomi’s Terms and Conditions before proceeding to your Projects list. Locate the Project you wish to submit. Its status (highlighted below) will initially read Version1.0: Draft. Select the clipboard icon to view a submission log of your Project’s status changes throughout the process. Select the status link to expand your Project’s metadata details.
The following details are illustrated in the image below:
This section contains the text to display on the Mi Game Center.
This section contains marketing assets for Phone and Tablet devices that display on the Mi Game Center. Note the following guidelines:
When you are ready, select Submit to submit your game to Xiaomi for review. The status indicator for your Project changes to In review. While your Project is in review, you cannot edit its details. Expand the Project and select Cancel to cancel the review and revise the Project’s details.
You need an ISBN publication license to publish your app. Xiaomi provides comprehensive support for the ISBN approval process through the State Administration of Press, Publication, Radio, Film and Television (SAPPRFT).
Xiaomi does not guarantee that the SAPPRFT will approve all games (see SAPPRFT submission guidelines). To meet store and country compliance rules, you may need to make some changes to your game.
Consider the following tips when submitting for Chinese government approval:
Before finalizing your submission, follow these steps to ensure Xiaomi has your most current IAP Product catalog:
When Xiaomi indicates approval, your project status changes to Approved and receives the following credentials:
Enter these credential values in the AppStoreSettings Asset or initialization script, then set test mode to false
. Create a new build of your game, and push this build to Xiaomi as before.
Xiaomi provides a developer contract via email. Once you sign the contract, Xiaomi facilitates the government approval process. It will notify you when the game receives an ISBN license and clearance to publish on the Mi Game Center.
Xiaomi’s content review process typically takes 1–2 business days, unless it recommends changes. The government approval process and license distribution, however, can take 4–8 months. For more information on the review process, please see the FAQs section of the Unity-Xiaomi partner page.