Using ChainSafe with Unity
Chainsafe SDK enables Unity developers to connect their games to the Caduceus EVM compatible blockchain. Unity is a very popular game development platform, and makes for intuitive collaboration with developers working with the Caduceus Metaverse blockchain ecosystem.
Chainsafe SDK contains prefab components powered by the Nethereum .NET library.
Unity 3D currently enjoys about 50 percent market share of the independent game development market, followed by Unreal at about 15%. Larger game studios usually use custom gaming engines built in C++.
While no-code platforms exist, they are not sufficiently integrated with EVM compatible blockchains and this actually represents a higher entry to barrier for blockchain platforms. Hence, we should look at coding platforms as a priority.
Caduceus utilises Chainsafe SDK to connect with the widest possible gaming developer audience, introducing them to GameFi with the easiest possible onboarding.
C Sharp Scripts in Unity
Unity houses C Sharp scripts in a component known as Game Object. Game Object contains the game's control windows and other graphical objects.
Viewing this object lists all relevant C Sharp scripts in a right hand panel pane within the Unity IDE.
Let's take a look at the various components that make up the ChainSafe SDK.
Installation of ChainSafe SDK
ChainSafe Web3 Unity SDK can be installed using the Unity install package file. (Preferred option) https://github.com/ChainSafe/web3.unity/releases
Pull package into Unity project's Assets space. It will extract automatically.
See Loom video for more information:
Pointer_stringify function must be updated to a later stringify function UTF8ToString inside:
Chainsafe with prefab components powered by the Nethereum .NET library is the gaming stack we use to connect to the Caduceus RPC.
The ChainSafe SDK is comprised of independent compoinents that can work on their own, or together. These are the
- the Web3Wallet Library
- the EVM Library
- the Web3GL Library
- Prefabs that enable drag and drop of the libraries functionality into Unity games.
The SDK's assets are structured in the following manner:
Let's examine these components and libraries one by one.
Prefabs are a special type of component that allows fully configured GameObjects to be saved in the Project for reuse. These assets can then be shared between scenes, or even other projects without having to be configured again.
This forms the gaming tech stack we use to connect our games to the Caduceus RPC.
Prefabs have embedded C Sharp scripts, so one should carefully browse over to the right hand side of the screen and double click on the script to open it in VS Code.
The Web3Wallet Library is used for Wallet based or Signed Transactions. This would account for the majority of connection use cases that involve Wallets.
It calls separate endpoints from the EVM:
The private static string url = "https://metamask.app.link/dapp/chainsafe.github.io/game-web3wallet/"; private static string url = "https://chainsafe.github.io/game-web3wallet/";
Example - Message signing
We can sign a message using the SignMessage prefab. It redirects to our Metamask wallet within our Chrome Browser.
See Loom video
Connecting to Caduceus
Recommended methods to connect to Caduceus by including the Chain ID in code
To connect your ChainSafe using game to Caduceus, all you need to do is to specify the chainID within the prefab scripts. This is 256256 for Caduceus Main net, and 512512 for Galaxy Test Net.
Transfer ERC 721
This method is pending Chainsafe SDK approval. Please use the other methods such as Web3Wallet for immediate deployment.
The EVM class is located at web3.unity/Web3Unity/Scripts/Library/EVM.cs
This class connects to a permissioned API host endpoint at "https://api.gaming.chainsafe.io/evm"
Blockchain and network strings resolve to actual data at the above endpoint. You cannot call using the chain ID directly.
EVM Calls EVM calls are utilized by upper level libraries such as ERC 20 and ERC 721. EVM calls utilize Unity's own WebRequest.POST methods
Typical fields utilized by EVM include:
string _chain, string _network, string _contract, string _abi, string _method, string _args, string _multicall
When used as an object, the class's methods are used as follows:
await EVM.CreateMint(chain, network, account, to, cid721, type721);
chain, network, and account must be provided as strings and are pre-set at API endpoint.
The Web3GL-2020x library allows for Web3GL based web applications to utilize Metamask. This enables users to use their Metamask wallet to connect to Web3GL games.
This library contains a network.js file that configures chainids for connection.
A currently active chain can be defined in network.js under the window.web3ChainID object as follows: