Updating Smart Contract Data in Unity
Update blockchain data in Unity
This article will go over how to update/change data from an Ethereum smart contract inside of Unity.
- Created a Unity Project
- Imported Nethereum Library
- Deployed a smart contract: code with contract address
- Downloaded VS Code and Solidity Plugin
Create C# Definition
(These instructions are similar to Fetching Data from ETH)
Open the solidity code in VS Code. Make sure the Solidity Plugin is installed. Right click the contract and select
Solidity Compile and Code generate CSharp contract definition Below is an example of a Leaderboard contract.
It should generate a contract definition under
Here’s an example of
AddScoreFunction is used to update the leaderboard. It accepts two arguments
This file should be copied over to the Unity project.
Adding C# Definition to Unity
Open your Unity project and make sure the Nethereum
.dll files are already added. Check Part 1 if this isn’t completed.
Next, add the contract definition to your unity project by dragging the file over.
Update Data From an Ethereum Contract
Now that everything is set up, create a script to fetch data from a smart contract.
For demonstration purposes, create a script in the
UpdateLeaderboard.cs and update the script.
Note: Add your private key in the code below.
For more information: https://github.com/Nethereum/Unity3dSimpleSample#transfer-transaction
Give your project a run and check the Console
Copy the transaction ID and check the block explorer.
Congratulations! You’re able to update data from a decentralized source.