Skip to content

Documentation

SDK: https://github.com/ChainSafe/web3.unity

Bridging Unity games to the blockchain. Create your in game NFTs.

Tutorial

Working example on IPFS: QmXKxNmUDYZkFcEv9cpZ18ZnAVuy8uvz4yFz71TXjt7QQ7

Demo

Watch the video

Installing SDK

Watch the video

SDK Overview

Watch the video

Create In Game NFT

Watch the video

Minting NFT

Watch the video

Connect NFT to Marketplace

Watch the video

Building Game

Watch the video

Final Project

Watch the video

Player Account

PlayerPrefs.GetString("Account") is the user's wallet account accessed after the LoginScene.

string account = PlayerPrefs.GetString("Account");
print(account);

Supported Blockchains

The SDK will require variables chain and network.

chain network
ethereum mainnet ropsten kovan rinkeby goerli
avalanche mainnet testnet
binance mainnet testnet
moonbeam mainnet testnet
polygon mainnet testnet
xdai mainnet testnet

EVM Blockchain

Block number

Get the current latest block number

string chain = "ethereum";
string network = "mainnet"; // mainnet ropsten kovan rinkeby goerli

int blockNumber = await EVM.BlockNumber(chain, network);
print(blockNumber);

Balance Of

Get the balance of the native blockchain

string chain = "ethereum";
string network = "rinkeby"; // mainnet ropsten kovan rinkeby goerli
string account = "0xdD4c825203f97984e7867F11eeCc813A036089D1";

string balance = await EVM.BalanceOf(chain, network, account);
print(balance);

Verify

Verify a signed message.

string message = "YOUR_MESSAGE";
string signature = "0x94bdbebbd0180195b89721a55c3a436a194358c9b3c4eafd22484085563ff55e49a4552904266a5b56662b280757f6aad3b2ab91509daceef4e5b3016afd34781b";

string address = await EVM.Verify(message, signature);
print(address);

Is Transaction Confirmed

string chain = "ethereum";
string network = "mainnet";
string transaction = "0x911d4ec9193e0dc14d9d034d88c311453112c5097f29c366ccc9c5e5bc7072e1";

bool txConfirmed = await EVM.IsTxConfirmed(chain, network, transaction);
print(txConfirmed);

Nonce

string chain = "ethereum";
string network = "rinkeby";
string account = "0xdD4c825203f97984e7867F11eeCc813A036089D1";

string nonce = await EVM.Nonce(chain, network, account);
print(nonce);

Gas Price

string chain = "polygon";
string network = "mainnet";

string gasPrice = await EVM.GasPrice(chain, network);
print(gasPrice);

Gas Limit

string chain = "polygon";
string network = "mainnet";
string account = "0xdD4c825203f97984e7867F11eeCc813A036089D1"; // account to send to
string amount = "123"; // amount of wei to send
string transaction = "0x"; // from EVM.CreateTransaction()

string gasPrice = await EVM.GasLimit(chain, network, account, amount, transaction);
print(gasPrice);

ERC1155

A standard interface for contracts that manage multiple token types. A single deployed contract may include any combination of fungible tokens, non-fungible tokens or other configurations (e.g. semi-fungible tokens).

Balance Of

string chain = "avalanche";
string network = "testnet";
string contract = "0xbDF2d708c6E4705824dC024187cd219da41C8c81";
string account = "0xdD4c825203f97984e7867F11eeCc813A036089D1";
string tokenId = "2";

BigInteger balanceOf = await ERC1155.BalanceOf(chain, network, contract, account, tokenId);
print(balanceOf);

Balance Of Batch

Balance of batch will get the balance of a list of accounts and token ids. For example:

Get the balance of account 0xaCA9B6D9B1636D99156bB12825c75De1E5a58870 with token id 17 and

balance of account 0xaCA9B6D9B1636D99156bB12825c75De1E5a58870 with token id 22

string chain = "ethereum";
string network = "rinkeby";
string contract = "0x2ebecabbbe8a8c629b99ab23ed154d74cd5d4342";
string[] accounts = { "0xaCA9B6D9B1636D99156bB12825c75De1E5a58870", "0xaCA9B6D9B1636D99156bB12825c75De1E5a58870" };
string[] tokenIds = { "17", "22" };

List<BigInteger> batchBalances = await ERC1155.BalanceOfBatch(chain, network, contract, accounts, tokenIds);
foreach (var balance in batchBalances)
{
    print ("BalanceOfBatch: " + balance);
} 

URI

Returns meta data about the token.

string chain = "binance";
string network = "mainnet";
string contract = "0x3E31F70912c00AEa971A8b2045bd568D738C31Dc";
string tokenId = "770";

string uri = await ERC1155.URI(chain, network, contract, tokenId);
print(uri);

ERC721

A standard interface for non-fungible tokens, also known as deeds.

Balance Of

Counts all NFTs assigned to an owner

string chain = "ethereum";
string network = "mainnet";
string contract = "0x60f80121c31a0d46b5279700f9df786054aa5ee5";
string account = "0x6b2be2106a7e883f282e2ea8e203f516ec5b77f7";

int balance = await ERC721.BalanceOf(chain, network, contract, account);
print(balance);

Owner Of

Find the owner of a NFT

string chain = "moonbeam";
string network = "testnet";
string contract = "0xcB0cbcE06860f6C30C62560f5eFBF918150e056E";
string tokenId = "1";

string ownerOf = await ERC721.OwnerOf(chain, network, contract, tokenId);
print(ownerOf);

Owner Of Batch

string chain = "ethereum";
string network = "mainnet";
string contract = "0xA74E199990FF572A320508547Ab7f44EA51e6F28";
string[] tokenIds = {"700", "791"};

