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:
-
A .NET Core project with Cloud Code C# modules that implement Openfort C# SDK methods. Needs to be hosted in Unity Gaming Services.
-
A Unity sample game that connects to
ugs-backend
through Cloud Code. It uses Openfort Unity SDK to have full compatibility withugs-backend
responses.
Application Workflow#
![Integration workflow](https://blog-cms.openfort.xyz/uploads/unity_iap_sample_workflow_7f84b68e28.png)
Workflow#
- Sign in as a new anonymous player
- Create an Openfort player and a custodial account
- Save the Openfort player's information to the Unity player's dashboard
- Initialize Unity In-App Purchasing service
- Now the player can purchase a consumable or a non-consumable in-app product
- If the consumable purchase is successful, some ERC20 tokens will be transferred to its account
- If the non-consumable purchase is successful, the player will mint an NFT
- The player can also retrieve their ERC20 token balance and NFT inventory
Prerequisites#
-
Get started with Openfort
-
Get started with UGS
-
Get started with Google Play Console
Setup Openfort dashboard#
-
This sample requires an NFT contract to run. We use 0xbed6a05ce8719bc00db1cc81a814192c82be1bb1 (contract deployed in 80002 Amoy). You can use it for this tutorial too:
-
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.
-
We aim to cover gas fees for our users when they mint the NFT. Set a new gas policy for that:
Add a rule so the NFT contract uses this policy:
Add also a rule for the ERC20 contract:
-
Enter a name and choose Add 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 Openfort dashboard variables#
Open the solution with your preferred IDE, open
SingletonModule.cs
and fill in these variables:OfApiKey
: Retrieve the Openfort secret keyOfNftContract
: Retrieve the NFT contract API IDOfGoldContract
: Retrieve the ERC20 contract API IDOfSponsorPolicy
: Retrieve the Policy API IDOfDevAccount
: Retrieve the Developer Account API ID
-
Package Code#
Follow the official documentation steps.
-
Deploy to UGS#
Follow the official documentation steps.
Set up unity-client
#
In Unity go to Edit --> Project Settings --> Services and link the unity-client
to your UGS Project:
![Services settings](https://blog-cms.openfort.xyz/uploads/iap_sample_16_c1ac7c4b45.png?updated_at=2023-12-28T15:52:03.478Z)
Select your Environment:
![UGS environment](https://blog-cms.openfort.xyz/uploads/iap_sample_17_e60d56f379.png?updated_at=2023-12-28T15:52:03.577Z)
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](https://blog-cms.openfort.xyz/uploads/iap_sample_18_3c78605c09.png?updated_at=2023-12-28T15:52:03.586Z)
Your UGS project dashboard should look like this:
![License key in UGS dashboard](https://blog-cms.openfort.xyz/uploads/iap_sample_19_6802cc268e.png?updated_at=2023-12-28T15:52:04.490Z)
Test in Editor#
Play the Main scene and you should see the sign-in panel:
![Sign in panel](https://blog-cms.openfort.xyz/uploads/iap_sample_20_8940611454.png?updated_at=2024-01-02T12:23:00.990Z)
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](https://blog-cms.openfort.xyz/uploads/iap_sample_3_478298f237.png?updated_at=2023-12-27T11:22:46.793Z)
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](https://blog-cms.openfort.xyz/uploads/iap_sample_4_a04e9420ed.png?updated_at=2023-12-27T15:44:43.689Z)
If you confirm, after a brief period you should see the Transaction successful message:
![Game Scene](https://blog-cms.openfort.xyz/uploads/iap_sample_5_ac991ecb2a.png?updated_at=2023-12-27T15:48:49.186Z)
You can then click on the inventory icon to see the representation of your on-chain assets:
![Game Scene](https://blog-cms.openfort.xyz/uploads/iap_sample_6_fd93f3f3a2.png?updated_at=2023-12-27T15:51:49.900Z)
In the Openfort Players dashboard, a new player entry should be visible. On selecting this player:
![Player Entry](https://blog-cms.openfort.xyz/uploads/playfab_opensea_img_34_706b0d267e.png?updated_at=2023-11-19T11:06:46.177Z)
You'll notice that a mint
transaction has been successfully processed:
![Mint Transaction](https://blog-cms.openfort.xyz/uploads/ugs_integration_7_75cf7a4264.png?updated_at=2023-12-14T16:05:01.500Z)
Additionally, by choosing your Amoy Account and viewing NFT Transfers, the transaction is further confirmed:
![Etherscan](https://blog-cms.openfort.xyz/uploads/ugs_integration_8_6b345bd148.png?updated_at=2023-12-14T16:05:00.991Z)
Build App Bundle#
In Unity go to Android Player settings and make sure Other Settings looks like this:
![Android Player settings](https://blog-cms.openfort.xyz/uploads/iap_sample_7_e6ec7eb903.png?updated_at=2023-12-28T07:47:59.386Z)
Also, make sure to sign the application with a Keystore in Publishing Settings:
![Application Signing](https://blog-cms.openfort.xyz/uploads/iap_sample_8_ecae38df0e.png?updated_at=2023-12-28T07:47:59.307Z)
Then go to Build Settings, check Build App Bundle (Google Play) and choose Build:
![Build](https://blog-cms.openfort.xyz/uploads/iap_sample_9_6d1e1a5636.png?updated_at=2023-12-28T07:52:15.586Z)
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:
Upload the
.aab
file and then choose Next:If needed, solve pending errors and warnings and then choose Save and publish:
-
Import IAP catalog#
On your Google Play Console app, go to Monetize --> Products --> In-app products and choose Import:
Upload the
GooglePlayProductCatalog.csv
file (which contains all the in-app products) and choose Import:You should see all the products have been created:
Test in Android#
Once the internal testing release is published, you have two options to test:
- Build and run the .apk directly to your device (if the version number is the same as in the internal release).
- Download the app from Google Play through the internal testing link:
![Internal testing link](https://blog-cms.openfort.xyz/uploads/iap_sample_21_f41d2c851f.png?updated_at=2023-12-28T16:06:28.194Z)
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:
- on Twitter at https://twitter.com/openfortxyz
- on Discord: https://discord.com/invite/t7x7hwkJF4
- by email: support+youtube@openfort.xyz