Unity sample
This sample demonstrates one way to integrate Identity Integration with Unity on mobile platforms. If you never play with Unity Android/iOS, please read Getting started with Android and Gettting Started with iOS.
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 DeepLink URL scheme.
- Generate the
Ed25519KeyIdentityin C# and pass the public key to the Web Brower. - Receive the
DelegationChainfrom the Web Brower and composite theDelegationIdentityfrom it. - Use the
DelegationIdentityto 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 DeepLink URL scheme into the AndroidManifest.xml.iOSPostBuildProcessor.cs
This is a Unity post-build processor for iOS platform, which inserts the DeepLink URL scheme into the pList file.DeepLinkPlugin.cs
It's a plugin which provides functions to open the web browser, handle the DeepLink activation, and composite theDelegationIdentityfrom 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 theClientGeneratorprovided by ICP.NET.TestICPAgent.cs
This is a testing script which calls 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. 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 mobile platforms in Unity.
- In Unity, open the
Assets\Scenes\SampleScene.unity - Navigate to the
AgentAndPluginin the scene hierarchy - Update the
Greet Backend Canisterwith your own greeting backend canister id - Update the
Greet Frontendwith your own greeting frontend - If you use a different URL scheme to launch your Unity project in ii_integration_dapp, please
- Update the
kAndroidScheme&kAndroidHostin AndroidPostBuildProcessor.cs accordingly. - Or update the
kURLScheme&kURLIdentifierin iOSPostBuildProcessor.cs accordingly.
- Update the
After updating your Unity project correctly, please build to the platform you want to build to in Unity, just
- Go to
File -> Build Settingswindow - Switch to
Android/iOSplatform - Click
Buildto build the app, or ClickBuild And Runto build and run on the connected device.