Unity (C#) Desktop - Auth
Interacting with Particle Auth within games built on Unity using C#.
Particle Auth for Unity on Windows/macOS
The Particle Auth Unity SDK is the key tool for integrating Particle’s Wallet-as-a-Service into Unity-based games.
This SDK streamlines configuration, integration, and interaction, making it the only library needed for most applications.
It supports end-to-end usage of Particle Network with email, phone, or custom JWT authentication.
While its structure is similar to the Web (JavaScript/TypeScript) SDK, there are several key differences, detailed below.
Due to limitations with Webview, Particle Auth for Unity on Windows & macOS does not include most built-in social logins
Options include email, phone, and custom JWT authentication (indicated by ‘email’, ‘phone’, or ‘jwt’).
Getting Started
To start with the Particle Auth Unity SDK on Windows and macOS, ensure you meet the prerequisites: a minimum Unity version and a Unity package for handling authentication popups.
Prerequisites
-
Unity 2021.3.35f1 or later.
-
Requires one of the following packages:
-
Recommended: 3D WebView for Windows and macOS (Web Browser) from Vuplex, version 4.3.3 or higher.
-
Other web browser packages will also work here if necessary.
-
Quickstart
Before diving into the specifics of SDK initialization and use, the best way to get started and explore a live implementation of the Unity SDK is through the Windows/macOS demo included within the GitHub repository.
This demo will consist of initializing the SDK and, as previously detailed, a Webview-based authentication menu for MPC-TSS login, enabling interaction with the resulting wallet.
Quickstart demo installation
To start testing the included demo, download the Windows/macOS demo from GitHub.
If you’ve already imported the SDK from the linked repository, follow these steps to initialize the demo:
-
Open the Windows/macOS Auth demo scene in the Unity editor.
-
Click the Particle Init button to initialize the SDK.
-
Click Login to create an account instance and enable the sample functions.
Initialization
After exploring the SDK’s implementation through the demo, let’s understand and dive into the configuration, which includes some structural deviations from the Web SDK.
Initializing the Particle Auth SDK
-
Create a GameObject:
- Add a new
GameObject
to your scene. - Attach the
ParticleSystem
script to this GameObject.
- Add a new
-
Attach Canvas WebView:
- Attach the
CanvasWebViewPrefab
to theParticleSystem
script.
- Attach the
-
Add Particle Unity RPC:
- Put a
ParticleUnityRpc
prefab into your scene. - Update the prefab with your
project id
,app id
, andclient key
.
- Put a
-
Configure the ParticleSystem:
-
Before using the SDK, you must configure the
ParticleSystem
(similar to theParticleNetwork
in the Web SDK). -
Execute this configuration by calling the
Init
method onParticleSystem.Instance
with the following parameters: -
config
: IncludesParticleConfigSecurityAccount
andParticleConfigWallet
. -
theme
: Controls the theme settings such as dark or light mode, whether to display the wallet or show the close button. -
language
: A string indicating the UI language, options include ‘en-US’, ‘zh-CN’, ‘zh-TW’, ‘ja-JP’, or ‘ko-KR’. -
chainInfo
: An object that specifies the primary blockchain to be used.
-
Among these parameters, the ParticleUnityRpc
prefab containing project id
, app id
and client key
are the most important. It directly connects your instance of Particle’s Wallet-as-a-Service with your configurations in the Particle dashboard.
Examples of utilization
Login
ParticleSystem.Instance.Login
displays the login popup, set to ‘email’, ‘phone’, or custom JWT authentication with ‘jwt’.
This popup acts as the primary mechanism for facilitating account creation.
SignMessage
ParticleSystem.Instance.SignMessage
enables the signing of a basic string.
This can be passed directly within SignMessage
for EVM chains, as shown below.
For Solana, you’ll need to convert this string to base58 before SignMessage
will function properly. The confirmation popup will display this string, base58 or otherwise, in UTF-8.
SignAndSendTransaction
The ParticleSystem.Instance.SignAndSendTransaction
method takes a transaction object constructed by ParticleSystem.Instance.MakeEvmTransaction
(for EVM chains). It then prompts the user for confirmation (signature).
Once the user confirms, the transaction will be sent.
SignTypedData
ParticleSystem.Instance.SignTypedData
facilitates the signature of structured data; by default, this uses eth_signTypedData_v4
.
For more information on signTypedData
, see the Web (JavaScript/TypeScript) page.
SignTransaction
ParticleSystem.Instance.SignTransaction
is a Solana-specific method for requesting a signature for a given transaction.
This will sign (but not send) a transaction.
SignAllTransactions
ParticleSystem.Instance.SignAllTransactions
is the plural of those mentioned above; this is also *Solana-specific
OpenWebWallet
ParticleSystem.Instance.OpenWebWallet
can open the web wallet.
Master reference
Below is a table containing every relevant method provided through ParticleSystem
alongside specific parameters and a short description for a direct, raw view.
For methods listed that needed to be covered in the above examples, live implementation often mimics the standard structure covered throughout this document.
Class | Methods | Parameters |
---|---|---|
ParticleSystem | Init | config, theme, language, chainName, chainId |
ParticleSystem | Login | preferredAuthType, account |
ParticleSystem | SignMessage | message |
ParticleSystem | SignAndSendTransaction | transaction |
ParticleSystem | SignTypedData | message, version |
ParticleSystem | SignTransaction | transaction |
ParticleSystem | SignAllTransactions | transactions |
ParticleSystem | MakeEvmTransaction | from, to, data, value |
ParticleSystem | UpdateChainId | chainId |
ParticleSystem | UpdateChainName | chainName |
Was this page helpful?