Loot boxes are virtual items that players can win, earn, or purchase, and then open to receive a randomized selection of items. Rewards can vary greatly depending on the game's genre, theme, and virtual economy. They can positively impact retention, supplement live events, and pique the curiosity of old and new players alike.
This sample demonstrates how to set up a basic loot box that grants random currency to players.
To see this use case in action:
- In the Unity Editor Project window, select Assets > Use Case Samples > Loot Boxes, and then double-click
LootBoxesSample.unity
to open the sample scene. - Enter Play Mode to interact with the use case.
The LootBoxesSceneManager.cs
script performs the following initialization tasks:
- Initializes Unity Gaming Services.
- Signs in the player anonymously using the Authentication service. If you’ve previously initialized any of the other sample scenes, Authentication will use your cached Player ID instead of creating a new one.
- Retrieves and updates currency balances from the Economy service for that authenticated user.
When you click the Open Loot Box button, you receive a random amount of rewards from the available pool (indicated in the currency HUD). The following occurs on the backend:
- The button's
OnClick
method calls theGetRandomCurrency
script from the Cloud Code service, which picks a random quantity of a random currency from an internal list to reward the user. - Cloud Code calls the Economy service directly to grant the awarded currency and update the player's balance.
- Cloud Code returns the results to the client and updates the UI.
To replicate this use case, you need the following Unity packages in your project:
Package | Role |
---|---|
Authentication | Automatically signs in the player as an anonymous user to keep track of their data server-side. |
Cloud Code | Picks and grants random currency for the loot box through the Economy server and returns the result. |
Economy | Retrieves the starting and updated currency balances at runtime. |
Deployment | The Deployment package provides a cohesive interface to deploy assets for Cloud Services. |
To use these services in your game, activate each service for your Organization and project in the Unity Dashboard.
To replicate this sample scene's setup in your own Unity project, we need to configure the following items:
- Cloud Code scripts
- Economy items
There are two main ways of doing this, either by using the Deployment package, or by manually entering them using the Dashboard. We recommend the usage of the Deployment package since it will greatly accelerate this process.
Here are the steps to deploy configuration using the Deployment package:
- Open the Deployment window
- Check in
Common
andLoot Boxes
- Click
Deploy Selection
This will deploy all the necessary items.
The Dashboard enables you to edit manually all your services configuration by project and environment. Here are the details necessary for the configuration of the current sample.
Publish the following script in the LiveOps dashboard:
Script | Parameters | Description | Location in project |
---|---|---|---|
LootBoxes_GrantRandomCurrency |
None | Picks a random quantity of a random currency from an internal list to reward the user, grants it on the Economy service, and returns the result. | Assets/Use Case Samples/Loot Boxes/Config as Code/LootBoxes_GrantRandomCurrency.js |
Configure the following resources in the LiveOps dashboard:
Resource type | Resource name | ID | Description |
---|---|---|---|
Currency | Coin | COIN |
A potential loot box reward. |
Currency | Gem | GEM |
A potential loot box reward. |
Currency | Pearl | PEARL |
A potential loot box reward. |
Currency | Star | STAR |
A potential loot box reward. |