The Chinese market for mobile games and applications remains largely closed to the rest of the world. Not long ago, Unity announced a collaboration with Xiaomi, designed to help developers publish their games using Unity services directly to the Xiaomi Mi Game Center. In the meantime, on the official website hangs a sign “The function is not yet included, wait for the news!”, We can prepare our build for Xiaomi. And if you're lucky, then successfully publish!

What is the article about?
The most fragile components when porting any game to a new platform are in-game purchases (IAP), advertising (Ads) and game services (ratings, achievements, multiplayer, social integration, etc.). The features of the implementation of these components in the Xiaomi Mi Game Center and will be discussed further.
Disclaimer
Please note that due to the active development of the Xiaomi platform and its integration with Unity, some of the information from this article may lose relevance. I am talking about what was relevant at the end of 2017. I also apologize to Unity specialists for quoting personal correspondence fragments. These fragments do not contain personal information, are for informational purposes only to a wide range of developers, and can be deleted upon request.
')
Features Xiaomi Mi Game Center
Google Play and Facebook are not available in China. This means that the integration of Google Play and Facebook services will not work. If you have your own implementation of authorization of players, game ratings, achievements and everything else, then you are lucky. In addition, the platform is very damp, and the rake will be many. In terms of monetization, everything is simple - Xiaomi implemented its payment service with authorization, whose support has already appeared in the Unity IAP, and advertising is available only through Unity Ads. Other advertising services, even if they are available in China, are not allowed to use.
Unfortunately, it can be included in the Xiaomi build.
In addition, for Unity Ads you will need to get a separate ID specifically for Xiaomi. Unity Ads support immediately warned that at the moment filling is extremely low.
Unity Ads for Xiaomi platform is super low.
Super, then super, go ahead)
First thing
Organizational moments - the most difficult. There will need to write and communicate a lot. First you need to contact the support of Unity and express your desire to publish the game in the Xiaomi Mi Game Center. At the time of this writing, all server-side actions were performed manually by Unity experts. We need to start a project and in-app purchases and get App ID, App Key, App Secret and SKU of all gaming purchases. You will also need to get a new ID for Unity Ads (the old ID for Google Play cannot be used), but this will be discussed in the next section.
I focus on two points:
- The Xiaomi Bundle ID must be different from the Google Play Bundle ID. We added the .mi registry at the end, it turned out to be com.herocraft.game.free.taptapbuilder.mi
- for SKU gaming purchases there is no such limit, but their length is limited to 40 characters. In our case, the SKUs also became different.
Then we will be able to go to the
id.unity.com/en/user_clients/settings page and get all the parameters for configuring the IAP. The Unity Project ID is the id of our project in Unity (the old project that already uses the Unity services), for example 9f379fb5-39b9-43c9-850c-b4a855e3b82a. Immediately you need to fill out previously issued App ID, App Key, App and save. I did not do this, and then I had to search for a problem for a long time = (

By the way, a few days ago, experts from the Middle Kingdom sent a letter:
The Mi-Unity developer portal is available now at developer.unity.mi.com
Alas, while there is empty and our project is not. It remains to hope that all this routine manual work will be automated in the future, and we will get a convenient developer console. But it is unlikely to happen right tomorrow)
Getting a new ID for UnityAds
New ID for UnityAds is required. You cannot use the ID from Google Play, because Xiaomi is a separate platform in the admin panel of Unity Ads. At the moment, the Xiaomi platform is hidden for all developers, and first you need to ask to enable it for your Unity account. To do this, we write a letter in support of unityads-support@unity3d.com with the following content:
Hi! Could you please tap the Tap Builder Mi Game Center for my project (a0853b2f-b5d8-450f-9f8c-5c701ef73c16)? I want to release my app in Xiaomi app store. Here is my Developer ID 26590 for confirmation. Thanks!
Be sure to include the name and ID of the project, as well as your Developer ID (which is displayed on the Invoicing page in your account). After you enable Mi Game Center, you can add it in the project settings as a separate platform, and then get an ID to initialize Unity Ads. It looks like this:

Done! Now the simplest thing is technical aspects.
Technical aspects
So, let's say that you are already using Unity IAP in your project. If this is not the case, then everything can be somewhat more complicated. First, I was immediately confused by some confusion with the plug-ins. In the Build Settings on the Android tab, we have a button Xiaomi Game Center, do not click Add!

There is also an
integration project on GitHub , do NOT go there!
And then there are plugins for Xiaomi in the Unity Asset Store. Also do not import anything!
All we need is the latest version of Unity IAP and Unity Ads (Services tab in Unity)!

I would also like to note that if you see the name Unity Channel, this is integration with Xiaomi. I do not know why they did not come up with a more obvious name) Maybe this is something more general in its idea.
Configure IAP
You need to create an IAP directory and start all purchases, if you have not done so before. In Store ID Overides you need to make a SKU, if it is different from what you had on Google Play. In our case, we had to reduce the SKU to fit in 40 characters.

