This repository provides a comprehensive solution to integrate WalletConnect and Substrate-based blockchain features into Unity WebGL projects, specifically targeting the Vara Network. It includes a TypeScript library compatible with Unity's WebGL environment and a step-by-step guide for seamless integration.
- Full support for Vara Network interactions.
- WalletConnect integration for managing blockchain accounts and transactions.
- Compatible with Unity WebGL builds.
- Polyfills for Node.js modules (
Buffer
,crypto
) to enable browser-based environments.
Follow the instructions below to set up and use this repository.
- Node.js (v16 or later)
- Yarn (v1.22 or later)
- Unity (2021.3 LTS or later)
- WebGL Build Module for Unity
- Clone the repository:
git clone https://github.com/Vara-Lab/vara-wallet-unity-webgl.git cd vara-wallet-unity-webgl
- Install dependencies:
yarn install
- Build the TypeScript library:
yarn build
The built library will be available in the dist/ folder as walletconnect.umd.js
-
Copy the Library
Copy the
dist/walletconnect.umd.js
file to theAssets/StreamingAssets
folder in your Unity project.- If the folder
StreamingAssets
doesn’t exist, create it in theAssets
directory.
- If the folder
-
Create a Unity-Compatible Bridge
Add a
.jslib
file (e.g.,walletconnect.jslib
) in theAssets/Plugins
folder.Example
walletconnect.jslib
:mergeInto(LibraryManager.library, { InitializeApi: async function () { try { if (typeof WalletConnect === "undefined") { throw new Error("WalletConnect is not defined. Ensure walletconnect.umd.js is loaded."); } await WalletConnect.ApiService.initializeApi("wss://testnet.vara.network"); console.log("API initialized successfully."); } catch (error) { console.error("Error initializing API:", error); } } });
-
Create a Unity-Compatible Bridge
Create a Unity C# script to call the JavaScript functions.
Example WalletIntegration.cs:
using UnityEngine;
using System.Runtime.InteropServices;
public class WalletIntegration : MonoBehaviour
{
[DllImport("__Internal")]
private static extern void InitializeApi();
void Start()
{
#if UNITY_WEBGL && !UNITY_EDITOR
Debug.Log("Initializing WalletConnect API...");
InitializeApi();
#else
Debug.Log("Not running in WebGL");
#endif
}
}
- Build for WebGL
-
Switch your Unity platform to WebGL: Go to File > Build Settings.
-
Select WebGL and click Switch Platform. Configure the WebGL build settings:
-
In Project Settings > Player > WebGL, disable compression or use Gzip.
-
Build your Unity project:
-
Go to File > Build and Run.
We welcome contributions to this project! If you'd like to contribute, please follow these guidelines:
-
Fork the Repository:
Click on the "Fork" button at the top of this repository to create your own copy. -
Create a Feature Branch:
Create a new branch for your feature or bugfix.git checkout -b feature/your-feature-name
-
Submit a Pull Request:
Once your changes are ready, submit a pull request to themain
branch. Be sure to include a detailed description of your changes and the problem they solve.
This project is licensed under the MIT License. See the LICENSE file for details.