Guides & References

A full guide and reference page for in-app purchases in the Unity Purchase provides the necessary kits and services for a seamless integration.

Unity's In-App Purchase (IAP) system allows developers to sell a variety of items directly within their games. This can include premium content, virtual goods, and subscriptions. Here's a brief guide on how to set up and use Unity IAP:

Understanding Unity IAP

  • Unity IAP lets you integrate in-app purchases into your games to sell digital goods or services.

  • It supports various platforms, including Android and iOS.

How to Login or Sign In

To use Unity Purchases, you need a Unity account. If you don't have one, you can create it at Unity's official website.

  1. Sign Up or Log In:

    • Visit Unity ID to create a new account or log into an existing one.

    • Fill in your details or sign in with your credentials.

  2. Access the Unity Dashboard:

    • Once logged in, access the Unity Dashboard.

    • Navigate to the Projects section to manage your existing projects or create a new one.

Enabling Unity Purchases Service

To enable the Unity Purchases service for your project:

  1. Open Your Project in Unity Editor:

    • Launch Unity Editor(Unity Hub) and open your project.

  2. Go to Services Window:

  3. Select the Purchases Service:

  4. Link or Create a Project ID:

    • If not linked, link your Unity project with a Unity Project ID or create a new one. (Do the Same thing for Organizations)

  5. Enable the Service:

    • Toggle the switch to enable(ON) the Purchases service for your project.

Using Unity Purchases in Unity

To use Unity Purchases in your Unity project:

  1. If doesn't Install the Packages, Install the Unity IAP and UDP Package:

    • Open the Unity Package Manager (Window > Package Manager).

    • Find and install the In App Purchasing package.

    • Find and install the Unity Distribution Portal package.

  2. Configure In-App Purchases:

    • Access the IAP Catalog in the Services window.

    • Define your products (e.g., consumables, non-consumables, subscriptions).

      • You can add products by writing code or go to the Service > In App Purchasing > IAP Catalog and add it.

              var huaweiStore = HuaweiStore.GetInstance();
              iStore = huaweiStore;
              iStore.Initialize(this);
      
              List<ProductDefinition> list = new List<ProductDefinition>();
      
              var coins100 = new ProductDefinition("coins100", ProductType.Consumable);
              var coins1000 = new ProductDefinition("coins1000", ProductType.Consumable);
              var removeAds = new ProductDefinition("removeAds", ProductType.NonConsumable);
              var premium = new ProductDefinition("premium", ProductType.Subscription);
      
              list.Add(coins100);
              list.Add(coins1000);
              list.Add(removeAds);
              list.Add(premium);
      
              ReadOnlyCollection<ProductDefinition> products = new ReadOnlyCollection<ProductDefinition>(list);
              iStore.RetrieveProducts(products);
      
              huaweiStore.LoadOwnedConsumables();
              huaweiStore.LoadOwnedNonConsumables();
              huaweiStore.LoadOwnedSubscriptions();
    • Change the default store to "Unity Distribution Portal".

    • Activate the Huawei Account kit and In-App Purchases.

    • Adding below codes in HMSEditorUtils.cs file for running Unity Purchase

      • CODE:

        coreAssemblyDefinition.versionDefines = enable ? new List<VersionDefine> { new VersionDefine { name = "com.unity.purchasing", expression = "", define = "UNITY_PURCHASING" } } : new List<VersionDefine>();
        coreAssemblyDefinition.references = enable ? new List<string> { "GUID:60bfecf5cb232594891bc622f40d6bed" } : new List<string>();
  3. Implementing Code:

    • In your Unity scripts, use the UnityEngine.Purchasing namespace.

    • Set up a purchasing system, initialize it, and handle purchase events.

    • Look at codes at these links;

  4. Testing Purchases:

    • Unity provides options for testing IAPs on test mobile devices(Do not use Editor with Huawei Services).

    • Ensure to test thoroughly before deploying.

  5. Build and Deploy:

    • Build your project for the desired platform once everything is set up and tested.

    • Deploy it to the respective any stores.

Conclusion

Unity Purchases simplifies the implementation of in-app purchases, offering a unified API for different platforms. Remember to follow platform-specific guidelines for IAPs when deploying to various stores.


Sources🌐

Last updated