A sample game demonstrating use of Google Play technologies on Android with the Unity engine.
This verson of TrivialKart demonstrates:
- In-app purchases through Google Play using the Unity IAP system
- Play Games Services, for signin, achievements, leaderboards, friends and cloud save
- Play Integrity for receiving integrity signals about device integrity and Play license status
- The Input SDK for Google Play Games for PC
- Unity 2020 LTS or higher with Android build support (Play plugins are compatible with earlier versions of Unity, but the TrivialKart project is built using 2020 LTS)
- Google Play developer account
Open the project in Unity and open the TrivialKartScene
file
from Assets/Scenes
.
By default, Google Play featues are disabled. The scene be run in-editor or exported to device and run. With in-app purchase integration disabled, all prices are placeholder and purchasing in-game items automatically succeeds.
In TrivialKart, the player has a vehicle which they can drive by tapping on it. Driving the car uses gas. When the car runs out of gas, to continue driving, more must be purchased using in-game currency. If the player runs out of in-game currency, they may buy more via in-app purchases. In-game currency is an example of a consumable purchase that may be repeated.
The game has different cars available for unlock. One car is purchasable using in-game currency; other cars require unlocking via in-app purchase. These unlocks are permanent one-time purchases.
The game also has subscriptions available for in-app purchase. One subscription unlocks a different travel background. A second, more expensive subscription unlocks the travel background and adds a 40% discount off the cost of purchasing gas or unlocks using in-game currency. The features of a subscription are only available if the subscription is active.
- Go to the Google Play Developer Console and create a new application.
- Select the
Monetization setup
page for your new application. Copy the Base64-encoded public key text in theLicensing
area. You will need to use this key when configuring Unity IAP.
- In Unity, if you haven't already, select
File -> Build Settings...
from the Unity menu bar and switch the platform toAndroid
. - From the
Build Settings
window, select thePlayer Settings...
button. - In the
Player Settings
window, expand theOther Settings
section and enter the application package identifer you specified in the Play Console in thePackage Name
field.
Follow the instructions at Setting up Unity IAP
This sample was tested using version 4.1.2 of the Unity In App Purchasing package installed through the Package Manager. We recommend this as a minimum version for this sample. Use of earlier versions, or the older version of Unity IAP available on the Unity Asset Store is not supported.
NOTE If the Services menu does not appear in your Unity menu bar, try uninstalling and reinstalling the In App Purchasing package from the Unity Package Manager.
- Select
Services -> Unity IAP -> Receipt Validation Obfuscation
from the Unity menu bar. - Paste the Base64-encoded public key you retrieved from the Google Play Developer console into the text box.
- Click the
Obfuscate Google Play License Key
button. - Close the popup window.
- From the Unity menu bar, select
TrivialKart -> BuildOptions -> Build with IAP
This menu item acts as a global toggle, if unchecked NO_IAP
is defined in
the Scripting Define Symbols
. If checked USE_IAP
is defined in the
Scripting Define Symbols
.
- In the Unity
Build Settings
window, make sure theTrivialKart/Scenes/playScene
is added to the list ofScenes In Build
. - Configure the project for signing by creating a new key store at
File > Build Settings > Player Settings > Publishing Settings
. Additional instructions on configurating signing are available. on the Unity site. - Build an App Bundle.
- Use the Google Play Developer Console to upload your build to Google Play. You can upload to the Internal test track.
Note: you must upload a build to Google Play to be able to create your in-app purchasing items in the Google Play Developer Console Console.
-
Return to the Google Play Developer Console.
-
Under
Monetize -> Products -> In-app products
, create the following in-app products:Product ID Price car_offroad $2.99 car_kart $4.99 five_coins $0.99 ten_coins $1.99 twenty_coints $2.49 fifty_coins $4.99 -
Under
Monetize -> Products -> Subscriptions
, create subscriptions with these IDs and prices (Fill out the other fields. Set them to "Active"):Product ID Price silver_subscription $1.99 golden_subscription $4.99 -
Publish your build to the testing channel. It may take up to a few hours to process the build. Running a build before processing complete can result in errors such as Google Play reporting that "this version of the application is not enabled for in-app billing".
-
Add tester accounts to your game. This will allow you to test purchases and subscriptions without being charged. Test accounts also have greatly reduced subscription periods, allowing for easier testing of subscription features.
Follow the instructions for setting up Play Games Services for your app in the Play Console.
You will also need to enable save games since TrivialKart uses the Cloud Save feature.
In the Play Games Services section of your Play Console entry for your app, access the Achievements and Leaderboard sections to add the following achievements and leaderboard:
Achievements
Name: tk_achievement_drive
Description: Drive a while
Initial State: Revealed
Incremental: Unchecked
Points: 5
Name: tk_achievement_truck
Description: Unlock the truck
Initial State: Revealed
Incremental: Unchecked
Points: 5
Leaderboards
Name: tk_leaderboard_distance
Format: Number
Number of decimals 2
Sort order: Largest first
Make sure to publish your achievements and leaderboard after creation.
- Download version 11.0 or later of the Play Games Services for Unity plugin from its GitHub releases page.
- Extract the
.zip
file. - Install the
.unitypackage
file located in thecurrent-build
directory of the extracted archive usingAssets > Import Package > Custom Package
.
- From the Play Console entry for your app, select
Play Games Services -> Setup and management -> Achievements
. - Find the Get resources button and click it.
- Copy the text from the
Android (XML)
tab. - From the Unity menu bar, select
Window -> Google Play Games -> Setup -> Android Setup...
. - Paste the resource text into the Resources Definition field.
- Click the Setup button
- Go to File > Build Settings > Player Settings and click Publishing Settings section.
- Choose Proguard for Minify > Release. Then, enable User Proguard File.
- If you want the plugin to be proguarded for debug apks as well, you can repeat for Proguard for Minify > Debug.
- Copy the contents of the proguard configuration file at
Assets/GooglePlaygames/com.google.play.games/Proguard/games.txt
intoAssets/Plugins/Android/proguard-user.txt
.
From the Unity menu bar, select
TrivialKart -> BuildOptions -> Build with Google Play Games Services
Builds published for testing or distribution on Google Play are resigned by Google. When you configure Play Games Services, these keys are used for authentication. If you run a locally built APK signed with your upload key, Play Games Services won't be able to authenticate. To fix this, you can add the upload key to a new OAuth client for your local builds. To do this:
- In Play Console, go to Setup->App Integrity
- Select the App Signing tab
- Find the Upload key certificate section and copy the SHA-1 certificate fingerprint
- Go to Grow->Play Games Services->Setup and Management->Configuration
- Find the Credentials section and click on Add Credentials
- Give the credential a different name from the existing name
- Click Create OAuth Client and follow the instructions, using the SHA-1 certificate fingerprint from the Upload key
- Click Refresh OAuth clients until the new client is listed in the OAuth client list
- Select the new client from the list and click Save changes
- Return to the Configuration screen and click Review and publish
- On the next screen, click Publish
See the Play Integrity guide for information on configuring Play Integrity in the Google Play Console.
You will need to download the unitypackage
file for Play Integrity from the
Google Play Unity plugins releases page.
After downloading the plugin, install it using using Assets > Import Package > Custom Package
.
The Play Integrity code requires communication with a backend server to:
- Generate a nonce
- Decrypt the integrity verdict
You can find implementation details for these tasks in the Work with integrity verdicts guide.
A server implementation is not included with this sample. You can build and deploy a server using the framework and platform of your choice. TrivialKart expects the server to define two endpoints accessed via HTTP requests:
A getNonce
endpoint, accessed via a GET request. This URL returns a json payload containing
the nonce value. The JSON payload containts a single key/value pair with the key being nonce
.
A processToken
endpoint, accessed via a POST request. The URL expects a JSON payload containing
the encrypted integrity token returned by the Play Integrity plugin. The JSON payload
contains a single key/value pair with the key being tokenString
. The POST request returns a
JSON payload containing the integrity verdict information. This is serialized into the structures
defined in the IntegrityVerdict.cs
source file.
You will need to customize the following lines in PlayIntegrityController.cs
to point to the URLs for your server:
private readonly string URL_GETNONCE = "https://your-play-integrity-server.com/getNonce";
private readonly string URL_PROCESSTOKEN = "https://your-play-integrity-server.com/processToken";
From the Unity menu bar, select
TrivialKart -> BuildOptions -> Build with Play Integrity
An integrity verdict is requested when opening the store page. A verdict will only be requested once per play session. The summary results string is displayed near the top of the store page.
Note that in a real game, you would not directly return the decrypted verdict information, but communicate the results in a way that makes sense for your game. For simplicity, this sample expects the raw verdict json.
- Enable x86 ABI architecture. This can be activated using
Player Settings > Other Settings > Target Architectures
and enabling both x86 (Chrome OS) and x86-64 (Chrome OS). Note: You can only enable x86 support when using the IL2CPP Scripting Backend. This can be done fromPlayer Settings > Configuration > Scripting Backend > IL2CPP
. - Disable unsupported android features and permissions. This can be done by adding custom build logic to your
AndroidManifest.xml
. To do this, openPlayer Settings > Publishing Settings
and find theBuild
section. Then, enable theCustom Main Manifest
or corresponding manifest for the feature or permission you need to disable. At this point, you can find that manifest and addandroid:required="false"
to the<uses-feature>
oruses-permission
declaration for all features that Google Play Games does not support. For more information, see the Unity documention on Android Manifests. You should also disable any lines requesting permissions in your Unity code. See the Unity documentation on requestion permissions for more details. - Install the Input SDK Unity package using
Assets > Import Package > Custom Package
. - From the Unity menu bar, select
TrivialKart > BuildOptions > Build for Google Play Games PC
to enable the appropriate scripting defines. - Create a build using the normal Unity build process.
If you've found any errors or bugs in this sample game, please file an issue.
This is not an officially supported Google product.
If you forget to select the version you want to build before closing Unity, you may be unable to see the TrivialKart > BuildOptions
menu.
To fix it, delete the directives under Edit > Project Settings > Player > Other Settings > Script Compilation
and restart Unity.
2022-04-01: 1.1.0 - Added Play Games Services, Play Integrity and Google Play Games for PC features. 2022-01-27: 1.0.0 - Initial release.
The PNG and font graphic asset files used in TrivalKart are from Kenney's game assets. These files are licensed under a Creative Commons Zero license.