Skip to content

Commit f2b3dd7

Browse files
committed
Scene_WebGL_MetaMask example
1 parent 9faa7be commit f2b3dd7

File tree

9 files changed

+2459
-13
lines changed

9 files changed

+2459
-13
lines changed

Assets/Thirdweb/Examples/Scenes/Scene_WebGL_MetaMask.unity

Lines changed: 2304 additions & 0 deletions
Large diffs are not rendered by default.

Assets/Thirdweb/Examples/Scenes/Scene_WebGL_MetaMask.unity.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Thirdweb/Examples/Scripts/PlaygroundAnimation.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ public class PlaygroundAnimation : MonoBehaviour
1010
private Coroutine _coroutine;
1111
private Dictionary<RectTransform, Vector3> originalPositions = new Dictionary<RectTransform, Vector3>();
1212

13-
public float animationDuration = 1f; // Duration of the animation
13+
public float animationDuration = 1f;
1414

1515
private void OnEnable()
1616
{
17-
// Start the animation coroutine
1817
if (_coroutine != null)
1918
{
2019
StopCoroutine(_coroutine);
@@ -25,7 +24,6 @@ private void OnEnable()
2524

2625
private void OnDisable()
2726
{
28-
// Stop the animation coroutine and reset positions
2927
if (_coroutine != null)
3028
{
3129
StopCoroutine(_coroutine);
@@ -35,11 +33,9 @@ private void OnDisable()
3533

3634
private IEnumerator Animate()
3735
{
38-
// Get the current object's transform
3936
var rectTransform = GetComponent<RectTransform>();
4037
var children = rectTransform.GetComponentsInChildren<RectTransform>();
4138

42-
// Store the original positions and set initial setup
4339
foreach (var child in children)
4440
{
4541
if (child.parent == transform)
@@ -73,7 +69,7 @@ private IEnumerator Animate()
7369
}
7470
else if (IsChildOfLayoutGroup(child) && child.TryGetComponent(out CanvasRenderer childCanvasRenderer))
7571
{
76-
StartCoroutine(FadeInOnly(child, childCanvasRenderer));
72+
StartCoroutine(FadeInOnly(childCanvasRenderer));
7773
}
7874
}
7975

@@ -96,12 +92,11 @@ private IEnumerator FadeInAndMoveUp(RectTransform child, CanvasRenderer canvasRe
9692
yield return null;
9793
}
9894

99-
// Ensure final state
10095
canvasRenderer.SetAlpha(1);
101-
child.anchoredPosition = targetPosition; // Set to original position
96+
child.anchoredPosition = targetPosition;
10297
}
10398

104-
private IEnumerator FadeInOnly(RectTransform child, CanvasRenderer canvasRenderer)
99+
private IEnumerator FadeInOnly(CanvasRenderer canvasRenderer)
105100
{
106101
float elapsedTime = 0f;
107102

@@ -114,7 +109,6 @@ private IEnumerator FadeInOnly(RectTransform child, CanvasRenderer canvasRendere
114109
yield return null;
115110
}
116111

117-
// Ensure final state
118112
canvasRenderer.SetAlpha(1);
119113
}
120114

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
using System.Threading.Tasks;
2+
using TMPro;
3+
using UnityEngine;
4+
using UnityEngine.UI;
5+
6+
namespace Thirdweb.Unity.Examples
7+
{
8+
public class WebGLMetaMaskExample : MonoBehaviour
9+
{
10+
[field: SerializeField, Header("Chain Settings")]
11+
private ulong ChainId = 421614;
12+
13+
[field: SerializeField, Header("UI Settings")]
14+
private TMP_Text LogText;
15+
16+
[field: SerializeField]
17+
private Button SignButton;
18+
19+
[field: SerializeField]
20+
private Button GetAddressButton;
21+
22+
[field: SerializeField]
23+
private Button GetBalanceButton;
24+
25+
private bool _connected;
26+
27+
private void Awake()
28+
{
29+
LogText.text = string.Empty;
30+
SignButton.onClick.AddListener(Sign);
31+
GetAddressButton.onClick.AddListener(GetAddress);
32+
GetBalanceButton.onClick.AddListener(GetBalance);
33+
}
34+
35+
private async void Start()
36+
{
37+
_connected = await TryConnect();
38+
}
39+
40+
private async Task<bool> TryConnect()
41+
{
42+
if (Application.platform != RuntimePlatform.WebGLPlayer)
43+
{
44+
Log("This example is only for WebGL builds. Use WalletConnect for native platforms.");
45+
return false;
46+
}
47+
48+
if (_connected)
49+
{
50+
return true;
51+
}
52+
53+
try
54+
{
55+
var options = new WalletOptions(provider: WalletProvider.MetaMaskWallet, chainId: ChainId);
56+
_ = await ThirdwebManager.Instance.ConnectWallet(options);
57+
Log($"Connected.");
58+
return true;
59+
}
60+
catch (System.Exception e)
61+
{
62+
Log($"Error connecting to wallet: {e.Message}");
63+
return false;
64+
}
65+
}
66+
67+
private async void Sign()
68+
{
69+
if (await TryConnect())
70+
{
71+
try
72+
{
73+
var wallet = ThirdwebManager.Instance.GetActiveWallet();
74+
var message = "Hello, World!";
75+
var signature = await wallet.PersonalSign(message);
76+
Log($"Signature: {signature}");
77+
}
78+
catch (System.Exception e)
79+
{
80+
Log($"Error signing message: {e.Message}");
81+
}
82+
}
83+
}
84+
85+
private async void GetAddress()
86+
{
87+
if (await TryConnect())
88+
{
89+
try
90+
{
91+
var wallet = ThirdwebManager.Instance.GetActiveWallet();
92+
var address = await wallet.GetAddress();
93+
Log($"Address: {address}");
94+
}
95+
catch (System.Exception e)
96+
{
97+
Log($"Error getting address: {e.Message}");
98+
}
99+
}
100+
}
101+
102+
private async void GetBalance()
103+
{
104+
if (await TryConnect())
105+
{
106+
try
107+
{
108+
var wallet = ThirdwebManager.Instance.GetActiveWallet();
109+
var balance = await wallet.GetBalance(chainId: ChainId);
110+
var balanceEth = Utils.ToEth(wei: balance.ToString(), decimalsToDisplay: 2, addCommas: true);
111+
var chainData = await Utils.FetchThirdwebChainDataAsync(client: ThirdwebManager.Instance.Client, chainId: ChainId);
112+
Log($"Balance: {balanceEth} {chainData.NativeCurrency.Symbol}");
113+
}
114+
catch (System.Exception e)
115+
{
116+
Log($"Error getting balance: {e.Message}");
117+
}
118+
}
119+
}
120+
121+
private void Log(string message)
122+
{
123+
LogText.text = message;
124+
ThirdwebDebug.Log(message);
125+
}
126+
}
127+
}

Assets/Thirdweb/Examples/Scripts/WebGLMetaMaskExample.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Thirdweb/Runtime/Unity/ThirdwebMainThreadExecutor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.Threading.Tasks;
44
using UnityEngine;
55

6-
namespace Thirdweb.Unity
6+
namespace Thirdweb.Unity.Helpers
77
{
88
public class ThirdwebMainThreadExecutor : MonoBehaviour
99
{

Assets/Thirdweb/Runtime/Unity/ThirdwebManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public void Initialize()
139139
Client = ThirdwebClient.Create(
140140
clientId: ClientId,
141141
bundleId: BundleId,
142-
httpClient: Application.platform == RuntimePlatform.WebGLPlayer ? new UnityThirdwebHttpClient() : new ThirdwebHttpClient(),
142+
httpClient: Application.platform == RuntimePlatform.WebGLPlayer ? new Helpers.UnityThirdwebHttpClient() : new ThirdwebHttpClient(),
143143
headers: new Dictionary<string, string>
144144
{
145145
{ "x-sdk-name", Application.platform == RuntimePlatform.WebGLPlayer ? "UnitySDK_WebGL" : "UnitySDK" },

Assets/Thirdweb/Runtime/Unity/ThirdwebUnityHttpClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using UnityEngine;
77
using UnityEngine.Networking;
88

9-
namespace Thirdweb.Unity
9+
namespace Thirdweb.Unity.Helpers
1010
{
1111
public class UnityThirdwebHttpClient : IThirdwebHttpClient
1212
{

ProjectSettings/EditorBuildSettings.asset

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@ EditorBuildSettings:
88
- enabled: 1
99
path: Assets/Thirdweb/Examples/Scenes/Scene_Playground.unity
1010
guid: 73a8efadf4c9a9b4cb1dd2dfdbe39c29
11+
- enabled: 0
12+
path: Assets/Thirdweb/Examples/Scenes/Scene_WebGL_MetaMask.unity
13+
guid: 6d172c4405a310a49956993b8b2ff904
1114
m_configObjects: {}

0 commit comments

Comments
 (0)