Skip to content
If you have written a Solana program, you can use it in the Unity game engine using a code generator that lets you port an Anchor IDL (a JSON representation of a Solana program) to C#. You can find an examples implementation of a Unity game client that uses the Anchor to Unity port using the Solana Game Preset.

​Generating the Client

When using Anchor you will be able to generate an IDL file which is a JSON representation of your program. With this IDL you can then generate different clients. For example JavaScript or C# for Unity. IDL to C# Converter The converter is a .NET tool, so install the .NET 8 SDK first. These commands create a local tool manifest, install the converter, and generate a C# client for the game:
Replace idl/file.json with the path to your program’s IDL (anchor build writes it to target/idl/) and src/ProgramCode.cs with the C# file to create. anchorgen doesn’t create folders, so the output folder must already exist. This generates a C# representation of your program, which lets you deserialize the data and easily create instructions to the program.

​Building the Transaction in Unity C#

Within Unity game engine we can then use the Solana Unity SDK to interact with the program.
  1. First we find the onchain address of the game data account with TryFindProgramAddress. We need to pass in this account to the transaction so that the Solana runtime knows that we want to change this account.
  2. Next we use the generated client to create a MoveRight instruction.
  3. Then we request a block hash from an RPC node. This is needed so that Solana knows how long the transaction will be valid.
  4. Next we set the fee payer to be the players wallet.
  5. Then we add the move right instruction to the transaction. We can also add multiple instructions to a singe transaction if needed.
  6. Afterwards the transaction gets signed and then send to the RPC node for processing. Solana has different Commitment levels. If we set the commitment level to Confirmed we will be able to get the new state already within the next 500ms.
The MoveRight method below follows these steps. See the full Unity client for the complete example.
A full example of a Unity game that interacts with an Anchor program can be found in the Solana Game Preset which you can find here.