Develop a Lightning Game with Unity BTCPay SDK — Part 2(set up Unity Project)
For this tutorial, we are going to use a game project asset from Unity Asset Store, called “Survival Shooter” and this article is inspired by this Unity’s live tutorial “Creating an In-Game Shop”, where it adds an In-Game Shop UI to Survival Shooter game.
So, Our base project is “Survival Shooter” project, and with this Unity tutorial for In-Game Store, you create in-Game Store UI. But, this Unity tutorial itself is 1 hour long, and if you actually do this tutorial, It would take 2 hours or more, depending on your skill level.
So, We have prepared the version of Survival Shooter which comes with In-game shop UI already, so that you only need to integrate it with the Unity BTCPay SDK. Sounds good?
Install testnet Lightning wallet for testing the lightning game
If your phone is android, Download eclair testnet wallet. And get some fund from faucet. https://faucet.lightning.community/
If your phone is iphone, Check this link for ios lightning Wallet, which does not require a backend lightning daemon. SwiftLightning may work.
Download In-Game Shop version of Survival Shooter
Now, let’s get the ball rolling!
<Click to download Unity Project>
Download the zipped file containing the initial Unity project and Open from the latest Unity. Check if it works by clicking the play button. It should work both on Windows and on MacOS.
How to test this initial Unity project is shown the video below.
Once you have open the project, download the Unity BTCpay SDK package from our Github release page. It contains the SDK’s dll and dependencies and C# sample codes, sample game objects and etc.
Check how to import it by the video below.
Now, it’s coding time! Here is the steps we will take on Unity and Code editor. (In this video, I use Visual Studio 2017 as Csharp code editor. You may use MonoDevelop.)
- Create LightningManager as empty Game Object and put csharp code on it.
- Add Canvas and Panel for lightning QR code image, which will popup when weapon button on Shop UI is clicked.
- Add close button to cancel Lightning payment
- On BTCPay server, create a pairing code (please check the post Part1)and set it on inspector of LightningManager
Create LightningManager
We are going to create an empty Game object where we put the most of logic. We call it lightningManager, which has requirements below.
- Instantiate the object of BTCPayClient class from SDK, which is main class
- Get Lightning BOLT invoice string using SDK and from BTCPay server
- Generate QR code image out of the invoice string using SDK
- Show QR code for the player to scan from LN wallet
- Subscribe to the invoice payment event and pass callback
- Callback to handle Invoice passed when payment is done
Check this video which covers above.
Once the LightningManger code and QR code UI are set. Almost done.
Last tutorial video is below. In this video, wrap up by setting Gameobject references and test the game.
Hope you have successfully run the game and buy a weapon with the lightning wallet.
Please comment below if you have faced any issue or suggestion for improvement.