Skip to main content

Embedded Wallets SDK for Unity

Overview

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

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

Installation

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

Download Unity Package

Download the .unitypackage 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"

Setup

info

Prerequisites Before you start, make sure you have registered on the Web3Auth Dashboard and have set up your project. You can look into the Dashboard Setup guide to learn more.

Configure Web3Auth Project

  • Go to Developer Dashboard, create or select an Web3Auth project:
  • Add {{SCHEMA}}://{YOUR_APP_PACKAGE_NAME}/auth to Whitelist URLs.
  • Copy the Client ID for usage later.

Initialize Web3Auth

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

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

web3Auth.setOptions(new Web3AuthOptions(){
clientId = "YOUR_CLIENT_ID", // Get your Client ID from Web3Auth Dashboard
network = Web3Auth.Network.SAPPHIRE_MAINNET, // or Web3Auth.Network.SAPPHIRE_DEVNET
redirectUrl = new Uri("torusapp://com.torus.Web3AuthUnity/auth"),
});

3. Setup Event Handlers

Set up event handlers for login and logout events:

void Start()
{
web3Auth = GetComponent<Web3Auth>();
web3Auth.setOptions(new Web3AuthOptions()
{
clientId = "YOUR_CLIENT_ID",
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

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

tip

Head over to the advanced configuration sections to learn more about each configuration option.

web3Auth = GetComponent<Web3Auth>();

web3Auth.setOptions(new Web3AuthOptions(){
clientId = "YOUR_CLIENT_ID",
network = Web3Auth.Network.SAPPHIRE_MAINNET, // or Web3Auth.Network.SAPPHIRE_DEVNET
redirectUrl = new Uri("torusapp://com.torus.Web3AuthUnity/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. You can check our documentation on how to integrate Nethereum it with Web3Auth here.

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

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