[Huawei Developers] Mayism:
These posts have shown the first few steps of developing a Unity game:

Unity Editor Installation and APK Packaging

How Can I Use the HUAWEI Game Service Demo Provided by Unity

Initializing a Huawei Game and Implementing HUAWEI ID Sign-in Using Unity

I tried to upload the packaged APK file to AppGallery Connect. Let’s see how it went.

Based on Unity documents, I first completed my game information in UDP, and uploaded its demo package. But the upload failed and UdpSdkNotDetectedError was displayed.

I asked Unity technical support for help, and they told me to also integrate the UDP SDK.

The UDP SDK is used for in-app purchases. You can learn more about it from this document:

https://docs.unity3d.com/Packages/com.unity.purchasing.udp@2.0/manual/index.html

Integration of the UDP SDK

According to the Unity documents, there are three ways to integrate the UDP SDK:

Using Unity IAP (Unity IAP adds another layer to the UDP APIs.)

UDP is included in Unity IAP from version 1.22.0 to 1.23.5. If you use the Unity IAP package (1.22.0–1.23.5), do not install the UDP separately.

Using the UDP package

Using the UDP package and Unity IAP

The package of the Unity IAP 2.0.0 and later does not contain the UDP package. You need to download them both.

Latest version of Unity Asset Store (2020–11–24)

UDP version: 2.0.0

Unity IAP version: 2.2.2

Unity official documentation:

https://docs.unity3d.com/Packages/com.unity.purchasing.udp@2.0/manual/index.html

https://distribute.dashboard.unity.com/udp/guideDoc/HUAWEI

Importing a UDP Package

If it’s your first time integrating the UDP SDK, just download the latest version of UDP package from Unity Asset Store.

Here’s the address:

https://assetstore.unity.com/packages/add-ons/services/billing/unity-distribution-portal-138507

Import the resource to Unity Editor.

If you see Unity Distribution Portal here, the resource has been imported.

Associating a Unity Project with a UDP Client ID

Document: https://docs.unity3d.com/Packages/com.unity.purchasing.udp@2.0/manual/games-with-iap.html

Before you integrate Unity IAP, you need to associate your Unity project with a UDP client ID. Follow the instructions below.

Integrating the Unity IAP API.

Reference:

https://docs.unity3d.com/Packages/com.unity.purchasing.udp@2.0/manual/games-with-iap.html

Before You Start

If you’re still confused by now, don’t worry, check the sample code provided by Unity. You can view it here after you downloaded the UDP package.

Integrating and Testing the StoreService.Initialize API

Integration

According to Unity, this API must be called before your game is released to other app stores using UDP.

View attachment 5203261

I will continue with my earlier integrations, and here’s a few tips for you:

1. Add a button. Right-click HeaderPanel and choose UI > Button. Rename it Initialize.

2. Write code for the button.

Key points:

1) Add a namespace for UDP.

View attachment 5203263

2) Declare the Initialize button, the callback listener of the API, and the boolean value.

private static InitListener m_InitListener=new InitListener();
// IAP initialized flag
private static bool m_Initialized;
private static Button initialize_button;

3) Capture the Initialize button in initUI().

initialize_button= GameObject.Find(“Initialize”).GetComponent<Button>();

4) Add the following code to private void initListeners():

initialize_button.onClick.AddListener(() =>
{
Show("starting initialize");
StoreService.Initialize(m_InitListener);
Show("initialize finished");
});

5) If you want the StoreService.Initialize API to be automatically called at app launch, add the highlighted code here:

void Start()
{
initUI();
initListeners();
// Call the Appinit method in your game code
appInit();
// Call the Initialize method in your game code
initialize();
}

private void initialize()
{
m_Initialized = false;
Debug.Log("Init button is clicked.");
Show("Initializing");
StoreService.Initialize(m_InitListener);
}

6) Edit the callback processing logic.

