How to Integrate with the Ethereum Blockchain using Unity
How to Integrate with the Ethereum Blockchain using Unity
#Installation
To interact with the Ethereum blockchain, Magic Unity SDK integrates Nethereum as sub dependency.
#Initialization
The Magic class is the entry-point to the Magic SDK. It must be instantiated with a Magic publishable key.
01using link.magic.unity.sdk;
02using UnityEngine;
03
04public class MagicUnity : MonoBehaviour
05{
06 // Start is called before the first frame update
07 void Start()
08 {
09 Magic magic = new Magic("YOUR_PUBLISHABLE_KEY");
10 Magic.Instance = magic;
11 }
12}
#Use Different Networks
#Testnet
Goerli Block Explorer: https://goerli.etherscan.io
Goerli Testnet Faucet: https://goerlifaucet.com
01Magic magic = new Magic("YOUR_PUBLISHABLE_KEY", EthNetwork.Goerli);
#Custom Node
You can allow specific URLs to interact with the Magic SDK, such as a custom RPC URL to send transactions to your node. The Content Security Policy (CSP) of a browser dictates what resources can be loaded. If you're used a dedicated wallet, you can update the policy in the settings page of the dashboard with your custom URL. If you're using a universal wallet, please reach out to support to get your URL added.
Note: the use of a custom node will require the RPC URL to the project's Content Security Policy from your Magic dashboard. Refer to the CSP documentation.
01var config = new CustomNodeConfiguration(rpcUrl: "https://alchemy.io")
02Magic magic = new Magic("YOUR_PUBLISHABLE_KEY", config);
Do not set the custom nodes to local IP address (E.x. "http://127.0.0.1"\), because local IP will point to the network environment inside mobile device / simulator. Try accessible IP address in the same Wifi/Internet Environment (E.x. "http://10.0.0.93:3000"\)
#Associated Class
CustomNodeConfiguration(rpcUrl: String, chainId: Int?)
rpcUrl
:Your own node URLchainId
: Your own node's chainId
Magic.EthNetwork
01enum class EthNetwork {
02 Mainnet, Goerli
03 }
#Nethereum
There are two ways to set the wallet provider to Magic in Nethereum
1. Construct an ethApiService and pass Magic provider to it
01var ethApiService = new EthApiService(Magic.Instance.Provider);
02var accounts = await ethApiService.Accounts.SendRequestAsync();
03var balance = await ethApiService.GetBalance.SendRequestAsync(accounts[0]);
2. By directly passing Magic provider to each of the Nethereum RPC methods
01var ethAccounts = new EthAccounts(Magic.Instance.Provider);
02var accounts = await ethAccounts.SendRequestAsync();
#Common Methods
#Send Transaction
01public class MagicUnityButton : MonoBehaviour
02{
03 public async void SendTransaction()
04 {
05 // ⭐️ After user is successfully authenticated
06 var ethAccounts = new EthAccounts(Magic.Instance.Provider);
07 var accounts = await ethAccounts.SendRequestAsync();
08
09 // Construct a transaction
10 var transaction = new EthSendTransaction(Magic.Instance.Provider);
11 var transactionInput = new TransactionInput
12 { To = accounts[0], Value = new HexBigInteger(10), From = accounts[0]};
13
14 // Send the transaction
15 var hash = await transaction.SendRequestAsync(transactionInput);
16 Debug.Log(hash);
17 }
18}
#Sign Message
01public class MagicUnityButton : MonoBehaviour
02{
03 public async void EthSign()
04 {
05 // ⭐️ After user is successfully authenticated
06 var ethAccounts = new EthAccounts(Magic.Instance.Provider);
07 var accounts = await ethAccounts.SendRequestAsync();
08 var ethSign = new EthSign(Magic.Instance.Provider);
09 var signedResult = await ethSign.SendRequestAsync(accounts[0], "hello world");
10 Debug.Log(signedResult);
11 }
12}
#Get User Info
01public class MagicUnityButton : MonoBehaviour
02{
03 public async void Login()
04 {
05 // ⭐️ After user is successfully authenticated
06 var ethApiService = new EthApiService(Magic.Instance.Provider);
07 var accounts = await ethApiService.Accounts.SendRequestAsync();
08 Debug.Log($"token {token}");
09 }
10}