Skip to content

Documentation

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

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

Discussion

Discord: https://discord.gg/gG7dZrGJaa

Tutorial

Demo

Watch the video

Installing SDK

Watch the video

SDK Overview

Watch the video

SDK Prefabs

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

Transaction Status

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

string txConfirmed = await EVM.TxStatus(chain, network, transaction);
print(txConfirmed); // success, fail, pending

Nonce

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

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

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.

Working example: https://chainsafe.github.io/game-sendContract-example/

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract AddTotal {
    uint256 public myTotal = 0;

    function addTotal(uint8 _myArg) public {
        myTotal = myTotal + _myArg;
    }
}
// set chain: ethereum, moonbeam, polygon etc
string chain = "ethereum";
// set network mainnet, testnet
string network = "rinkeby";
// smart contract method to call
string method = "myTotal";
// abi in json format
string abi = "[ { \"inputs\": [ { \"internalType\": \"uint8\", \"name\": \"_myArg\", \"type\": \"uint8\" } ], \"name\": \"addTotal\", \"outputs\": [], \"stateMutability\": \"nonpayable\", \"type\": \"function\" }, { \"inputs\": [], \"name\": \"myTotal\", \"outputs\": [ { \"internalType\": \"uint256\", \"name\": \"\", \"type\": \"uint256\" } ], \"stateMutability\": \"view\", \"type\": \"function\" } ]";
// address of contract
string contract = "0x7286Cf0F6E80014ea75Dbc25F545A3be90F4904F";
// array of arguments for contract
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

Get User's Network

/*
1 Mainnet
3 Ropsten
4 Rinkeby
5 Goerli
42 Kovan
56 Binance Smart Chain Mainnet
97 Binance Smart Chain Testnet
100 xDai
137 Matic
1287 Moonbase Testnet
80001 Matic Testnet
43113 Avalanche Testnet
43114 Avalanche Mainnet
*/
int networkId = Web3GL.Network();

Send Transaction through WebGL

// account to send to
string to = "0x428066dd8A212104Bc9240dCe3cdeA3D3A0f7979";
// amount in wei to send
string value = "12300000000000000";
// gas limit OPTIONAL
string gasLimit = "";
// gas price OPTIONAL
string gasPrice = "";
// connects to user's browser wallet (metamask) to send a transaction
try {
    string response = await Web3GL.SendTransaction(to, value, gasLimit, gasPrice);
    Debug.Log(response);
} catch (Exception e) {
    Debug.LogException(e, this);
}

Send Contract through WebGL

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

Working example: https://chainsafe.github.io/game-sendContract-example/

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract AddTotal {
    uint256 public myTotal = 0;

    function addTotal(uint8 _myArg) public {
        myTotal = myTotal + _myArg;
    }
}
// smart contract method to call
string method = "addTotal";
// abi in json format
string abi = "[ { \"inputs\": [ { \"internalType\": \"uint8\", \"name\": \"_myArg\", \"type\": \"uint8\" } ], \"name\": \"addTotal\", \"outputs\": [], \"stateMutability\": \"nonpayable\", \"type\": \"function\" }, { \"inputs\": [], \"name\": \"myTotal\", \"outputs\": [ { \"internalType\": \"uint256\", \"name\": \"\", \"type\": \"uint256\" } ], \"stateMutability\": \"view\", \"type\": \"function\" } ]";
// address of contract
string contract = "0x7286Cf0F6E80014ea75Dbc25F545A3be90F4904F";
// array of arguments for contract
string args = "[\"1\"]";
// value in wei
string value = "0";
string gasLimit = "";
// gas price OPTIONAL
string gasPrice = "";
// connects to user's browser wallet (metamask) to update contract state
try {
  string response = await Web3GL.SendContract(method, abi, contract, args, value, gasLimit, gasPrice);
  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);
}

Importing NFTs

NFT Textures