The Aptos Unity SDK is a library that provides a convenient way to interact with the Aptos blockchain using C# under the .NET framework. The SDK is designed to offer all the necessary tools to build applications that interact with the Aptos blockchain.
- Binary Canonical Serialization (BCS) encoding and decoding
- Ed25519, SingleKey, MultiKey, and Keyless signer support
- Utilities for transaction building, signing, and submission
- Abstractions over the Aptos Fullnode and Indexer APIs
Initialize an instance of the AptosClient
class to interact with the Aptos blockchain. You can use a pre-defined network configuration from the Networks
class.
// 1. Import the Aptos namespace
using Aptos;
// 2. Initialize the Aptos client
var config = new AptosConfig(Networks.Mainnet);
var client = new AptosUnityClient(config);
// 3. Use the client to interact with the blockchain!
var ledgerInfo = await client.Block.GetLedgerInfo();
To sign and submit a transaction, you can build a payload using the AptosClient
and sign it with an Account
signer.
using Aptos;
// 1. Initialize the Aptos client
var config = new AptosConfig(Networks.Mainnet);
var client = new AptosUnityClient(config);
// 2. Create a new account
var account = Account.Generate();
// 2. Create a transaction payload
var transaction = await client.Transaction.Build(
sender: account,
data: new GenerateEntryFunctionPayloadData(
function: "0x1::aptos_account::transfer_coins",
typeArguments: ["0x1::aptos_coin::AptosCoin"],
functionArguments: [account.Address, "100000"]
)
);
// 3. Sign and submit the transaction
var pendingTransaction = client.Transaction.SignAndSubmitTransaction(account, transaction);
// 4. (Optional) Wait for the transaction to be committed
var committedTransaction = await client.Transaction.WaitForTransaction(pendingTransaction);
Call view functions to query smart contracts.
using Aptos;
// 1. Initialize the Aptos client
var config = new AptosConfig(Networks.Mainnet);
var client = new AptosUnityClient(config);
// 2. Call a view function
var result = await client.Contract.View(
new GenerateViewFunctionPayloadData(
function: "0x1::coin::name",
functionArguments: [],
typeArguments: ["0x1::aptos_coin::AptosCoin"]
)
);
// 3. Parse the return values
Console.WriteLine(result[0]);
- Open the Unity Package Manager (
Window
>Package Manager
). - Click on the
+
button and selectAdd package from git URL...
. - Enter the URL of the Aptos Unity SDK path in this repository:
https://github.com/aptos-labs/unity-sdk.git?path=/Packages/com.aptoslabs.aptos-unity-sdk
- Go to
Releases
and download the latest release. - Drag and drop the
.unitypackage
file into your Unity project.
The Aptos Unity SDK will be available on the Unity Asset Store soon.
The Aptos Unity SDK is under Packages/com.aptoslabs.aptos-unity-sdk
in this repository. Make sure to make any necessary changes in the appropriate directory.
To update the .NET SDK, follow these steps:
- Open the NuGet package manager located towards the top of the Editor.
- Search for the
Aptos
package. Make sure that "Show Pre-release" is checked. - Install the package, it should automatically update it in the
com.aptoslabs.aptos-unity-sdk/Runtime/Libraries
directory.
To export the SDK, you can use the HybridPackage workflow. This will create a .unitypackage
file from the Package Manager. Follow these steps:
- Open the
Asset Store Tools
window located towards the top of the Editor and openAsset Store Uploader
. - Login to your Publisher account. If you don't have one, you can create one here.
- In the
Asset Store Uploader
window, click on the package you created for testing. - Change the
Upload Type
toLocal UPM Package
and change thePackage path
toPackages/com.aptoslabs.aptos-unity-sdk
. - Click on
Export
and this will create a.unitypackage
file for thePackages/com.aptoslabs.aptos-unity-sdk
directory. - (Optional) Click on
Validate
to make sure that the package doesn't have any errors. - You are done! You can distribute this Unity Package for users to import into their projects.