public class InitListener : IInitListener
{
public void OnInitialized(UserInfo userInfo)
{
Debug.Log("[Game]On Initialized suceeded");
Show("Initialize succeeded");
m_Initialized = true;
}

public void OnInitializeFailed(string message)
{
Debug.Log("[Game]OnInitializeFailed: " + message);
Show("Initialize Failed: " + message);
}
}

Packaging and Testing

The following information indicates that the API is called successfully.

Adding a Sandbox Test Account

If the following message is displayed, you need to add a sandbox account to check whether the IAP API is integrated successfully before app release.

Check this document:

https://docs.unity3d.com/Packages/com.unity.purchasing.udp@2.0/manual/creating-a-game-on-udp.html

Make sure to click SAVE after adding the account.

The following information indicates that the API is called successfully.

Integrating and Testing the StoreService.QueryInventory API

API Functions

This API is used to query HUAWEI IAP for any missing transactions.

For consumables, if a product is purchased but not delivered, this API returns the product ID. Which means, it returns the consumable with a payment exception.

For non-consumables, this API returns all non-consumables that have been purchased by the player.

Integration

Similar to the integration of StoreService.Initialize.

Packaging and Testing

When calling this API, you can pass the product ID you want to query. If you pass one, you’ll see the corresponding information here.

After launching the app, you’ll see the following screen:

Creating a Product

To better test the IAP API, you can create a few products. Check this document:

https://docs.unity3d.com/Packages/c...#managing-in-app-purchases-on-the-udp-console

Here, I created two products.

Note: Currently, Unity IAP does not support subscriptions for games released on HUAWEI AppGallery.

Integrating and Testing the StoreService.Purchase API

Integration

Similar to the integration of StoreService.Initialize.

Packaging and TestingNon-consumables

StoreService.Purchase(“non_consumable_product01”, “payload”, m_PurchaseListener);

Here, non_consumable_product01 is a non-consumable I created in UDP.

After a successfull purchase is successful, call the API again. The following message is displayed.

Call StoreService.QueryInventory(productIds,m_PurchaseListener);.

Define productIds as follows:

List<string> productIds = new List<string> { “consumable_product01”, “non_consumable_product01” };

Now, you can see the information of the two products I created and the non-consumable I just purchased.

Consumables

After a consumable is purchased, it will be delivered and consumed.

If a consumable fails to be delivered after being purchased, you can call the StoreService.QueryInventory API to query its information, which is similar to HUAWEI IAP.

1) The consumable is not consumed after purchase.

Call this API:

StoreService.Purchase("non_consumable_product01", "payload", m_PurchaseListener);

A message is displayed, indicating that the product needs to be consumed before another purchase.

2) StoreService.QueryInventory(productIds,m_PurchaseListener);

Define productIds as follows:

List<string> productIds = new List<string> { "consumable_product01", "non_consumable_product01" };

The consumable is included in the purchased products that are returned.

3) Call this API to consume the product:

StoreService.ConsumePurchase(PurchaseInfo,IPurchaseListener)

According to the official document, PurchaseInfo is returned using the onPurchase method. You can obtain this method from the callback of StoreService.QueryInventory or StoreService.Purchase.

Here, I called the StoreService.QueryInventory API, obtained the consumable, and consumed it:

StoreService.ConsumePurchase(inventory.GetPurchaseInfo("consumable_product01"), this);

The following information indicates that the product is consumed.

Note:

After the consumption, if you call the StoreService.QueryInventory API again, you’ll see that there’s no consumable left. The query and redeliver process is complete.

Server APIs involved in Unity IAP

According to Unity, if the server APIs fail to receive the callback, no matter what the payment result is, from an app store, payment failure will be returned by UDP. Unity also provides some server APIs for you to integrate.

I’m not going to show you the demo test. You can try it on your own.

Payment Callback API

If you have your own game server, you can receive successful payment notifications sent by UDP.

For details, check this document:

https://docs.unity3d.com/Packages/c...ames-with-iap.html#game-client-implementation

Configure the callback URL here.

Order Query API

You can also send an HTTP GET request for order details and verify its information.

For details, check this document:

https://docs.unity3d.com/Packages/c...ames-with-iap.html#server-side-implementation