When finished, you need to click App Store Export. If everything goes smoothly, then you can go further.
Important note from the Chinese:
Google Play and App Store I Xiaomi doesn’t support this feature. That's why Xiaomi IAP needs that “MiGameProductCatalog.prop” file. IAP products locally.
Those. it is important to fill in everything correctly, as well as to indicate in advance the agreed price (you did this when you started the purchase). I was wrong with the cost, and then nothing worked.
Switch Unity IAP to Xiaomi:

Next, you need to run the Unity IAP / Receipt Validation Obfuscator and enter our RSA Public Key Client in the bottom field (which was obtained here:
id.unity.com/en/user_clients/settings ).

Checking the digital signature of purchases is absolutely the same as on Google Play, only the key is different. The Chinese love to break the game and get gaming purchases for free. So I recommend making a digital signature verification, although 90% of developers skip this step. And now let's stop on ADVERTISING! You may be interested in my article:
Protection of games and mobile applications from hacking for dummiesFinally write the code
To earn IAP you need to do 4 actions: initialize it, authorize the user, make purchases and verify the digital signature.
IAP Initialization
Since the initialization of the IAP occurs locally in the directory created, it does not require access to the network or authorization of the user.
var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance()); builder.Configure<IUnityChannelConfiguration>().fetchReceiptPayloadOnPurchase = true; var catalog = ProductCatalog.LoadDefaultCatalog(); 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); } } UnityPurchasing.Initialize(this, builder);
Everything is simple, I copied it from IAPDemo, which is inside the Unity IAP plugin. It is important to set fetchReceiptPayloadOnPurchase = true for the server to return a digital signature. If you do not check the CPU, you can not install.
After successful initialization, the IStoreListener.OnInitialized method will be called, which will return all purchases to IStoreController.products.all. Then you can draw the game store UI and display the prices. Yes, in general, you already know this if you use Unity IAP.
User Authorization
Authorization can be performed either when the application is launched (Awake), or when it is required, for example, before making a purchase. I did an authorization upon request, but to simplify the example, let's execute it in Awake:
public class XiaomiIntegration : MonoBehaviour, ILoginListener { public void Awake() { var appInfo = new AppInfo { appId = "", appKey = "", clientId = "", clientKey = "", debug = true }; StoreService.Initialize(appInfo, this); } public void OnInitialized() { StoreService.Login(this); } public void OnInitializeFailed(string message) { } public void OnLogin(UserInfo userInfo) { } public void OnLoginFailed(string message) { } }
After successful authorization, user ID will be stored in userInfo. Alas, to get his name and other information that could be used in ratings and other services is not yet possible. I display this ID in my own rating list instead of the username from my Google Play or Came Center account. Comment of experts from China:
You can get the user id after login, but there is no API.
Mi account isn't like Facebook or Google Plus. It doesn't have a friends list or SNS.
There is one very important nuance that will not allow you to test real purchases if you are not Chinese. Even if you have a Mi account. The fact is that during the first authorization, the user will have to fill out this form:

What it is:
It's “Personal real information registration” that is required by the Chinese government. Players in this form.
"You will not pass!". In the test mode (debug = true during initialization) this form does not appear.
Purchase and signature verification
It's all as usual. It is worth noting that it is necessary to use unified SKU. That is, not those specific (cropped in my case), SKU for Xiaomi, but the universal ID that is used for Google Play and the App Store.
var product = StoreController.products.WithID(sku); StoreController.InitiatePurchase(product);
When the purchase is successful, you can verify its digital signature:
var receipt = args.purchasedProduct.receipt; var validator = new CrossPlatformValidator(GooglePlayTangle.Data(), AppleTangle.Data(), UnityChannelTangle.Data(), Application.identifier); validator.Validate(receipt);
The Validate method will throw an IAPSEcurityException if the CPU fails validation.
Conclusion
Thanks for attention! Put likes, subscribe and something like that) I will compile a list of useful links below.
References
developer.unity.mi.com - Mi-Unity developer portal
unity3d.com/ru/partners/xiaomi/guide - Xiaomi setup guide (not yet available).
id.unity.com/en/user_clients/settings - project settings for Xiaomi
unitytech.imtqy.com/channel/stand-alone-xiaomi-sdk-doc - Xiaomi integration project. May be useful if you are not using Unity IAP. And maybe for some other reason. I can only assume that this project, like the plug-ins in the Asset Store, will be moved to Build Settings / Android / Xiaomi Mi Game Center in the future.
unityads.unity3d.com/admin - from here you can make a request for an ID for Xiaomi, in any case you need guys from Unity Services Support (Unity Ads Support).
UPD 06/21/2018
Alas, the publication in Xiaomi did not meet expectations. It took a lot of effort, and this translation, and communication with the Chinese, and a raw platform with a bunch of flaws and bugs. Another implementation of the requirements of the government (black lists) and extremely low filling of advertising without the possibility of using third-party providers. Even despite the fact that we published on behalf of the major publisher Herocraft, the feature on the page turned out to be quite insignificant, and now we have about 200 installations per day (which looks depressing).
It was also possible to talk with other developers who have decided to try their luck, many of them complain about drugs. All have similar problems and results. So if you still decide, be prepared)