Embedded Wallets SDK for Unity
Overview
The MetaMask Embedded Wallet SDK for Unity provides social and custom authentication for games and other Unity applications. The SDK supports Android, iOS, WebGL, macOS, and Windows.
Requirements
- Unity Editor 2019.4.9f1 or greater
- .Net Framework 4.x
- Basic knowledge of C# and Unity development
Prerequisites
- Set up your project on the Embedded Wallets dashboard
See the dashboard setup guide to learn more.
Installation
Install the MetaMask Embedded Wallet SDK for Unity using the following method.
Download Unity package
Download the
latest .unitypackage and import
it into your Unity project.
If you're upgrading from v7, follow the Unity SDK v8 migration guide.
You may encounter errors when importing this package into your existing project.
The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?)
To fix this problem you need to add the following line into the dependencies object which is inside the Packages/manifest.json file.
"com.unity.nuget.newtonsoft-json": "3.2.1"
Configure Web3Auth project
- Create or select an Embedded Wallets project in the MetaMask Developer dashboard.
- Add
<YOUR_SCHEME>://<YOUR_APP_PACKAGE_NAME>/authto Allowlist URLs. - Copy your client ID.
- In Unity, select Window > Web3Auth > Generate Deep Link, enter the same redirect URL, and select Generate.
Initialize Web3Auth
1. Add the Web3Auth component
Add the SDK's Web3Auth component to a game object.
Create a separate script for your authentication logic and attach it to the same game object:
using System;
using System.Collections.Generic;
using UnityEngine;
public class AuthManager : MonoBehaviour
{
private Web3Auth web3Auth;
void Start() {}
public void login() {}
private void onLogin(Web3AuthResponse response) {}
public void logout() {}
private void onLogout() {}
}
Declare a field for the SDK component:
private Web3Auth web3Auth;
Get the component in Start():
web3Auth = GetComponent<Web3Auth>();
2. Configure Web3Auth options
After instantiation, within your Start() function, set up the Web3Auth Options:
web3Auth.setOptions(new Web3AuthOptions(){
clientId = "<YOUR_CLIENT_ID>",
web3AuthNetwork = Web3Auth.Network.SAPPHIRE_MAINNET,
redirectUrl = new Uri("<YOUR_SCHEME>://<YOUR_APP_PACKAGE_NAME>/auth"),
});
3. Set up event handlers
Set up event handlers for login and logout events:
void Start()
{
web3Auth = GetComponent<Web3Auth>();
web3Auth.setOptions(new Web3AuthOptions()
{
clientId = "<YOUR_CLIENT_ID>",
web3AuthNetwork = Web3Auth.Network.SAPPHIRE_MAINNET,
redirectUrl = new Uri("<YOUR_SCHEME>://<YOUR_APP_PACKAGE_NAME>/auth"),
});
// Set up event handlers
web3Auth.onLogin += onLogin;
web3Auth.onLogout += onLogout;
}
private void onLogin(Web3AuthResponse response)
{
Debug.Log("Login successful!");
var privateKey = response.privateKey;
}
private void onLogout()
{
Debug.Log("Logout successful!");
}
Advanced configuration
The Web3Auth Unity SDK offers a rich set of advanced configuration options:
- Custom authentication: Define authentication methods.
- Whitelabeling and UI customization: Personalize the modal's appearance.
- Multi-Factor Authentication (MFA): Set up and manage MFA.
- Dapp share: Share dapp sessions across devices.
See the advanced configuration sections to learn more about each configuration option.
- Basic Configuration
- Advanced Configuration
web3Auth = GetComponent<Web3Auth>();
web3Auth.setOptions(new Web3AuthOptions(){
clientId = "<YOUR_CLIENT_ID>",
web3AuthNetwork = Web3Auth.Network.SAPPHIRE_MAINNET,
redirectUrl = new Uri("<YOUR_SCHEME>://<YOUR_APP_PACKAGE_NAME>/auth"),
});
web3Auth = GetComponent<Web3Auth>();
var authConnection = new AuthConnectionConfig()
{
authConnectionId = "<YOUR_AUTH_CONNECTION_ID>",
authConnection = AuthConnection.GOOGLE,
clientId = "<YOUR_GOOGLE_CLIENT_ID>"
};
web3Auth.setOptions(new Web3AuthOptions(){
clientId = "<YOUR_CLIENT_ID>",
web3AuthNetwork = Web3Auth.Network.SAPPHIRE_MAINNET,
redirectUrl = new Uri("<YOUR_SCHEME>://<YOUR_APP_PACKAGE_NAME>/auth"),
authConnectionConfig = new List<AuthConnectionConfig>
{
authConnection
},
mfaSettings = new MfaSettings(
new MfaSetting(true, 1, true),
new MfaSetting(true, 2, true),
new MfaSetting(true, 3, false),
new MfaSetting(true, 4, false),
null,
null
)
});
Blockchain integration
Web3Auth is blockchain agnostic, enabling integration with any blockchain network. We recommend using the Nethereum Library for Ethereum integration.
Ethereum integration
We recommend you use the Nethereum Library for making the blockchain calls. See how to integrate Nethereum with Web3Auth.
For Ethereum integration, you can get the private key and use it with Nethereum:
using Nethereum.Web3;
using Nethereum.Web3.Accounts;
public class Web3AuthScript : MonoBehaviour
{
private Web3 web3;
private Web3Auth web3Auth;
private string privateKey;
private Account account;
private const string rpcUrl = "<YOUR_RPC_URL>";
void Start()
{
web3Auth = GetComponent<Web3Auth>();
// Add the Embedded Wallet SDK initialization code here.
web3Auth.onLogin += onLogin;
}
private async void onLogin(Web3AuthResponse response)
{
privateKey = response.privateKey;
account = new Account(privateKey);
web3 = new Web3(account, rpcUrl);
var balance = await web3.Eth.GetBalance.SendRequestAsync(account.Address);
}
}
Solana integration
For Solana integration, you can get the Ed25519 private key:
private void onLogin(Web3AuthResponse response)
{
var ed25519PrivateKey = response.ed25519PrivateKey;
// Use Ed25519 private key with Solana libraries
}