This example shows how to integrate a custom Hathor token to the demo Unity game. This will draw tokens from a central wallet and distribute it to the player's wallet. In this example, the player's wallet will be hardcoded in the config file. In a real integration, each player should be allocated a wallet address dynamically.
- Hathor Headless wallet installed and running. This should be loaded and have tokens available. You can use any custom token or HTR. For information on how to run a headless wallet click here
- Unity Hub;
- Text Editor
- Download Unity Hub
- After the download is completed, run the installation program
- Once the installation was finished and the Unity Hub is running on your machine, create a new project
- Give your project a name
- On your new project, select Window / Assets Store
- A new browser window with the Unity Store will be opened, on this screen search for 3d Game Kit
- On the search results, click on the 3D Game Kit, the 3d Game Kit will be opened
- Select Add to my assets, this will add the game to your assets
- Click on Open in Unity
- The Package Manager window will open with the game selected, click on Import
- A Warning message will appear, on this message click Import
- A second window will open informing about dependencies, click on Install/Upgrade
- A third window called Import Unity Package will open, select Import
- After the asset is imported to your project, close the Package manager window.
- Download the project to your local machine
- Drag and drop the project directory to your Unity project Assets
- On your assets, select the UnityHathor directory
- Select Scripts
- On the Scripts directory Open HathorConnect file
- Change the URL address to the correct headless wallet address that you are using.
- Save the file
- On the Scripts directory open HathorPlayer file
- Change the
x_wallet_idfield with the correct name of your wallet, which you just set up - Save the file
- On your assets open the 3dGameKit, Scenes, GamePlay directory
- Select and open the Level1 file
- Once it opens, on your assets go to the HathorUnity, Prefabs directory
- Select the HathorCountFloatingText.prefab file
- Drag the file to the Level1, System on the Hierarchy list.
- On your assets go to 3DGameKit, Prefabs, Characters, Ellen and select Ellen.prefab
- Open the file
- On the Inspector window, go to the end of the file and click on Add Component
- On the New component window, select HathorPlayer
- Open your hathor Wallet (Desktop or Mobile) and copy your wallet address
- Paste the wallet address on the WalletPlayer field
- Save the file
- On your assets select 3DGameKit, Scripts, Game, Enemies, Chomper directory
- On the directory open the ChomperBehaviour.cs file
- Import
UnityHathor
using UnityEngine;
using UnityHathor; // add this line- Create the Hathor variables, after line 21
public static readonly int hashIdleState = Animator.StringToHash("ChomperIdle");
//Hathor - Start Variables Block
public GameObject hathorCountPopup;
public GameObject playerObject;
//Hathor - End Variables Block- Add code to transfer tokens inside death or destroy functions (around line 243)
// Inside your death or destroy functions
// Hathor - Grab the wallet inserted in the character wallet
var walletPlayer = playerObject.GetComponent<HathorPlayer>().walletPlayer;
// generate a random value between 0 and 5 where 5 is 0.05 tokens
int hathorAmount = UnityEngine.Random.Range(0, 5);
// Hathor - Check if hathorCountPopup is no null
if (hathorCountPopup)
{
// Hathor - Shows the amount of coins received
HathorQueue.ShowHathorCountPopup(hathorCountPopup, hathorAmount, transform);
}
// Hathor - If the value is below or equal zero, do not attempt to transact
if (hathorAmount > 0)
{
// Hathor - Create a new struct with the player wallet and the value to be transacted
HathorTransactStruct newTx = new HathorTransactStruct(walletPlayer, hathorAmount);
// Hathor - Add the new struct to the transaction queue
HathorQueue.transactsQueue.Enqueue(newTx);
}- Save the file
- On your assets go to 3DGameKit, Prefabs, Character, Enemies, Chomper
- Select the Chomper.prefab file and open it
- On the inspector window search for the Hathor Count Popup property
- Click on the Select Game Object icon
- On the Select Game Object window, write HathorCountFloatingText
- On the property Player Object and click on the Select Game Object icon
- On the Select Game Object window, write Ellen
- Run the game. For each Chomper that Ellen kills, you’ll receive new Tokens