Skip to content

Commit f5f46d2

Browse files
authored
Merge pull request #155 from ChainSafe/v3-updates
Explaining how devs themselves can use our SDK with their custom webGL template and with react-unity-webgl
2 parents 8578033 + 5ab75d0 commit f5f46d2

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

dictionary.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ Mainnet
187187
mainnets
188188
mainnetgoerli
189189
metamask
190+
Majiick
190191
MetaMask
191192
Metamask
192193
Multicall
@@ -231,6 +232,7 @@ RPC
231232
RPCs
232233
Reown
233234
reown
235+
react-unity-webgl
234236
SampleLoginMetamask
235237
SampleMain
236238
scalable
@@ -325,6 +327,7 @@ web3auth
325327
Web3GL
326328
web3unity
327329
Web3Unity
330+
web3UnityInstance
328331
web3authwallet
329332
web3wallet
330333
Web3Initialized

docs/v2.6/18_faq.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,66 @@ IndexOutOfRangeException: Index was outside the bounds of the array.
140140
```
141141

142142
When building to WebGL you could run into this issue on some Unity versions. To fix this, simply open your project's _Player Settings_ then navigate to _Resolution and Presentation_ and pick **Web3.Unity** under _WebGL Template_. Even if it was already selected your project should be able to build to WebGL now.
143+
144+
### I cannot use your WebGL template to build for WebGL since I already have my own template with other dependencies
145+
No problem! Our WebGL template primarily ensures that the web3UnityInstance is assigned after the Unity instance is fully loaded and injected into the HTML. Here's how this is typically handled:
146+
```js
147+
script.onload = () => {
148+
createUnityInstance(canvas, config, (progress) => {
149+
progressBarFull.style.width = 100 * progress + "%";
150+
}).then((unityInstance) => {
151+
web3UnityInstance = unityInstance;
152+
loadingBar.style.display = "none";
153+
fullscreenButton.onclick = () => {
154+
unityInstance.SetFullscreen(1);
155+
};
156+
}).catch((message) => {
157+
alert(message);
158+
});
159+
};
160+
```
161+
This assignment is critical for web3.unity to function properly. Regardless of how or where you are initializing the Unity instance, ensure that the web3UnityInstance is explicitly assigned to your Unity instance during the initialization process.
162+
163+
164+
<details>
165+
<summary>Example how to do it with react-unity-webgl</summary>
166+
167+
In react-unity-webgl add this:
168+
```js
169+
const { unityProvider, sendMessage, isLoaded, loadingProgression } =
170+
useUnityContext({
171+
loaderUrl: 'build/build.loader.js',
172+
dataUrl: 'build/build.data',
173+
frameworkUrl: 'build/build.framework.js',
174+
codeUrl: 'build/build.wasm'
175+
})
176+
177+
useEffect(() => {
178+
window.web3UnityInstance = {
179+
SendMessage: (gameObjectName, methodName, parameter) => {
180+
if (!gameObjectName || !methodName) {
181+
console.error(
182+
"SendMessage requires at least 'gameObjectName' and 'methodName'."
183+
)
184+
return
185+
}
186+
187+
// Call the sendMessage function
188+
sendMessage(gameObjectName, methodName, parameter)
189+
190+
// Log for debugging
191+
console.log(
192+
'SendMessage called with:',
193+
gameObjectName,
194+
methodName,
195+
parameter
196+
)
197+
}
198+
}
199+
}, [isLoaded])
200+
201+
```
202+
Big thanks to Majiick for this solution.
203+
204+
</details>
205+

0 commit comments

Comments
 (0)