• Stars
    star
    103
  • Rank 333,046 (Top 7 %)
  • Language
    JavaScript
  • Created about 2 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Best in class web3 SDK for Unity games



thirdweb Unity SDK

Join our Discord!
Preview

Documentation

See full documentation on the thirdweb portal.

Technical Demo

Try out our multichain game that leverages Embedded and Smart Wallets to create seamless experiences, built in 3 weeks - Web3 Warriors.

image

Supported platforms

Build games for WebGL, Standalone and Mobile using 1000+ supported chains.

tw_wallets

Installation

Head over to the releases page and download the latest .unitypackage file.

Follow our Getting Started Guide.

All you need is a ThirdwebManager prefab in your scene to interact with the SDK from anywhere!

Various blockchain interaction examples are available in our Scene_Prefabs scene.

Payment related interaction examples are available in our Scene_Pay scene.

Notes:

  • The SDK has been tested on Web, Desktop and Mobile platforms using Unity 2021 and 2022 LTS. We highly recommend using 2022 LTS.
  • The example scenes are built using Unity 2022 LTS, it may look off in previous versions of Unity.
  • The Newtonsoft DLL is included as part of the Unity Package, feel free to deselect it if you already have it installed as a dependency to avoid conflicts.
  • If using .NET Framework and encountering an error related to HttpUtility, create a file csc.rsp that includes -r:System.Web.dll and save it under Assets.

Usage

In order to access the SDK, you only need to have a ThirdwebManager in your scene.

// Configure the connection
var connection = new WalletConnection(
  provider: WalletProvider.EmbeddedWallet, // The wallet provider you want to connect to (Required)
  chainId: 5,                              // The chain you want to connect to (Required)
  email: "[email protected]"                 // The email you want to authenticate with (Required for this provider)
);

// Connect the wallet
string address = await ThirdwebManager.Instance.SDK.Wallet.Connect(connection);

// Interact with the wallet
CurrencyValue balance = await ThirdwebManager.Instance.SDK.Wallet.GetBalance();
var signature = await ThirdwebManager.Instance.SDK.Wallet.Sign("message to sign");

// Get an instance of a deployed contract (no ABI required!)
var contract = ThirdwebManager.Instance.SDK.GetContract("0x...");

// Fetch data from any ERC20/721/1155 or Marketplace contract
CurrencyValue currencyValue = await contract.ERC20.TotalSupply();
NFT erc721NFT = await contract.ERC721.Get(tokenId);
List<NFT> erc1155NFTs = await contract.ERC1155.GetAll();
List<Listing> listings = await contract.Marketplace.DirectListings.GetAllListings();

// Execute transactions from the connected wallet
await contract.ERC20.Mint("1.2");
await contract.ERC721.Signature.Mint(signedPayload);
await contract.ERC1155.Claim(tokenId, quantity);
await contract.Marketplace.DirectListings.BuyListing(listingId, quantity);

// Custom interactions
var res = await contract.Read<string>("myReadFunction", arg1, arg2, ...);
var txRes = await contract.Write("myWriteFunction", arg1, arg2, ...);

Build

General

  • Use Smaller (faster) Builds in the Build Settings (IL2CPP Code Generation in Unity 2022).
  • Use IL2CPP over Mono when possible in the Player Settings.
  • Using the SDK in the editor (pressing Play) is an accurate reflection of what you can expect to see on native platforms.
  • In most cases, setting Managed Stripping Level to minimal when using IL2CPP is also helpful - you can find it under Player Settings > Other Settings > Optimization

WebGL

  • In order to communicate with the SDK on WebGL, you need to Build and run your project so it runs in a browser context.
  • Open your Build settings, select WebGL as the target platform.
  • Open Player settings > Resolution and Presentation and under WebGLTemplate choose Thirdweb.
  • Save and click Build and Run to test out your game in a browser.

Important: If you're uploading your build, set Compression Format to Disabled in Player Settings > Publishing Settings.

Please note that Embedded Wallets (OAuth version) may not work when testing locally using Unity's default Build and Run feature for WebGL.

You must host the build or run it locally yourself after adding the Cross-Origin-Opener-Policy header and setting it to same-origin-allow-popups.

Here's a simple way to do so, assuming you are in your WebGL build output folder:

const express = require('express');
const app = express();
const port = 8000;

app.use(function(req, res, next) {
  res.header('Cross-Origin-Opener-Policy', 'same-origin-allow-popups');
  next();
});