List<string> batchOwners = await ERC721.OwnerOfBatch(chain, network, contract, tokenIds);
foreach (string owner in batchOwners)
{
  print ("OwnerOfBatch: " + owner);
}

URI

string chain = "polygon";
string network = "mainnet";
string contract = "0xbCCaa7ACb552A2c7eb27C7eb77c2CC99580735b9";
string tokenId = "965";

string uri = await ERC721.URI(chain, network, contract, tokenId);
print(uri)

ERC20

A standard interface for tokens.

Balance Of

string chain = "xdai";
string network = "mainnet";
string contract = "0xa106739de31fa7a9df4a93c9bea3e1bade0924e2";
string account = "0x000000ea89990a17Ec07a35Ac2BBb02214C50152";

BigInteger balanceOf = await ERC20.BalanceOf(chain, network, contract, account);
print(balanceOf);

Name

Returns the name of the token. E.g. "Wrapped Ether"

string chain = "xdai";
string network = "mainnet";
string contract = "0xa106739de31fa7a9df4a93c9bea3e1bade0924e2";

string name = await ERC20.Name(chain, network, contract);
print(name);

Symbol

Returns the symbol of the token. E.g. “WETH”.

string chain = "ethereum";
string network = "mainnet";
string contract = "0xdac17f958d2ee523a2206206994597c13d831ec7";

string symbol = await ERC20.Symbol(chain, network, contract);
print(symbol);

Decimals

Returns the number of decimals the token uses - e.g. 8, means to divide the token amount by 100000000 to get its user representation.

string chain = "xdai";
string network = "mainnet";
string contract = "0xa106739de31fa7a9df4a93c9bea3e1bade0924e2";

BigInteger decimals = await ERC20.Decimals(chain, network, contract);
print(decimals);

Total Supply

Returns the total token supply.

string chain = "ethereum";
string network = "mainnet";
string contract = "0x6b3595068778dd592e39a122f4f5a5cf09c90fe2";

BigInteger totalSupply = await ERC20.TotalSupply(chain, network, contract);
print(totalSupply);

Custom Interactions

Call Custom Blockchains

Connect to any EVM compatible blockchain by providing an RPC. All methods have an optional field to add an RPC URL.

string chain = "rootstock";
string network = "testnet"; 
string account = "0xdD4c825203f97984e7867F11eeCc813A036089D1";
string rpc = "https://public-node.testnet.rsk.co";

string balance = await EVM.BalanceOf(chain, network, account, rpc);
print(balance);

Call Custom Contracts

Call will execute a smart contract method without altering the smart contract state.

// SPDX-License-Identifier: MIT

pragma solidity >=0.7.0 <0.9.0;

contract Increment {
    uint public x = 0;

    function increment() public {
        x++;
    }
}
// set chain - ethereum, moonbeam, polygon etc
string chain = "ethereum";
// set network - mainnet, testnet
string network = "rinkeby";
// smart contract method to call
string method = "x";
// abi in json format
string abi = "[ { \"inputs\": [], \"name\": \"increment\", \"outputs\": [], \"stateMutability\": \"nonpayable\", \"type\": \"function\" }, { \"inputs\": [], \"name\": \"x\", \"outputs\": [ { \"internalType\": \"uint256\", \"name\": \"\", \"type\": \"uint256\" } ], \"stateMutability\": \"view\", \"type\": \"function\" } ]";
// address of contract || token address
string contract = "0xB6B8bB1e16A6F73f7078108538979336B9B7341C";
// array of arguments for contract: "[\"1\", \"hello\"]"
string args = "[]";

// connects to user's browser wallet to call a transaction
string response = await EVM.Call(chain, network, contract, abi, method, args);
// display response in game
print(response);

WebGL

Send through WebGL

Send will execute a smart contract method, altering the smart contract state.

Working example on IPFS: QmUPMGp3zFXrrb7y3bfyJ6EbNJZWHeXHb251a1cAiNKuyi

// smart contract method to call
string method = "increment";
// abi in json format
string abi = "[ { \"inputs\": [], \"name\": \"increment\", \"outputs\": [], \"stateMutability\": \"nonpayable\", \"type\": \"function\" }, { \"inputs\": [], \"name\": \"x\", \"outputs\": [ { \"internalType\": \"uint256\", \"name\": \"\", \"type\": \"uint256\" } ], \"stateMutability\": \"view\", \"type\": \"function\" } ]";
// address of contract || token address || token address
string contract = "0xB6B8bB1e16A6F73f7078108538979336B9B7341C";
// array of arguments for contract: "[\"1\", \"hello\"]"
string args = "[]";
// value in wei
string value = "0";
// gas limit OPTIONAL
string gas = "21000";
// connects to user's browser wallet (metamask) to send a transaction
try {
  string response = await Web3GL.Send(method, abi, contract, args, value, gas);
  // 0x13c25ca8ab90eba92ab71ffb274f09b91cd838dfa2b4c78542fa6f5645355454
  Debug.Log(response); 
} catch (Exception e) {
  Debug.LogException(e, this);
};

Sign through WebGL

try {
  string message = "hello";
  string response = await Web3GL.Sign(message);
  Debug.Log(response);
} catch (Exception e) {
  Debug.LogException(e, this);
}

Get Network through WebGL

Get the user's current network id. For example, 0 is no network, 1 is Ethereum mainnet and 56 is BSC mainnet.

Working example on IPFS: QmfUYqAqFxHJFTx1WN5oUmEbLpscJRJ3XYrNmAFMENMMd5

// list of networks here: https://chainlist.org/
int network = Web3GL.Network();
print("User's current network: " + network);

Importing NFTs

NFT Textures