Unity
Integration of Unity WebGL with the tconnect.io SDK
This section of the documentation describes the integration of Unity WebGL applications with the tconnect.io SDK. The goal is to enable communication between a Unity WebGL application and a React application utilizing the tconnect.io SDK.
Overview
Unity is a powerful platform for developing 2D and 3D games and applications, including WebGL applications. Unity WebGL builds can be seamlessly embedded in React applications. Using the tconnect.io SDK, Web3 features such as wallet integration can be added to these applications.
Use Case
We consider a simple WebGL application with two buttons:
Open Modal: A button that opens the React modal, part of the tconnect.io sdk, to connect to a wallet.
Display Address: A button that displays the wallet address connected through the modal in the Unity application.
Implementation Steps
Unity Script: ReactCommunicator
The following Unity script manages communication between the Unity application and React:
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class ReactCommunicator : MonoBehaviour
{
// Text field to display received data
public TMP_Text displayText;
// Function called by the button
public void RequestDataFromReact()
{
Debug.Log("Unity: Request sent to React!");
// Calls the JavaScript function in the WebGL build
Application.ExternalCall("sendDataToUnity");
}
public void openModalUnity()
{
Debug.Log("Unity: Open modal");
Application.ExternalCall("openModalUnity");
}
// This function is called by React
public void ReceiveDataFromReact(string data)
{
Debug.Log($"Unity: Data received from React - {data}");
displayText.text = data;
}
}React Communication Class: useUnityCommunicator
useUnityCommunicatorThe following React hook manages communication between React and Unity:
Embedding Unity in React
Embedding the Unity WebGL Build: The WebGL build can be embedded in React using the
react-unity-webglpackage or as iFrame.Initializing Communication: The
useUnityCommunicatorhook is used to set up communication between Unity and React.
Conclusion
By following these steps, Unity WebGL applications can be seamlessly embedded in React and integrated with the tconnect.io SDK. This enables smooth integration of Web3 features such as wallet connectivity into your Unity applications. Our demo application showcases more than just the two functionalities presented; it also supports Get Signature, Transfer, and Show Balance, offering a broader range of Web3 interactions to enhance the user experience. Further customizations can be made based on the specific requirements of your application.
Last updated