> For the complete documentation index, see [llms.txt](/llms.txt).

# Embedded Wallets SDK for Unity

## Overview[​](#overview "Direct link to Overview")

MetaMask Embedded Wallets SDK (formerly Web3Auth Plug and Play) provides a seamless authentication experience for Unity game applications with social logins, external wallets, and more. Our Unity SDK, written in C#, simplifies how you connect users to their preferred wallets and manage authentication state across all mobile platforms.

## Requirements[​](#requirements "Direct link to Requirements")

- Unity Editor 2019.4.9f1 or greater
- .Net Framework 4.x
- iOS Platform Target Version 14 and above
- Android Target SDK Version 24 and above
- Basic knowledge of C# and Unity Development

## Prerequisites[​](#prerequisites "Direct link to Prerequisites")

- Set up your project on the [Embedded Wallets dashboard](https://developer.metamask.io/)

tip

See the [dashboard setup](/embedded-wallets/dashboard/) guide to learn more.

## Installation[​](#installation "Direct link to Installation")

Install the Web3Auth Unity SDK using one of the following methods:

### Download Unity package[​](#download-unity-package "Direct link to Download Unity package")

Download the [.unitypackage](https://github.com/Web3Auth/web3auth-unity-sdk/releases/latest) from our latest release and import the package file into your existing Unity3D project.

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[​](#configure-web3auth-project "Direct link to Configure Web3Auth project")

- From the [Embedded Wallets dashboard](https://developer.metamask.io/), create or select an Web3Auth project:
- Add `{{SCHEMA}}://{YOUR_APP_PACKAGE_NAME}/auth` to **Allowlist URLs**.
- Copy the `Client ID` for usage later.

## Initialize Web3Auth[​](#initialize-web3auth "Direct link to Initialize Web3Auth")

### 1\. Create Web3Auth instance[​](#1-create-web3auth-instance "Direct link to 1. Create Web3Auth instance")

Attach a `Web3Auth.cs` script to your game object where you want to write your authentication code:

/Assets/Web3Auth.cs

```
using System;
using System.Linq;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Newtonsoft.Json;

public class Web3Auth : MonoBehaviour
{
  // Start is called before the first frame update
  void Start() {}
  public void login() {}
  private void onLogin(Web3AuthResponse response) {}
  public void logout() {}
  private void onLogout() {}
}

```

Within your script, import the `Web3Auth` component in your class:

```
Web3Auth web3Auth;

```

Create an instance within your `Start()` function by creating an instance of the component you just imported:

```
web3Auth = GetComponent<Web3Auth>();

```

### 2\. Configure Web3Auth options[​](#2-configure-web3auth-options "Direct link to 2. Configure Web3Auth options")

After instantiation, within your `Start()` function, set up the Web3Auth Options:

```
web3Auth.setOptions(new Web3AuthOptions(){
    clientId = "YOUR_WEB3AUTH_CLIENT_ID", // Pass your Web3Auth Client ID, ideally using an environment variable // Get your Client ID from MetaMask Developer Dashboard
    network = Web3Auth.Network.SAPPHIRE_MAINNET, // or Web3Auth.Network.SAPPHIRE_DEVNET
    redirectUrl = new Uri("torusapp://com.torus.Web3AuthUnity/auth"),
});

```

### 3\. Set up event handlers[​](#3-set-up-event-handlers "Direct link to 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_WEB3AUTH_CLIENT_ID", // Pass your Web3Auth Client ID, ideally using an environment variable
        network = Web3Auth.Network.SAPPHIRE_MAINNET,
        redirectUrl = new Uri("torusapp://com.torus.Web3AuthUnity/auth"),
    });

    // Set up event handlers
    web3Auth.onLogin += onLogin;
    web3Auth.onLogout += onLogout;
}

private void onLogin(Web3AuthResponse response)
{
    Debug.Log("Login successful!");
    Debug.Log("Private Key: " + response.privKey);
}

private void onLogout()
{
    Debug.Log("Logout successful!");
}

```

## Advanced configuration[​](#advanced-configuration "Direct link to Advanced configuration")

The Web3Auth Unity SDK offers a rich set of advanced configuration options:

- **[Custom authentication](/embedded-wallets/sdk/unity/advanced/custom-authentication/):** Define authentication methods.
- **[Whitelabeling and UI customization](/embedded-wallets/sdk/unity/advanced/whitelabel/):** Personalize the modal's appearance.
- **[Multi-Factor Authentication (MFA)](/embedded-wallets/sdk/unity/advanced/mfa/):** Set up and manage MFA.
- **[Dapp share](/embedded-wallets/sdk/unity/advanced/dapp-share/):** Share dapp sessions across devices.

tip

See the [advanced configuration sections](/embedded-wallets/sdk/unity/advanced/) to learn more about each configuration option.

- Basic Configuration
- Advanced Configuration

```
web3Auth = GetComponent<Web3Auth>();

web3Auth.setOptions(new Web3AuthOptions(){
    clientId = "YOUR_WEB3AUTH_CLIENT_ID", // Pass your Web3Auth Client ID, ideally using an environment variable
    network = Web3Auth.Network.SAPPHIRE_MAINNET, // or Web3Auth.Network.SAPPHIRE_DEVNET
    redirectUrl = new Uri("torusapp://com.torus.Web3AuthUnity/auth"),
});

```

```
web3Auth = GetComponent<Web3Auth>();

var loginConfigItem = new LoginConfigItem()
    {
        verifier = "google-verifier", // Get this from the MetaMask Developer Dashboard
        typeOfLogin = TypeOfLogin.GOOGLE,
        clientId = "YOUR_GOOGLE_CLIENT_ID" // Google's client ID
    };

web3Auth.setOptions(new Web3AuthOptions(){
    clientId = "YOUR_WEB3AUTH_CLIENT_ID", // Pass your Web3Auth Client ID, ideally using an environment variable
    network = Web3Auth.Network.SAPPHIRE_MAINNET, // or Web3Auth.Network.SAPPHIRE_DEVNET
    redirectUrl = new Uri("torusapp://com.torus.Web3AuthUnity/auth"),
    loginConfig = new Dictionary<string, LoginConfigItem>
    {
        {"google", loginConfigItem}
    }
    mfaSettings = new MfaSettings() {
        deviceShareFactor = new MfaSetting() {
            enable = true,
            priority = 1
        },
        backUpShareFactor = new MfaSetting() {
            enable = true,
            priority = 2
        },
        socialBackupFactor = new MfaSetting() {
            enable = true,
            priority = 3
        },
        passwordFactor = new MfaSetting() {
            enable = true,
            priority = 4
        }
    }
});

```

## Blockchain integration[​](#blockchain-integration "Direct link to Blockchain integration")

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

### Ethereum integration[​](#ethereum-integration "Direct link to Ethereum integration")

tip

We recommend you use the [Nethereum Library](https://docs.nethereum.com/) for making the blockchain calls. See [how to integrate Nethereum with Web3Auth](/embedded-wallets/connect-blockchain/evm/ethereum/unity/).

For Ethereum integration, you can get the private key and use it with Nethereum:

```
using Nethereum.Web3;
using Nethereum.Util;
using Nethereum.Signer;
using Nethereum.Hex.HexConvertors.Extensions;
using Nethereum.ABI.Encoders;
using Nethereum.Hex.HexTypes;
using Nethereum.Web3.Accounts;
using Nethereum.Web3.Accounts.Managed;

public class Web3AuthScript : MonoBehaviour
{
    Web3 web3;
    Web3Auth web3Auth;
    private string privateKey;
    private Account account;

    const string rpcURL = "" // EVM chain RPC URL

    void Start()
    {

      web3Auth = GetComponent<Web3Auth>();

      // Add Web3Auth Unity SDK Initialisation Code here

      web3Auth.onLogin += onLogin;
      web3Auth.onLogout += onLogout;
      web3 = new Web3(rpcURL);
    }

    private void onLogin(Web3AuthResponse response)
    {
        // get the private key from web3auth response
        privateKey = response.privKey;
        // generate the user account
        var account = new Account(privateKey);
        // get the user address
        address = account.Address;

        // create the web3 instance
        web3 = new Web3("rpcURL");

        // get the user balance
        var balance = web3.Eth.GetBalance.SendRequestAsync(address).Result.Value;
    }
// ...
}

```

### Solana integration[​](#solana-integration "Direct link to Solana integration")

For Solana integration, you can get the Ed25519 private key:

```
private void onLogin(Web3AuthResponse response)
{
    var ed25519PrivKey = response.ed25519PrivKey;
    // Use Ed25519 private key with Solana libraries
    Debug.Log("Solana Ed25519 Private Key: " + ed25519PrivKey);
}

```
