Unity Android sample
This sample demonstrates one way to integrate Identity Integration with Unity on Android. If you never play with Unity Android, please read Getting started with Android document.
Overview
In this example, you can learn how to communicate with the IC from C#. As we described in Internet Identity Integration, the game mainly focuses on:
- Provide a plugin to handle the communication between C# and the browser.
- Register the App Links in the Android manifest file.
- Generate the
Ed25519KeyIdentity
in C# and pass the public key to the Web Brower. - Receive the
DelegationChain
from the Web Brower and composite theDelegationIdentity
from it. - Use the
DelegationIdentity
to communicate with the backend canister.
Key Files
There're several important files in this example.
AndroidPostBuildProcessor.cs
This is a Unity post-build processor for Android platform, which inserts the App Links URL scheme into the AndroidManifest.xml.DeepLinkPlugin.cs
It's a plugin which provides functions to open the web browser, handle the App Link activation, and composite theDelegationIdentity
from Json string.GreetingClient.cs
This provides the C# APIs for calling the Greeting backend canister in ii_integration_dapp. It can be generated by running theClientGenerator
provided by ICP.NET.TestICPAgent.cs
This is a testing script which can parse the delegation identity and call the APIs provided by GreetingClient.cs to communicate with the greeting backend canister.
Deployment
The current Unity project is configured to use the deployed dapp, its assetlinks.json
file contains the SHA256
value from the test.keystore file (the passwords for the keystore and ii-test
key are both unity123456
). If you deploy your own dapp by following ii_integration_dapp, you need to make some modifications before you start to build this project to Android platform in Unity.
- In Unity, open the
Assets\Scenes\SampleScene.unity
. - Navigate to the
AgentAndPlugin
in the scene hierarchy. - Update the
Greet Backend Canister
with your own greeting backend canister id. - Update the
Greet Frontend
with your own greeting frontend. - If you use a different URL scheme to launch your Unity project in ii_integration_dapp, please update the
kAndroidScheme
,kAndroidHost
andkURLPath
in AndroidPostBuildProcessor.cs accordingly. - Naviagte to
Editor -> Project Settings -> Android -> Publishing Settings
, choose the keystore file which contains the sameSHA256
value that you use to in your assetlinks.json file.
After updating your Unity project correctly, please build to Android platform in Unity, just
- Go to
File -> Build Settings
window. - Switch to
Android
platform. - Click
Build
to build the Android app, or ClickBuild And Run
to build and run on the connected device.
Verification
While installing your app on your Android devices, the Android installer will fetch the assetlinks.json file from the website and verify the SHA256
value. But sometimes the verification might fail due to reasons like an unstable network. Once you find the Android App Links doesn't work as you expected, you can run the below adb command to retrieve the domain verification status of your apps.
adb shell pm get-app-links <your-package-name>
The below is the result of running this command against this sample Unity project, with package name com.dfinity.ii_applink_integration
.
PS ..> adb shell pm get-app-links com.dfinity.ii_applink_integration
com.dfinity.ii_applink_integration:
ID: 9be82cc2-52ba-4e91-a152-5ba976d6176a
Signatures: [A3:E2:36:BC:E9:04:3F:8F:A9:C5:9B:B5:FE:89:95:C8:08:BA:35:2D:07:D8:76:13:65:A9:27:D6:33:6B:44:6E]
Domain verification state:
6x7nu-oaaaa-aaaan-qdaua-cai.icp0.io: verified
The Domain verification state
of the sample dapp should be verified
to have Android App Links working properly.