![見出し画像](https://assets.st-note.com/production/uploads/images/82839059/rectangle_large_type_2_1c78ffd1a29de45015b4ae7d5856172b.png?width=800)
Nethereum 入門 (2) - Unityでの利用
UnityでNethereumを使う方法をまとめました。
・Unity 2021.3.4f1
・Nethereum 4.6.1
前回
1. Unityプロジェクトの準備
Unityプロジェクトの準備の手順は、次のとおりです。
(1) Unityのプロジェクトを作成。
(2) Nethereumのgithubリポジトリから「netStandardAOTMinimalUnity.zip」をダウンロード。
今回は、4.6.1を利用します。
・最小限のdllセット : netStandardAOTMinimalUnity.zip
・すべてのdllセット : net472dllsAOT.zip または netStandardAOT.zip
※ dllが重複した場合は消す必要あり。
![](https://assets.st-note.com/img/1658050853987-0g255K5P82.png?width=800)
2. はじめてのアプリの作成
はじめてのアプリの作成手順は、次のとおりです。
(1) Hierarchyウィンドウに空のゲームオブジェクトを追加し、「HelloWorld」と名前を指定。
(2) ゲームオブジェクト「HelloWorld」に新規スクリプト「HelloWorld」を追加し、以下のように編集。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Nethereum.JsonRpc.UnityClient;
using Nethereum.RPC.Eth.DTOs;
using Nethereum.Util;
public class HelloWorld : MonoBehaviour
{
void Start()
{
StartCoroutine(GetAccountBalance());
}
private IEnumerator GetAccountBalance()
{
// 残高リクエストの生成
// <自身のプロジェクトID>はInfuraで生成した自身のプロジェクトIDと置き換える
var balanceRequest = new EthGetBalanceUnityRequest("https://mainnet.infura.io/v3/<プロジェクトID>");
// イーサリアム財団のアカウントの残高の確認 (単位はwei)
yield return balanceRequest.SendRequest("0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae", BlockParameter.CreateLatest());
var weiAmount = balanceRequest.Result.Value;
print($"Balance in Wei: {weiAmount}");
// weiをetherに変換
var etherAmount = UnitConversion.Convert.FromWei(weiAmount);
print($"Balance in Ether: {etherAmount}");
}
}
前回と同様に、Infuraなどのパブリックノードを使用してEthereumメインネットに接続し、アカウントの残高を確認するコードになります。Infuraについては、前回の記事を参照。
(3) Playボタンを押して実行。
コンソールにアカウントの残高が表示されることを確認します。
![](https://assets.st-note.com/img/1658051359299-34MS2D3Y22.png?width=800)
3. 参考
次回
この記事が気に入ったらサポートをしてみませんか?