Unity (C#) - Connect
Leveraging Particle Connect within mobile applications built on Unity
Particle Connect for Unity
Particle Connect acts as a central wallet connection mechanism within games built on Unity.
With it, you can onboard users into a functional account through social logins (via Particle Auth) or typical Web3 wallets (via WalletConnect, Solana’s wallet-adapter
, or private key imports). This creates a Web2-adjacent login experience while retaining unified compatibility with existing popular Web3 wallets.
Particle Connect has a dedicated Unity SDK for integration; the configuration process and examples of utilization are outlined below.
Repository
Before diving in, all of the Particle Network Unity SDKs (for Particle Auth and Particle Connect) are publicly available and viewable through the official repository. Feel free to look at the underlying architecture and demos showcasing end-to-end implementation to gain an initial understanding of working with Particle Connect on Unity.
Getting Started
Prerequisites
Your Unity project must meet several requirements to avoid compatibility issues. These requirements will be, in some capacity, dependent on the platform that you’ve decided to use. They are as follows:
- Unity 2022.3.43f1 or higher.
- If you’re using iOS:
- Xcode 15.0 or higher.
- CocoaPods 1.14.3 or higher.
- iOS 14 or higher.
- If you’re using Android:
- minSdkVersion 23 or higher.
- compileSdkVersion, targetSdkVersion 34 or higher.
- JavaVersion 17.
- Jetpack (AndroidX).
- Android Gradle Plugin Version : 8.5.1 or higher.
- Gradle Version : 8.9 or higher.
Setting up the Particle dashboard
Once you’ve ensured that your project meets the above prerequisites, you’ll also need three key values from the Particle dashboard: your projectId
, clientKey
, and appId
.
These connect your Unity project directly with the Particle dashboard, enabling customization, analytics, tracking, etc.
Follow the quickstart tutorial to set up a project and find the required keys: Create a new project.
Configuration
With these values in hand, you can move on to initial configuration and dependency management, which will vary in complexity depending on the platform used. To begin, for both iOS and Android, you’ll need to download and install the Particle Unity package, which will include the necessary files for both Particle Connect and Particle Auth.
Head over to the GitHub repository, and download the latest release (.unitypackage
), then import this into your project.
iOS configuration
If you’re building a Unity game on iOS, you must follow a specific configuration process to ensure that Particle Connect functions. The first step within this process is to set up a scheme URL within the Unity editor.
- Head into the iOS Player Settings menu (
Edit
->Project Settings
->Player Settings
->iOS
). - From here, select
Other
, then scroll down toConfiguration
. - Open the
Supported URL schemes
section, and within theElement 0
field, paste in yourprojectId
with a prefix ofpn
.- For example, if your
projectId
(from the Particle dashboard) is something like63bfa427-cf5f-4742-9ff1-e8f5a1b9828f
, then the scheme URL, in this case, would bepn63bfa427-cf5f-4742-9ff1-e8f5a1b9828f
- For example, if your
Remove other services, if needed
Within ParticleNetworkIOSBridge.cs
, you’ll have a number of services
included in the SDK:
- ParticleNetworkBase
- required universally.
- ParticleAuthCore
- required for Particle Auth Core.
- ParticleConnect
- required for Particle Connect.
ParticleWalletGUI
- usage of the Particle Wallet UI, contains all services.
- ParticleAA
- usage of the Particle AA, contains all services.
You’ll also need to configure your Podfile if you haven’t already. To do so, go to the root of your project directory and run pod init
; this will generate a Podfile.
Open this Podfile, and insert the specific pods (services) you’d like to use within your project. In this case, ParticleConnect
and CommonConnect
will generally suffice, but additional services can be added if needed.
Additionally, you’ll need to paste in the code snippet below for installation handling:
You can also copy the example Podfile.
With your Podfile set, you’ll need to run pod install
and open your .xcworkspace
file, as shown below:
Finally, for iOS, you’ll need to formally use the projectId
, clientKey
, and appId
previously retrieved. To do this, head to the root of your Xcode project and create a file called ParticleNetwork-Info.plist
. Within this file, paste the following text (then replace the placeholders):
Android configuration
If you’re building your Unity game for Android, you’ll just need to configure two files before you’re ready to go. The first of these two files can be found at Assets/Plugins/Android/launcherTemplate.gradle
within your project. Here, you’ll need to ensure you have the necessary dependencies included.
Specifically, you’ll need the following dependencies at a minimum:
network.particle:auth-service
if you’re planning on using Particle Auth directly.network.particle:connect
, required for Particle Connect.network.particle:unity-bridge
, required universally.- Additional dependencies, optional:
network.particle:chains
, collection ofchainInfo
objects.network.particle:connect-solana-adapter
, Solana-specific custom wallet adapter to be used within Particle Connect.network.particle:connect-phantom-adapter
, Wallet adapter for Phantom to be used within Particle Connect.network.particle:connect-evm-adapter
, EVM-specific custom wallet adapter to be used within Particle Connect.network.particle:connect-auth-adapter
, Particle Auth adapter for Particle Connect (social logins).network.particle:connect-wallet-connect-adapter
, WalletConnect adapter for Particle Connect. The full list ofnetwork.particle
packages can be found here.
Finally, for Android, you’ll need to place your projectId
, clientKey
, and appId
within gradleTemplate.properties
, which can be found at Assets/Plugins/Android/gradleTemplate.properties
.
Initialization
Now that you’ve managed the relevant dependencies and configured your Unity environment, you’ll need to initialize ParticleNetwork
and ParticleConnectInteraction
(derived from Network.Particle.Scripts.Core
) before moving on. This is required for Particle Connect to function.
ParticleNetwork
can be initialized by simply calling Init
and passing in a chainInfo
object (*). ParticleConnectInteraction.Init
will require both a chainInfo
object and a DAppMetaData
parameter (in this example, represented as metadata
) containing a WalletConnect project ID retrieved from the WalletConnect dashboard, alongside the name of your project, the icon (preferably 512x512), the website URL, and a project description.
Additionally, with WalletConnect as the underlying infrastructure for Web3 wallets within Particle Connect, you’ll also have the opportunity to include configuration for multiple different chains on WalletConnect by calling ParticleConnectInteraction.SetWalletConnectV2SupportChainInfos
and passing in an array of chainInfo
objects.
An example of the above process can be found below.
Field | Type | Description |
---|---|---|
chainInfo | ChainInfo | The primary chain to be used within your application. This should be indicated by an overall blockchain, followed by a specific network. Within this example, it’s .Ethereum (alternatively it could be .EthereumSepolia , and so on.) |
dAppData | DAppMetadata | Metadata that describes your application and should be provided as an instance of DAppMetadata with the following parameters: walletConnectProjectId , the WalletConnect project ID retrieved from the WalletConnect dashboard, name , the name of your project. icon , a URL linking to the icon of your project; this should be 512x512, ideally. url , the URL of your project’s website. description , the description of your project, redirect , optional, a redirect universal link to you project, verifyUrl , optional. |
rpcUrl | RpcUrl? | Customize the RPC URL. |
env | Env | The specific environmennt to be used. This can be either .DEV , .STAGING , .PRODUCTION , this should be set to .PRODUCTION when released. |
Additionally, if you’d like to define multiple chains to be available through WalletConnect, you’ll need to create an array of ChainInfo
objects (chainInfos
in this example) and use it within ParticleConnectInteraction.SetWalletConnectV2SupportChainInfos
. E.g.:
Examples of Utilization
Set Chain Info
If you want to set the primary chain after initializing with .Init
and before a user is connected, you can call ParticleNetwork.SetChainInfo
, passing in the appropriate chainInfo
object for the desired chain.
If your application supports only EVM chains, ParticleNetwork.SetChainInfo
is sufficient and can be called anytime. However, if you support both EVM and Solana chains, use ParticleAuthCore.Instance.SwitchChain
when switching between EVM and Solana chains. For example:
Connect Wallet
Within implementations of Particle Connect, Connect
drives connection with a wallet in the Particle Connect stack, (social login or otherwise.) This is done through ParticleConnect.Instance.Connect
, then passing in a relevant WalletType
object, derived from Network.Particle.Scripts.Model
WalletType
is an enum
containing the following choices:
AuthCore
, Particle Auth Core, an alternative to Particle Auth (Particle
).EvmPrivateKey
, custom EVM wallet imports/exports.SolanaPrivateKey
, custom Solana wallet imports/exports.MetaMask
.Rainbow
.Trust
.ImToken
.Bitget
.WalletConnect
.Zerion
.Math
.Inch1
.Zengo
.Alpha
.OKX
.TokenPocket
, not supported by iOS.Phantom
, intended for Solana.
If you’re using AuthCore
, you’ll also need to pass in a ConnectConfig
object containing:
Field | Type | Description |
---|---|---|
loginType | LoginType | The specific social login to be used. This can be either .EMAIL , .PHONE , .GOOGLE , .FACEBOOK , .APPLE , .TWITTER , .DISCORD , .GITHUB , .TWITCH , .MICROSOFT , .LINKEDIN or JWT . |
account | string? | (Optional) when type is set to either .email , .phone , or .jwt , you can use the account parameter to pass in an expected email, phone number, or JWT. This is optional for the former two, but required for .jwt . The phone number must be in E.164 format. |
code | string? | (Optional) when type is set to either .email pr .phone , you can use the code parameter to pass the verification code. |
supportAuthTypes | List<SupportLoginType>? | The methods of authentication visible on the authentication popup UI. By default, this will be exclusive to the chosen social login method, although by passing in additional types, you can expand the UI to include the ability to login with those as an alternative to type. |
socialLoginPrompt | SocialLoginPrompt? | (Optional) either .None , .Consent , or .SelectAccount . |
loginPageConfig | LoginPageConfig? | (Optional) customize the UI page, includes project name, icon and description. |
E.g.:
Disconnect Wallet
To disconnect an active session identified by a specific address, you can call ParticleConnect.Instance.Disconnect
, passing in the particular wallet type (WalletType
object) and associated address that you’d like to disconnect from your application, as is shown below.
Is Connected
Another critical method is IsConnected
, returning a Boolean indicating whether or not a user (or, in this case, an address and WalletType
) is currently connected to your application. This can be called through ParticleConnectInteraction.IsConnected
. E.g.:
Sign and Send Transaction
ParticleConnect.Instance.SignAndSendTransaction
is the primary mechanism for EVM and Solana to send (or, in this case, suggest) transactions. Upon calling, the WalletType
(the adapter in question) will return a confirmation popup requesting a signature from the user before sending the transaction directly to the network. SignAndSendTransaction
takes three parameters:
WalletType
is the type of wallet being used.address
is the public address being used.transaction,
a stringified representation of a transaction object.
E.g.:
Sign Transaction
ParticleConnect.Instance.SignTransaction
is a Solana-specific method for signing a transaction without pushing it to the network. This will behave similarly to SignAndSendTransaction
, although it will only function on either the Phantom
or SolanaPrivateKey
wallet types and will purely return a signature.
The plural of this method is ParticleConnect.Instance.SignAllTransactions
, which takes a list of transactions rather than just one to be prompted for signature.
Sign Message
A simple message (UTF-8 string) can be signed on both EVM & Solana through ParticleConnect.Instance.SignMessage
, passing in the WalletType
, address
(publicAddress
in this example), and the message
in question. message
should be a general string to be signed. E.g.:
Sign Typed Data
As an alternative to SignMessage
, for EVM chains, you can request that a user signs typed (structured) data rather than purely a raw string. To do this, you can use adapter.signTypedData
(equivalent to eth_signTypedData
, represented as either TypedDataV4
, TypedDataV2
, or TypedDataV1
). This takes the following parameters:
address
is the user address to target for signature initiation.data
, the typed data to be signed; see the Web (JavaScript/TypeScript) page for additional guidance.
E.g.:
Import Wallet
If you’re using theEvmPrivateKey
or SolanaPrivateKey
wallet types, you can import wallets through either a seed phrase or private key. These methods will associate an account instance derived from these keys, allowing utilization within your application.
These can be achieved through ImportWalletFromPrivateKey
for importing a private key, or ImportWalletFromMnemonic
for importing a mnemonic (seed phrase). These methods require the WalletType
(either EvmPrivateKey
or SolanaPrivateKey
) and the private key/seed phrase to be imported.
Additionally, you can export one of these wallets with ParticleConnect.Instance.ExportWalletPrivateKey
, passing in the address (of the EvmPrivateKey
or SolanaPrivateKey
imported/generated wallet) that you’d like to export.
Dive Deeper
ParticleConnect
includes ParticleAuthCore
. When logged in with a Particle account, you can access additional functions such as getUserInfo
, openAccountAndSecurity
, hasMasterPassword
, hasPaymentPassword
, and changeMasterPassword
. For more information, refer to the Auth SDK reference.
Was this page helpful?