WalletConnect / WalletConnectUnity

WalletConnect v2 and Modal for Unity 🎮
MIT License
133 stars 49 forks source link
blockchain ethereum evm gamefi nft unity walletconnect web3

WalletConnectUnity

This repository is a monorepo of low-level packages that bring WalletConnect to Unity.

AppKit

WalletConenct AppKit offers a feature-rich and user-friendly solution for integrating Web3 with Unity-based apps. Use the Modal or Core packages from the WalletConnectUnity repository only when AppKit does not meet specific needs, such as integrations with non-EVM chains. Below is a feature matrix comparing WalletConnect Modal and AppKit.

WalletConnect Modal AppKit (Web3Modal)
WalletConnect
Network Switching
EVM Interaction API
Coinbase Wallet ✅ (WebGL only)
Browser Extension Wallets ✅ (WebGL only)
Fiat Onramp ✅ (WebGL only)
ENS Resolution
Email & Social Login 🔜
UI Framework uGUI UI Toolkit in native, HTML in WebGL
Supported Networks Blockchain-agnostic Only EVM

WalletConnectUnity Packages

Package Description OpenUPM
Core High-level, Unity-friendly extension of WalletConnectSharp
- Automatic active session management
- Option to resume session from storage
- Deep linking support
- IL2CPP support
- Lightweight IJsonRpcConnection implementation
- QR Code generation utility
- API to load wallets data and visual assets
openupm
Modal Simplest and most minimal way to connect your players with WalletConnect openupm
UI This is a technical package that provides UI for WalletConnect Modal. It is not intended to be used directly, but rather as a dependency of WalletConnect Modal. openupm
Nethereum This Unity package provides a simple way to integrate WalletConnect with Nethereum library. openupm

Older versions of WalletConnectUnity are available under legacy/* branches

Prerequisites

Supported Platforms

Documentation

Installation

Install via OpenUPM CLI To install packages via OpenUPM, you need to have [Node.js](https://nodejs.org/en/) and [openupm-cli](https://openupm.com/docs/getting-started.html#installing-openupm-cli) installed. Once you have them installed, you can run the following commands: - **WalletConnect Modal**: ```bash openupm add com.walletconnect.modal ``` - **WalletConnectUnity Core**: ```bash openupm add com.walletconnect.core ```
Install via Package Manager with OpenUPM 0. Open `Advanced Project Settings` from the gear ⚙ menu located at the top right of the Package Manager’s toolbar 1. Add a new scoped registry with the following details: - Name: `OpenUPM` - URL: `https://package.openupm.com` - Scope(s): `com.walletconnect` 2. Press plus ➕ and then `Save` buttons 3. In the Package Manager windows open the add ➕ menu from the toolbar 4. Select `Add package by name...` 5. Enter the name of the package you want to install: - **WalletConnectUnity Modal**: `com.walletconnect.modal` - **WalletConnectUnity Core**: `com.walletconnect.core` 6. Press `Add` button
Install via Package Manager with Git URL 0. Open the add ➕ menu in the Package Manager’s toolbar 1. Select `Add package from git URL...` 2. Enter the package URL. Note that when installing via a git URL, the package manager won't install git dependencies automatically. Follow the error messages from the console and add all necessary packages manually - **WalletConnectUnity Modal**: `https://github.com/WalletConnect/WalletConnectUnity.git?path=Packages/com.walletconnect.modal` - **WalletConnectUnity UI**: `https://github.com/WalletConnect/WalletConnectUnity.git?path=Packages/com.walletconnect.ui` - **WalletConnectUnity Core**: `https://github.com/WalletConnect/WalletConnectUnity.git?path=Packages/com.walletconnect.core` 3. Press `Add` button It's possible to lock the version of the package by adding `#{version}` at the end of the git URL, where `#{version}` is the git tag of the version you want to use. For example, to install version `1.0.0` of WalletConnectUnity Modal, use the following URL: ``` https://github.com/WalletConnect/WalletConnectUnity.git?path=Packages/com.walletconnect.modal#modal/1.0.0 ```

Usage

  1. Set up in project id and metadata WalletConnectProjectConfig ScriptableAsset (created automatically located at Assets/WalletConnectUnity/Resources/WalletConnectProjectConfig.asset, do NOT move it outside of Resources directory).
  2. Initialize WalletConnect and connect wallet:
// Initialize singleton
await WalletConnect.Instance.InitializeAsync();

// Or handle instancing manually
var walletConnectUnity = new WalletConnect();
await walletConnectUnity.InitializeAsync();

// Try resume last session
var sessionResumed = await WalletConnect.Instance.TryResumeSessionAsync();              
if (!sessionResumed)                                                                         
{                                                                                            
    var connectedData = await WalletConnect.Instance.ConnectAsync(connectOptions);

    // Create QR code texture
    var texture = WalletConnectUnity.Core.Utils.QRCode.EncodeTexture(connectedData.Uri);

    // ... Display QR code texture

    // Wait for wallet approval
    await connectedData.Approval;                                                            
}                                                                                            

WalletConnectProjectConfig Fields

WebGL Usage

Due to WebGL's single-threaded nature, certain asynchronous operations like Task.Run, Task.ContinueWith, Task.Delay, and ConfigureAwait(false) are not natively supported.

To enable these operations in WebGL builds, an additional third-party package, WebGLThreadingPatcher, is required. This package modifies the Unity WebGL build to delegate work to the SynchronizationContext, allowing these operations to be executed on the same thread without blocking the main application. Please note that all tasks are still executed on a single thread, and any blocking calls will freeze the entire application.

The WebGLThreadingPatcher package can be added via git URL:

https://github.com/VolodymyrBS/WebGLThreadingPatcher.git

Sample