Game ads in Unity to pay for gas fees
Integrate game ads in your web3 game.
Overview#
This sample project showcases the Openfort integration with Unity LevelPlay.
The sample includes:
-
A .NET Core project with Cloud Code C# modules that implement Openfort C# SDK methods. Needs to be hosted in Unity Gaming Services.
-
A Unity sample game that connects to
ugs-backend
through Cloud Code. It uses Openfort Unity SDK to have full compatibility withugs-backend
responses.
Application Workflow#
Prerequisites#
-
Get started with Openfort
-
Get started with UGS
-
Get started with ironSource:
- Sign up on the ironSource website
Set up Openfort dashboard#
-
This sample requires an NFT contract to run. We use 0xbed6a05ce8719bc00db1cc81a814192c82be1bb1 (contract deployed in 80002 Amoy). You can use it for this tutorial too:
-
This sample also requires an ERC20 contract to run. You can deploy a standard one and then add it to the Openfort dashboard following the same logic as above.
-
We aim to cover gas fees for our players when they mint the NFT (if they have watched the ad video). Set a new gas policy for that:
Add a rule so the NFT contract uses this policy:
Add also a rule for the ERC20 contract, as we want to send some ERC20 tokens to the player to be able to test the sample:
-
The players will be charged with 1 in-game ERC20 token when they decide not to watch the ad:
Add a rule so the policy applies to the NFT contract:
-
Enter a name and choose Add account:
This will automatically create a custodial wallet that we'll use to send the ERC20 tokens to the players. IMPORTANT: Transfer a good amount of tokens from the created ERC20 contract to this wallet to facilitate testing.
Set up ironSource#
-
Get Unity Cloud keys#
Before going into ironSource, go to the Unity Cloud dashboard and open Unity Ads Monetization using Shortcuts:
-
Create a LevelPlay Service Account
Now go to the API management section and choose Create LevelPlay Service Account:
Copy and save the Key ID and the Secret key somewhere safe and choose Done:
-
Get Monetization Stats API Access
Copy and save the API Key. Choose Create API Key if it's not already there:
-
Get Organization Core ID
Copy and save the Organization Core ID:
-
-
Create an ironSource LevelPlay app#
Go to the ironSource dashboard and under the LevelPlay section, choose Add app and enter your app details:
Select the following settings and choose Add app:
Activate Rewarded Video as an ad unit and choose Continue:
-
Set up SDK Networks#
In the Available Networks panel select Unity Ads:
Enable the Unity bidder auto-setup option, add all the credentials from the Get Unity Cloud keys section and choose Save:
Unity Ads will have appeared as a new available network. Choose Setup:
Because we enabled the Unity bidder auto-setup option, now you can choose Add bidder:
The needed information from Unity will be automatically retrieved. Choose Save:
Set up Unity Cloud#
Thanks to the Unity bidder auto-setup option, a new project has been automatically created in the Unity Cloud dashboard. Now your LevelPlay Service Account needs to have some admin roles over this newly created project. Go to Administration --> Service accounts and choose your account:
Scroll down and choose Manage project roles:
Select your project and choose Next:
In the Admin dropdown select:
- Player Resource Policy Editor
- Project Resource Policy Editor
- Unity Environments Admin
In the LiveOps dropdown select:
- Cloud Code Script Publisher
- Triggers Configuration Editor
- Leaderboards Admin
- Cloud Code Editor
Choose Save:
Set up ugs-backend
#
-
Set Openfort dashboard variables#
Open the solution with your preferred IDE, open
SingletonModule.cs
and fill in these variables:OfApiKey
: Retrieve the Openfort secret keyOfNftContract
: Retrieve the NFT contract API IDOfGoldContract
: Retrieve the ERC20 contract API IDOfFullSponsorPolicy
: Retrieve the Full Sponsor Policy API IDOfChargeErc20Policy
: Retrieve the Fixed Charge Policy API IDOfDevAccount
: Retrieve the Developer Account API ID
-
Package Code#
Follow the official documentation steps.
-
Deploy to UGS#
Follow the official documentation steps.
Set up unity-client
#
In Unity go to Edit --> Project Settings --> Services and link the unity-client
to your UGS Project:
Select your Environment:
Under Assets --> Scripts --> Controllers open the AdsController.cs:
Fill the appKey
variable with the ironSource LevelPlay app key and save the script:
Build to Android#
In Unity go to Android Player settings and make sure Other Settings looks like this:
Also, make sure to sign the application with a Keystore in Publishing Settings:
Return to Build Settings and choose Build:
Send and run the .apk on your device.
Conclusion#
Upon completing the above steps, your Unity game will be fully integrated with Openfort and Unity LevelPlay. Always remember to test every feature before deploying to guarantee a flawless player experience.
For a deeper understanding of the underlying processes, check out the tutorial video.
Get support#
If you found a bug or want to suggest a new [feature/use case/sample], please file an issue.
If you have questions, or comments, or need help with code, we're here to help:
- on Twitter at https://twitter.com/openfortxyz
- on Discord: https://discord.com/invite/t7x7hwkJF4
- by email: support+youtube@openfort.xyz