app.use(express.static('.'));
app.listen(port, () => console.log(`Server running on http://localhost:${port}`));

Once again, please note that no action is needed for hosted builds.

Mobile

  • For Android, it is best to run Force Resolve from the Assets menu > External Dependency Manager > Android Resolver > Force Resolve before building your game.
  • For iOS, if you are missing a MetaMask package, you can double click on main.unitypackage under Assets\Thirdweb\Plugins\MetaMask\Installer\Packages and reimport the iOS folder (only).
  • If you are having trouble building in XCode, make sure ENABLE_BITCODE is disabled and that the Embedded Frameworks in your Build Phases contain potentially missing frameworks like MetaMask or Starscream. You may also need to remove the Thirdweb/Core/Plugins/MetaMask/Plugins/iOS/iphoneos/MetaMask_iOS.framework/Frameworks folder in some cases. Recent versions should no longer require this.

Miscellaneous

If you don't want to use Minimal Stripping, you could instead create a link.xml file under your Assets folder and include assemblies that must be preserved, for instance:

<linker>
    <!--Thirdweb-->
    <assembly fullname="Amazon.Extensions.CognitoAuthentication" preserve="all" />
    <assembly fullname="AWSSDK.CognitoIdentity" preserve="all" />
    <assembly fullname="AWSSDK.CognitoIdentityProvider" preserve="all" />
    <assembly fullname="AWSSDK.Core" preserve="all" />
    <assembly fullname="AWSSDK.Lambda" preserve="all" />
    <assembly fullname="AWSSDK.SecurityToken" preserve="all" />
    <assembly fullname="embedded-wallet" preserve="all" />

    <!--Other-->
    <assembly fullname="System.Runtime.Serialization" preserve="all" />
    <assembly fullname="Newtonsoft.Json" preserve="all" />
    <assembly fullname="System" preserve="all">
        <type fullname="System.ComponentModel.TypeConverter" preserve="all" />
        <type fullname="System.ComponentModel.ArrayConverter" preserve="all" />
        <type fullname="System.ComponentModel.BaseNumberConverter" preserve="all" />
        <type fullname="System.ComponentModel.BooleanConverter" preserve="all" />
        <type fullname="System.ComponentModel.ByteConverter" preserve="all" />
        <type fullname="System.ComponentModel.CharConverter" preserve="all" />
        <type fullname="System.ComponentModel.CollectionConverter" preserve="all" />
        <type fullname="System.ComponentModel.ComponentConverter" preserve="all" />
        <type fullname="System.ComponentModel.CultureInfoConverter" preserve="all" />
        <type fullname="System.ComponentModel.DateTimeConverter" preserve="all" />
        <type fullname="System.ComponentModel.DecimalConverter" preserve="all" />
        <type fullname="System.ComponentModel.DoubleConverter" preserve="all" />
        <type fullname="System.ComponentModel.EnumConverter" preserve="all" />
        <type fullname="System.ComponentModel.ExpandableObjectConverter" preserve="all" />
        <type fullname="System.ComponentModel.Int16Converter" preserve="all" />
        <type fullname="System.ComponentModel.Int32Converter" preserve="all" />
        <type fullname="System.ComponentModel.Int64Converter" preserve="all" />
        <type fullname="System.ComponentModel.NullableConverter" preserve="all" />
        <type fullname="System.ComponentModel.SByteConverter" preserve="all" />
        <type fullname="System.ComponentModel.SingleConverter" preserve="all" />
        <type fullname="System.ComponentModel.StringConverter" preserve="all" />
        <type fullname="System.ComponentModel.TimeSpanConverter" preserve="all" />
        <type fullname="System.ComponentModel.UInt16Converter" preserve="all" />
        <type fullname="System.ComponentModel.UInt32Converter" preserve="all" />
        <type fullname="System.ComponentModel.UInt64Converter" preserve="all" />
    </assembly>
    <assembly fullname="Nethereum.ABI" preserve="all" />
    <assembly fullname="Nethereum.Accounts" preserve="all" />
    <assembly fullname="Nethereum.BlockchainProcessing" preserve="all" />
    <assembly fullname="Nethereum.Contracts" preserve="all" />
    <assembly fullname="Nethereum.HdWallet" preserve="all" />
    <assembly fullname="Nethereum.Hex" preserve="all" />
    <assembly fullname="Nethereum.JsonRpc.Client" preserve="all" />
    <assembly fullname="Nethereum.JsonRpc.RpcClient" preserve="all" />
    <assembly fullname="Nethereum.Keystore" preserve="all" />
    <assembly fullname="Nethereum.Merkle" preserve="all" />
    <assembly fullname="Nethereum.Merkle.Patricia" preserve="all" />
    <assembly fullname="Nethereum.Model" preserve="all" />
    <assembly fullname="Nethereum.RLP" preserve=" all" />
    <assembly fullname="Nethereum.RPC" preserve=" all" />
    <assembly fullname="Nethereum.Signer" preserve=" all" />
    <assembly fullname="Nethereum.Signer.EIP712" preserve=" all" />
    <assembly fullname="Nethereum.Siwe" preserve=" all" />
    <assembly fullname="Nethereum.Siwe.Core" preserve=" all" />
    <assembly fullname="Nethereum.Util" preserve=" all" />
    <assembly fullname="Nethereum.Web3" preserve=" all" />
</linker>

Additional Links

Prefab Examples

image

The Examples folder contains a demo scene Scene_Prefabs using our user-friendly prefabs - they include script examples to get inspired and are entirely optional.

All Prefabs require the ThirdwebManager prefab to get the SDK Instance, drag and drop it into your scene and select the networks you want to support from the Inspector.

Connect Wallet - All-in-one drag & drop wallet supporting multiple wallet providers, network switching, balance displaying and more!

  • Drag and drop it into your scene.
  • Set up the networks you want to support from the ThirdwebManager prefab.
  • You can add listeners from the inspector for various wallet events.

NFT Loader - Standalone drag & drop grid/scroll view of NFTs you ask it to display!

  • Go to the prefab's Settings in the Inspector.
  • Load specific NFTs with token ID.
  • Load a specific range of NFTs.
  • Load NFTs owned by a specific wallet.
  • Or any combination of the above - they will all be displayed automatically in a grid view with vertical scroll!
  • Customize the prefab's ScrollView and Content gameobjects if you want your content to behave differently.

NFT - Displays an NFT by calling LoadNFT through script!

  • Instantiate this Prefab through script.
  • Get its Prefab_NFT component.
  • Call the LoadNFT function and pass it your NFT struct to display your fetched NFT's images automatically.
  • Customize the prefab to add text/decorations and customize LoadNFT to use your NFT's metadata if you want to populate that text.
NFT nft = await contract.ERC721.Get(0);
Prefab_NFT nftPrefabScript = Instantiate(nftPrefab);
nftPrefabScript.LoadNFT(nft);

Events - Fetch and manipulate Contract Events with a simple API!

  • Get specific events from any contract.
  • Get all events from any contract.
  • Event listener support with callback actions.
  • Optional query filters.

Reading - Reading from a contract!

  • Fetch ERC20 Token(s).
  • Fetch ERC721 NFT(s).
  • Fetch ERC1155 NFT(s).
  • Fetch Marketplace Listing(s).
  • Fetch Pack contents.

Writing - Writing to a contract!

  • Mint ERC20 Token(s).
  • Mint ERC721 NFT(s).
  • Mint ERC1155 NFT(s).
  • Buy Marketplace Listing(s).
  • Buy a Pack.

Miscellaneous - More examples!

  • Get (Native) Balance.
  • Custom Contract Read/Write Calls.
  • Authentication.
  • Message Signing.

Smart Wallet

  • Adding admins to your smart wallet.
  • Removing admins from your smart wallet.
  • Creating session keys to grant temporary/restricted access to additional signers.

See full documentation on the thirdweb portal.

Contributing to thirdweb Unity SDK

We warmly welcome contributions to the thirdweb Unity SDK! If you're looking to contribute, here's how you can get started.

How to Contribute

  1. Fork the Repository: Click the "Fork" button at the top right of this page to create your own copy of the repository.
  2. Clone Your Fork: Clone your fork to your local machine for development.
  3. Create a Feature Branch: Make a new branch for your changes. This helps keep contributions organized.
  4. Make Your Changes: Work on your changes. Make sure they are well-tested and don't break existing functionality.
  5. Commit Your Changes: Commit your changes with a clear and descriptive commit message.
  6. Push to Your Fork: Push your changes to your forked repository.
  7. Submit a Pull Request: From your fork, submit a pull request to our main repository. Provide a clear description of your changes and any relevant issue numbers.

