Skip to content

Commit 4614e32

Browse files
authored
Merge pull request #505 from reown-com/devin/1750856354-improve-unity-events-docs
Improve Unity AppKit events documentation
2 parents fadd8a0 + a571dcc commit 4614e32

File tree

1 file changed

+133
-6
lines changed

1 file changed

+133
-6
lines changed

appkit/unity/core/events.mdx

Lines changed: 133 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,155 @@
22
title: Events
33
---
44

5+
AppKit Unity provides a comprehensive event system that allows you to respond to various state changes in your application. All events are automatically dispatched on Unity's main thread, making them safe to use for UI updates and GameObject manipulation.
6+
57
## AppKit Events
68

79
```csharp
8-
//Invoked after successful initialization of AppKit
9-
AppKit.Initialized += (sender, eventArgs) => { };
10+
// Invoked after successful initialization of AppKit
11+
AppKit.Initialized += (sender, eventArgs) => {
12+
Debug.Log("AppKit initialized successfully");
13+
};
1014

1115
// Invoked after successful connection of an account
1216
AppKit.AccountConnected += (sender, eventArgs) => {
13-
Account activeAccount = eventArgs.GetAccount();
17+
Account activeAccount = eventArgs.Account;
18+
Debug.Log($"Account connected: {activeAccount.Address}");
19+
20+
// Access all connected accounts
21+
foreach (var account in eventArgs.Accounts)
22+
{
23+
Debug.Log($"Available account: {account.Address} on {account.ChainId}");
24+
}
1425
};
1526

1627
// Invoked after successful disconnection of an account
17-
AppKit.AccountDisconnected += (sender, eventArgs) => { };
28+
AppKit.AccountDisconnected += (sender, eventArgs) => {
29+
Debug.Log("Account disconnected");
30+
};
1831

1932
// Invoked after account has changed
20-
// This happens when the wallet updates a session or the user changes the active chain.
33+
// This happens when the wallet updates a session or the user changes the active account
2134
AppKit.AccountChanged += (sender, eventArgs) => {
2235
Account newAccount = eventArgs.Account;
36+
Debug.Log($"Account changed to: {newAccount.Address}");
2337
};
2438

2539
// Invoked after active chain has changed
2640
AppKit.ChainChanged += (sender, eventArgs) => {
27-
Chain newChain = eventArgs.Chain;
41+
Chain previousChain = eventArgs.PreviousChain;
42+
Chain newChain = eventArgs.NewChain;
43+
44+
Debug.Log($"Chain changed from {previousChain?.Name} to {newChain?.Name}");
45+
Debug.Log($"New chain ID: {newChain?.ChainId}");
2846
};
2947
```
48+
49+
## Event Arguments
50+
51+
### InitializeEventArgs
52+
Empty event arguments indicating successful initialization.
53+
54+
### AccountConnectedEventArgs
55+
- `Account Account` - The currently active connected account
56+
- `IEnumerable<Account> Accounts` - All connected accounts
57+
- `Func<Task<Account>> GetAccountAsync` - (Deprecated) Use Account property instead
58+
- `Func<Task<Account[]>> GetAccountsAsync` - (Deprecated) Use Accounts property instead
59+
60+
### AccountDisconnectedEventArgs
61+
Empty event arguments indicating account disconnection.
62+
63+
### AccountChangedEventArgs
64+
- `Account Account` - The new active account
65+
66+
### ChainChangedEventArgs
67+
- `Chain PreviousChain` - The previously active chain (can be null)
68+
- `Chain NewChain` - The newly active chain
69+
70+
## Common Use Cases
71+
72+
### Updating UI on Account Changes
73+
74+
```csharp
75+
public class AccountDisplay : MonoBehaviour
76+
{
77+
[SerializeField] private Text accountText;
78+
[SerializeField] private Text chainText;
79+
80+
private void OnEnable()
81+
{
82+
AppKit.AccountConnected += UpdateAccountDisplay;
83+
AppKit.AccountChanged += UpdateAccountDisplay;
84+
AppKit.ChainChanged += UpdateChainDisplay;
85+
AppKit.AccountDisconnected += ClearDisplay;
86+
}
87+
88+
private void OnDisable()
89+
{
90+
AppKit.AccountConnected -= UpdateAccountDisplay;
91+
AppKit.AccountChanged -= UpdateAccountDisplay;
92+
AppKit.ChainChanged -= UpdateChainDisplay;
93+
AppKit.AccountDisconnected -= ClearDisplay;
94+
}
95+
96+
private void UpdateAccountDisplay(object sender, Connector.AccountConnectedEventArgs e)
97+
{
98+
accountText.text = $"Account: {e.Account.Address}";
99+
}
100+
101+
private void UpdateChainDisplay(object sender, NetworkController.ChainChangedEventArgs e)
102+
{
103+
chainText.text = $"Chain: {e.NewChain?.Name ?? "Unknown"}";
104+
}
105+
106+
private void ClearDisplay(object sender, Connector.AccountDisconnectedEventArgs e)
107+
{
108+
accountText.text = "No account connected";
109+
chainText.text = "No chain selected";
110+
}
111+
}
112+
```
113+
114+
### Handling Connection State
115+
116+
```csharp
117+
public class ConnectionManager : MonoBehaviour
118+
{
119+
[SerializeField] private Button connectButton;
120+
[SerializeField] private Button disconnectButton;
121+
122+
private void OnEnable()
123+
{
124+
AppKit.AccountConnected += OnAccountConnected;
125+
AppKit.AccountDisconnected += OnAccountDisconnected;
126+
127+
// Set initial state
128+
UpdateButtonStates();
129+
}
130+
131+
private void OnDisable()
132+
{
133+
AppKit.AccountConnected -= OnAccountConnected;
134+
AppKit.AccountDisconnected -= OnAccountDisconnected;
135+
}
136+
137+
private void OnAccountConnected(object sender, Connector.AccountConnectedEventArgs e)
138+
{
139+
UpdateButtonStates();
140+
Debug.Log("Wallet connected successfully!");
141+
}
142+
143+
private void OnAccountDisconnected(object sender, Connector.AccountDisconnectedEventArgs e)
144+
{
145+
UpdateButtonStates();
146+
Debug.Log("Wallet disconnected");
147+
}
148+
149+
private void UpdateButtonStates()
150+
{
151+
bool isConnected = AppKit.IsAccountConnected;
152+
connectButton.gameObject.SetActive(!isConnected);
153+
disconnectButton.gameObject.SetActive(isConnected);
154+
}
155+
}
156+
```

0 commit comments

Comments
 (0)