GameFi
Using ChainSafe with Unity
15min
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 industry background 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 getting started 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 chainsafe sdk 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 https //github com/chainsafe/web3 unity/releases download web3 unitypackage pull package into unity project's assets space it will extract automatically see loom video for more information https //www loom com/share/7700bdce0c3140b781374cab479ce30c https //www loom com/share/7700bdce0c3140b781374cab479ce30c install web3 unitypackage setup procedure https //docs gaming chainsafe io/installation#viewing the project in the browser https //docs gaming chainsafe io/installation#viewing the project in the browser pointer stringify function must be updated to a later stringify function utf8tostring inside https //github com/chainsafe/web3 unity/blob/499a356c8c4c8975e73206098a2f85dfe722e9f4/web3unity/scripts/library/webgl/web3gl jslib https //github com/chainsafe/web3 unity/blob/499a356c8c4c8975e73206098a2f85dfe722e9f4/web3unity/scripts/library/webgl/web3gl jslib chainsafe with prefab components powered by the nethereum net library is the gaming stack we use to connect to the caduceus rpc architecture 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 https //t20640610 p clickup attachments com/t20640610/148bb928 be39 4a6c 951e d977134f0a37/image png let's examine these components and libraries one by one prefabs 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 web3wallet library 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 \# install truffle \# prefab script code // https //chainlist org/ string chainid = "256256"; // mainnet // contract to interact with string contract = "0xde458cd3deaa28ce67beefe3f45368c875b3ffd6"; // value in wei string value = "0"; // abi in json format string abi = "\[{ \\"inputs\\" \[ { \\"internaltype\\" \\"address\\", \\"name\\" \\"from\\", \\"type\\" \\"address\\" }, { \\"internaltype\\" \\"address\\", \\"name\\" \\"to\\", \\"type\\" \\"address\\" }, { \\"internaltype\\" \\"uint256\\", \\"name\\" \\"tokenid\\", \\"type\\" \\"uint256\\" } ], \\"name\\" \\"safetransferfrom\\", \\"outputs\\" \[], \\"statemutability\\" \\"nonpayable\\", \\"type\\" \\"function\\" }]"; // smart contract method to call string method = "safetransferfrom"; // account to send erc721 to string toaccount = playerprefs getstring("account"); // token id to send string tokenid = "1"; // array of arguments for contract string\[] obj = { playerprefs getstring("account"), toaccount, tokenid }; string args = jsonconvert serializeobject(obj); // create data to interact with smart contract string data = await evm createcontractdata(abi, method, args); // gas limit optional string gaslimit = ""; // gas price optional string gasprice = ""; // send transaction string response = await web3wallet sendtransaction(chainid, contract, value, data, gaslimit, gasprice); print(response); transfer erc20 // https //chainlist org/ \# prefab script code string chainid = "256256"; // mainnet // contract to interact with string contract = "0xc778417e063141139fce010982780140aa0cd5ab"; // value in wei string value = "0"; // abi in json format string abi = ""; // smart contract method to call string method = "transfer"; // account to send erc20 to string toaccount = "0xdd4c825203f97984e7867f11eecc813a036089d1"; // amount of erc20 tokens to send string amount = "1000000000000000"; // array of arguments for contract string\[] obj = {toaccount, amount}; string args = jsonconvert serializeobject(obj); // create data to interact with smart contract string data = await evm createcontractdata(abi, method, args); // gas limit optional string gaslimit = ""; // gas price optional string gasprice = ""; // send transaction string response = await web3wallet sendtransaction(chainid, contract, value, data, gaslimit, gasprice); print(response); evm class 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 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 web3gl 2020x 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