Notes:

  • For WebGL-specific contributions, you may contribute to our JS package as well. The bulk of WebGL-specific behavior goes through its Unity bridge.
  • For new Wallet Provider contributions, see our guide to Submit your Wallet.

Guidelines

  • Keep It Simple: Try to keep your contributions small and simple. This makes them easier to review and merge.
  • Supported Platforms: Make sure your changes work on either WebGL only, Native platforms only, or both. A good test is to build Scene_Prefabs to test your changes there.
  • Test Your Code: Ensure your code works as expected and doesn't introduce new issues.
  • Be Respectful: When discussing changes, always be respectful and constructive.

Need Help?

If you're unsure about something or need help, feel free to reach out to us on Discord.

Thank you for contributing to the thirdweb Unity SDK!

More Repositories

1

js

Best in class web3 SDKs for Browser, Node and Mobile apps
TypeScript
397
star
2

nftlabs-protocols

TypeScript
186
star
3

typescript-sdk

Best in class web3 SDK for Browser, Node and Mobile apps
TypeScript
135
star
4

python-sdk

Best in class web3 SDK for Python 3.7+
Python
82
star
5

dynamic-contracts

Architectural pattern for writing dynamic smart contracts in Solidity.
Solidity
69
star
6

go-sdk

Best in class web3 SDK for Go 1.16+
Go
34
star
7

docs

thirdweb docs portal
HTML
33
star
8

thirdweb-cli

Publish and deploy smart contracts without dealing with private keys
TypeScript
27
star
9

nftlabs-sdk-example

TypeScript
25
star
10

nftpacks-contracts

NFT Packs is a tool for NFT giveaways. Bundle ERC 721 NFTs as rewards into packs and airdrop them to an audience.
Solidity
19
star
11

examples

TypeScript
17
star
12

hashlips-to-thirdweb

TypeScript
16
star
13

docs-v2

thirdweb docs
MDX
13
star
14

portal

portal.thirdweb.com
TypeScript
12
star
15

ui

TypeScript
11
star
16

generate-contract-snapshot

Generate a snapshot of owners with quantity for any erc721/erc1155, on any chain.
TypeScript
11
star
17

shopify-thirdweb-theme

thirdweb shopify theme
JavaScript
10
star
18

niftyswap.gg

Trade NFTs.
Solidity
10
star
19

nextjs-membership-lounge

TypeScript
10
star
20

proxy-contract-wallet-example

Example smart contract setup for mapping an end-user wallet to an ownable smart contract as its 'proxy wallet'.
Solidity
9
star
21

modular-contracts

The next iteration of thirdweb smart contracts. Install hooks into core contracts.
Solidity
7
star
22

auth

Best in class wallet authentication for Node backends
TypeScript
6
star
23

ozdefender-autotask

JavaScript
5
star
24

support-discord-bot

A self-hosted dedicated forum-based support Discord bot for the thirdweb community.
JavaScript
5
star
25

web3warriors

Landing page for Web3Warriors
TypeScript
5
star
26

built-on-thirdweb

TypeScript
5
star
27

qr-membership-website

JavaScript
4
star
28

pack-protocol

$PACK Protocol lets anyone create and sell packs filled with rewards. A pack can be opened only once. On opening a pack, a reward from the pack is distributed to the pack opener.
TypeScript
4
star
29

signature-minting-to-qr-code

TypeScript
3
star
30

shopify-demo-store

TypeScript
3
star
31

portal-content

3
star
32

widgets

TypeScript
2
star
33

unity-webgl-template

HTML
2
star
34

unity-webgl-sdk

C#
2
star
35

signature-based-minting-sample-app

TypeScript
2
star
36

chain-icons

TypeScript
2
star
37

cookiedao

The best DAO for cookie lovers
JavaScript
2
star
38

farza-nft

TypeScript
1
star
39

deterministic-deploy-demo

TypeScript
1
star
40

react-unity-webgl-example

Mathematica
1
star
41

hotpotato

TypeScript
1
star
42

nftlabs-python-sdk-example

Python
1
star
43

guides

To keep tracks of guides that go on portal/guides.
1
star
44

bakery.gg

Jake/Nacho/Doug Hackweek project
Solidity
1
star
45

pack-protocol-subgraph

TypeScript
1
star
46

unity-sdk-bridge

TypeScript
1
star
47

contracts-next-scripts

Scripts showing how to interact with hooks architecture using the thirdweb v5 SDK.
JavaScript
1
star