The Ankr Unity SDK provides an easy way to interact with Web3 and to work with contracts deployed on the blockchain. As a plugin it easily integrates with MetaMask on Android and iOS.
View Demo
Table of Contents
-
Download
mirageSDK.unitypackagefrom the latest release. -
In your project, locate the 'Assets' folder. Move
mirageSDK.unitypackageinto this folder. -
'Import all' to have access to all SDK capabilities.
The SDK is designed to make it super easy to get started with Game development by enabling connection and interaction across different blockchains.
-
Contains a huge range of classes, plugins and example scripts for a variety of use cases.
-
Nethereum libraries provide support for web requests using RPC over Http.
-
Ankr RPC network infrastructure provides fast and easy connection to multiple chains.
- Smart Contracts must already be deployed on the blockchain. You have the Smart Contract addresses and ABI
- Web3.js or Ethers.js
This help content focuses on the following use cases.
-
Connecting to a Web3 Wallet (MetaMask) and Authenticating a user
-
Performing Updates to NFTs by interacting with the blockchain and calling smart functions.
Connecting to an Ethereum i.e. MetaMask wallet provides a link between a wallet address and a user's game account.
- Create an instance of a
Web3class and callInitializemethod after successful login in metamask
string provider_url = "<ethereum node url>";
Web3 web3 = new Web3(provider_url);
web3.Initialize();- Login via MetaMask is required to authenticate the user.
- Call the
SignMethodto trigger Signing via MetaMask.
string message = "Hi I am a message !"
string signature = await web3.Sign(message);-
Continue by deploying the Unreal Engine (backend)
-
The backend returns a JSON object (payload) with the
session_idandURI. -
Web3.Sign(string)returns the signature.
- Verify the user account and address as follows:
POST `/account/verification/address`
The body contains a message and the signature returned from Web3.sign().
{
"message": "Hi I am a message !", // your message
"signature":"0x..." // result of Web3.Sign()
}It returns the address of the user from their signature so it can be written to database as follows:
...
sigPublicKey := getAddrFromSign(input.Signature, data)
address := string(sigPublicKey);
// add address to a database
...Inside MirageSDK/Examples/UseCases/LinkingAccountWalletis an example script demonstrating how to link a crypto wallet (MetaMask) to a player account.
Making updates to the NFT e.g. adding a red hat to a character requires signing and sending a transaction.
All updates are transactions that must be signed via a prompt from MetaMask.
There are two ways to make update transactions. Using the GetData method and the CallMethod
Use the GetData method to retrieve information from the blockchain. (READ functions do NOT require mining.). Other non-standard Get functions are also available
These methods require
- Contract Address
- ABI
The following extract is an example usage of the GetData method to retrive information about an NFT:
private async UniTask<BigInteger> GetHat(BigInteger tokenID)
{
var getHatMessage = new GetHatMessage
{
CharacterId = tokenID.ToString()
};
var hatId = await _gameCharacterContract.GetData<GetHatMessage, BigInteger>(getHatMessage);
UpdateUILogs($"Hat Id: {hatId}");
return hatId;
}Use the CallMethod to write new information to the blockchain. These methods utilize gas.
The following extract is an example of how a CallMethod is used to update an NFT:
public async void UpdateNFT()
{
// 1) Request nft parameters and signature for parameters
var info = await RequestPreparedParams(0);
// 2) Call method that check signature and update nft
var receipt = await _contract.CallMethod("updateTokenWithSignedMessage", new object[] { info });
Debug.Log($"Receipt: {receipt}");
}We have two ERC proposals.
ERC-4884 Rentable NFT Standard
ERC-4911 Composability Extension For ERC-721 Standard
For full examples:
View ERC20 token example and ERC721 token example