Originbyte
Search…
⌃K

Unity SDK

The Unity toolset for game developers on Sui

Features

  • For Rpc clients’ direct interaction with the Sui JSON-RPC https://docs.sui.io/sui-jsonrpc
    • Read API
    • Event Read API
    • Transaction Builder Api
    • Event streaming support
    • Helper methods to build and execute transactions
    • Typed move calls
  • Wallet Management
    • Generate and Restore key pairs with Mnemonics (currently Ed25519 supported)
      • SLIP-0010 : Universal private key derivation from master private key also supported
    • Sign transactions
    • Store key pair in PlayerPrefs
  • Interact with Origin Byte Nft Protocol https://github.com/Origin-Byte/nft-protocol
  • Helper Scripts and prefabs to load NFTs (even Capys!)
  • Windows desktop and WebGL platforms tested
  • Unity 2021.3.10f1 LTS or later supported
  • Samples are using Sui 0.20.0 devnet

Getting Started

To start, download the repository and open with Unity:

The Samples are in ./Assets/SuiUnitySDK/Samples Open a Scene from ./Assets/SuiUnitySDK/Samples/Scenes

Installation

Choose one of the following methods:

1. Using the openupm registry, you can install it via openupm-cli:

openupm add com.originbyte.suiunitysdk

2. Via 'Add package from git URL' in the Unity Package Manager:

https://github.com/Origin-Byte/sui-unity-sdk.git#upm

3. Via git url by adding this entry in your manifest.json:

"com.originbyte.suiunitysdk": "https://github.com/Origin-Byte/sui-unity-sdk.git#upm"

4. Download the latest sui-unity-sdk.unitypackage from the releases: https://github.com/Origin-Byte/sui-unity-sdk/releases

  1. 1.
    Drag and drop the sui-unity-sdk.unitypackage to the Project window
  2. 2.
    As soon as the Import Windows pop up, click the Import button

Start using the SDK via SuiApi and SuiWallet classes. You can check out some samples below:

Usage Samples

All samples can be found in SuiUnitySDK/Samples

Wallet management

You can click on the Create New Wallet or Import Wallet buttons to initialize the currently active Wallet.
PlayerPrefs is currently used as a keystore. It saves the last active wallet and will be loaded from there on the next restart.
You are now ready to execute transactions that require signatures.
var mnemonics = SuiWallet.CreateNewWallet();
...
SuiWallet.RestoreWalletFromMnemonics(mnemonics);
var activeAddress = SuiWallet.GetActiveAddress();

RPC Read API

Once entering an address, you'll be able to see the results as a formatted JSON.
var ownedObjectsResult = await SuiApi.Client.GetObjectsOwnedByAddressAsync(address);
Ouput.text = JsonConvert.SerializeObject(ownedObjectsResult.Result, Formatting.Indented);

RPC Move Call and Execute Transaction Samples

This sample code calls a function in a published Move Package that has a counter module with an increment function. It modifies a shared object, incrementing the counter by 1.
You can find out more about this here.
var signer = SuiWallet.GetActiveAddress();
var packageObjectId = "0xa21da7987c2b75870ddb4d638600f9af950b64c6";
var module = "counter";
var function = "increment";
var typeArgs = System.Array.Empty<string>();
var args = new object[] { SharedCounterObjectId };
var gasObjectId = GasObjectIdInput.text;
var rpcResult = await SuiApi.Client.MoveCallAsync(signer, packageObjectId, module, function, typeArgs, args, gasObjectId, 2000);
var keyPair = SuiWallet.GetActiveKeyPair();
var txBytes = rpcResult.Result.TxBytes;
var signature = keyPair.Sign(rpcResult.Result.TxBytes);
var pkBase64 = keyPair.PublicKeyBase64;
await SuiApi.Client.ExecuteTransactionAsync(txBytes, SuiSignatureScheme.ED25519, signature, pkBase64);
...

Mint An NFT using the OriginByte NFT Protocol

This sample demonstrates the minting of an NFT for a collection using the OriginByte NFT protocol with RPC calls. More information on the Protocol can be found here. In this sample we automatically query for 2 separate SUI coin type objects, as the Move call executes in a batch transaction and Sui does not allow the same coin object to be both used as gas and mutate in the Move call.
var signer = SuiWallet.GetActiveAddress();
// package id of the Nft Protocol
var packageObjectId = "0x1e5a734576e8d8c885cd4cf75665c05d9944ae34";
var module = "std_nft";
var function = "mint_and_transfer";
var typeArgs = System.Array.Empty<string>();
// We need 2 separate gas objects because both of them will be mutated in a batch transaction
var gasObjectIds = await SuiHelper.GetCoinObjectIdsAboveBalancesOwnedByAddressAsync(signer, 2);
if (gasObjectIds.Count < 2)
{
Debug.LogError("Could not retrieve 2 sui coin objects with at least 2000 balance. Please send more SUI to your address");
return;
}
var args = new object[] { NFTNameInputField.text, NFTUrlInputField.text, false, new object[] { "description" },
new object[] { NFTDescriptionInputField.text }, NFTCollectionIdInputField.text, gasObjectIds[0], signer };
NFTMintedText.gameObject.SetActive(false);
NFTMintedReadonlyInputField.gameObject.SetActive(false);
var rpcResult = await SuiApi.Client.MoveCallAsync(signer, packageObjectId, module, function, typeArgs, args, gasObjectIds[1], 2000);
if (rpcResult.IsSuccess)
{
var keyPair = SuiWallet.GetActiveKeyPair();
var txBytes = rpcResult.Result.TxBytes;
var signature = keyPair.Sign(rpcResult.Result.TxBytes);
var pkBase64 = keyPair.PublicKeyBase64;
var txRpcResult = await SuiApi.Client.ExecuteTransactionAsync(txBytes, SuiSignatureScheme.ED25519, signature, pkBase64);
}
else
{
Debug.LogError("Something went wrong with the move call: " + rpcResult.ErrorMessage);
}

Nft loaders

Check out the NftLoaders scene. Contains samples for using Nfts as UI Image sprite, main texture of material.
Change the Ids and Address to yours.

Capy Loader

You can also import Capys! This uses the experimental Vector Graphics package to render the SVG images as PNGs. Check out the CapyNftLoaders scene to see it in action!
It can load the whole hierarchy of accessories for the Capy, or just the main Capy object.

Dependencies

Every dependency used by the SDK can be found in ./Assets/SuiUnitySDK/Plugins.
NuGet packages are in ./Assets/SuiUnitySDK/Plugins/NuGetPackages.
  • suinet, our internal C#/.NET Sui library. It will be regularly updated as features are added to the core library.
  • Chaos.NaCl.Standard
  • Portable.BouncyCastle
  • Newtonsoft.Json (included in recent Unity versions by default)
  • com.unity.vectorgraphics
    • if you got compilation errors, you need to add this entry to your .Packages/manifest.json file's dependencies section:
    "com.unity.vectorgraphics": "2.0.0-preview.20"

Samples Dependencies

Dependencies used by the Samples can be found in ./Assets/SuiUnitySDK/Samples/Plugins
  • TextMesh Pro
  • WebGLCopyAndPaste

Our Roadmap

  • Mobile platform support (iOS, Android)
  • WalletConnect
  • Streaming RPC client, Event subscription
  • Secp256k1 keypair support
  • More samples
  • OriginByte NFT ecosystem access from Unity
  • Higher level APIs, easy-to-use Prefabs
  • Rust & Typescript SDK parity

Our Community