Skip to main content

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:

  1. Provide a plugin to handle the communication between C# and the browser.
  2. Register the App Links in the Android manifest file.
  3. Generate the Ed25519KeyIdentity in C# and pass the public key to the Web Brower.
  4. Receive the DelegationChain from the Web Brower and composite the DelegationIdentity from it.
  5. 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 the DelegationIdentity 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 the ClientGenerator 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.

  1. In Unity, open the Assets\Scenes\SampleScene.unity.
  2. Navigate to the AgentAndPlugin in the scene hierarchy.
  3. Update the Greet Backend Canister with your own greeting backend canister id.
  4. Update the Greet Frontend with your own greeting frontend.
  5. If you use a different URL scheme to launch your Unity project in ii_integration_dapp, please update the kAndroidScheme, kAndroidHost and kURLPath in AndroidPostBuildProcessor.cs accordingly.
  6. Naviagte to Editor -> Project Settings -> Android -> Publishing Settings, choose the keystore file which contains the same SHA256 value that you use to in your assetlinks.json file.

After updating your Unity project correctly, please build to Android platform in Unity, just

  1. Go to File -> Build Settings window.
  2. Switch to Android platform.
  3. Click Build to build the Android app, or Click Build 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.