Management API Reference

Integrate Android In-App Purchases (IAP) in Unity

Integrate Android IAP in your web3 game.

Overview#

This sample project showcases the Openfort integration with Android In-App Purchasing in Unity.

The sample includes:

Application Workflow#

Integration workflow

Workflow#

  1. Sign in as a new anonymous player
  2. Create an Openfort player and a custodial account
  3. Save the Openfort player's information to the Unity player's dashboard
  4. Initialize Unity In-App Purchasing service
  5. Now the player can purchase a consumable or a non-consumable in-app product
  6. If the consumable purchase is successful, some ERC20 tokens will be transferred to its account
  7. If the non-consumable purchase is successful, the player will mint an NFT
  8. The player can also retrieve their ERC20 token balance and NFT inventory

Prerequisites#

Setup Openfort dashboard#

  • Add an NFT contract

    This sample requires an NFT contract to run. We use 0xbed6a05ce8719bc00db1cc81a814192c82be1bb1 (contract deployed in 80002 Amoy). You can use it for this tutorial too:

    Contract Info
  • Add an ERC20 contract

    This sample also requires an ERC20 contract to run. You can deploy a standard one and then add it to the Openfort dashboard following the same logic as above.

  • Add a Policy

    We aim to cover gas fees for our users when they mint the NFT. Set a new gas policy for that:

    Gas Policy

    Add a rule so the NFT contract uses this policy:

    NFT Policy Rule

    Add also a rule for the ERC20 contract:

    ERC20 Policy Rule
  • Add a Developer Account

    Enter a name and choose Add account:

    Developer account

    This will automatically create a custodial wallet that we'll use to send the ERC20 tokens to the players. IMPORTANT: Transfer a good amount of tokens from the created ERC20 contract to this wallet to facilitate testing.

Set up ugs-backend#

Set up unity-client#

In Unity go to Edit --> Project Settings --> Services and link the unity-client to your UGS Project:

Services settings

Select your Environment:

UGS environment

Now make sure In-App Purchasing is enabled and Current Targeted Store is set to Google Play. Then follow the instructions to set the Google Play License Key to your UGS project:

Google Play License Key

Your UGS project dashboard should look like this:

License key in UGS dashboard

Test in Editor#

Play the Main scene and you should see the sign-in panel:

Sign in panel

Choose Sign in. The first time it will create a new player but the next time it will sign in as the same player. After some authentication-related logs, this panel should appear:

Game Scene

Here you have two options:

  • Purchase ERC20 tokens (x10)
  • Purchase NFT

By clicking any of them, a Fake Store panel will pop up, letting you confirm or cancel the purchase:

Game Scene

If you confirm, after a brief period you should see the Transaction successful message:

Game Scene

You can then click on the inventory icon to see the representation of your on-chain assets:

Game Scene

In the Openfort Players dashboard, a new player entry should be visible. On selecting this player:

Player Entry

You'll notice that a mint transaction has been successfully processed:

Mint Transaction

Additionally, by choosing your Amoy Account and viewing NFT Transfers, the transaction is further confirmed:

Etherscan

Build App Bundle#

In Unity go to Android Player settings and make sure Other Settings looks like this:

Android Player settings

Also, make sure to sign the application with a Keystore in Publishing Settings:

Application Signing

Then go to Build Settings, check Build App Bundle (Google Play) and choose Build:

Build

Set up Google Play Console#

  • Create internal release#

    On your Google Play Console app, go to Release --> Testing --> Internal testing --> Testers and select or create an email list with the emails that will test your app. Then choose Create new release:

    New release

    Upload the .aab file and then choose Next:

    Upload build

    If needed, solve pending errors and warnings and then choose Save and publish:

    Save and publish
  • Import IAP catalog#

    On your Google Play Console app, go to Monetize --> Products --> In-app products and choose Import:

    Create product

    Upload the GooglePlayProductCatalog.csv file (which contains all the in-app products) and choose Import:

    Import products

    You should see all the products have been created:

    Products created

Test in Android#

Once the internal testing release is published, you have two options to test:

Internal testing link

Conclusion#

Upon completing the above steps, your Unity game will be fully integrated with Openfort and Unity In-App Purchasing service. Always remember to test every feature before deploying to guarantee a flawless player experience.

For a deeper understanding of the underlying processes, check out the tutorial video.

Get support#

If you found a bug or want to suggest a new [feature/use case/sample], please file an issue.

If you have questions, or comments, or need help with code, we're here to help: