Unity Integration
Integrating leNFT's SDK into Unity Games
Overview
Some developers might want to integrate leNFT's SDK directly into their Unity games. This can be achieved by adding JavaScript sources to your project, and then call those functions directly from your script code as described in the Unity documentation. This guide provides step-by-step instructions on how to integrate the leNFT SDK into your Unity project using Unity WebGL.
Prerequisites
Unity installed with WebGL support.
Basic knowledge of C# and JavaScript.
Familiarity with React and Unity WebGL.
File Structure
Ensure that the following files are placed in the appropriate locations within your Unity project:
lenft.jslib: Place this file in your project's Assets/Plugins folder. It will be responsible for interacting with the functions in your browser's JavaScript.
lenft.cs: Create a C# script with this name. It should contain a C# class that imports the JavaScript functions. Place it inside the Assets/Scripts folder.
Step 1: Implement lenft.jslib
In the this example we integrate two functions, one to connect the user's wallet and the other to perform a buy operation for 1 NFT with a maximum price of 0.15 ETH.
Step 2: Implementing lenft.cs
This script imports the JavaScript functions and makes them callable from C#.
Step 3: Expose Functions in Browser JavaScript
To expose the functions called in lenft.jslib in your browser's JavaScript, consider using React Unity WebGL or a similar setup. Below is an example of how to do it with React Unity WebGL:
In this example we wrap the leNFT SDK buy function and expose the buy(amount, maximumPrice)
through the window object. We also expose a connectWallet()
function responsible for connecting the user's wallet and initialize the SDK.
You should also replace the liquidtyPool
with the address of your NFT's liquidity pool.
Conclusion
You can now use the functions in your C# script to directly call your browser's javascript function.
Last updated