For AI agents: a documentation index is available at /llms.txt. A markdown version of this page is available at the same URL with .md appended (or via Accept: text/markdown).
Skip to main content

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

tip

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.

warning

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.

/Packages/manifest.json
"com.unity.nuget.newtonsoft-json": "3.2.1"

Configure Web3Auth project

  1. Create or select an Embedded Wallets project in the MetaMask Developer dashboard.
  2. Add <YOUR_SCHEME>://<YOUR_APP_PACKAGE_NAME>/auth to Allowlist URLs.
  3. Copy your client ID.
  4. 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:

/Assets/AuthManager.cs
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:

tip

See the advanced configuration sections to learn more about each configuration option.

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"),
});

Blockchain integration

Web3Auth is blockchain agnostic, enabling integration with any blockchain network. We recommend using the Nethereum Library for Ethereum integration.

Ethereum integration

tip

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
}