Loot boxes

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.

Prerequisites

To use this sample use case, you must download and install the UGS Use Cases project in your Unity project.

Overview

To see this use case in action, open the samples menu and navigate to Loot Boxes. To open this scene directly and interact with the use case:

  1. In the Unity Editor Project window, select Assets > Use Case Samples > Loot Boxes, then double-click LootBoxesSample.unity to open the sample scene.
  2. Press Play to enter Play mode.

Initialization

The LootBoxesSceneManager.cs script performs the following initialization tasks in its Start function:

  1. Initializes Unity Gaming Services.
  2. 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.
  3. Retrieves and updates the player's currency balances from the Economy service.

Functionality

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:

  1. The button's OnClick method calls the GetRandomCurrency script from the Cloud Code service, which picks a random quantity of a random currency from an internal list to reward the user.
  2. Cloud Code calls the Economy service directly to grant the awarded currency and update the player's balance.
  3. Cloud Code returns the results to the client and updates the UI.

Setup

Requirements

To replicate this use case, you'll need the following Unity packages in your project:

PackageRole
AuthenticationAutomatically signs in the player as an anonymous user to keep track of their data server-side.
Cloud CodePicks and grants random currency for the loot box through the Economy server and returns the result.
DeploymentProvides a cohesive interface to deploy assets for Cloud Services.
EconomyRetrieves the starting and updated currency balances at runtime.

To use these services in your game, activate each service for your Organization and project in the Unity Cloud Dashboard.

Unity Cloud services configuration

To replicate this sample scene's setup in your own Unity project, configure the following items:

  • Cloud Code scripts
  • Economy items

To configure these items you can use the Deployment package, or manually enter them using the Unity Cloud Dashboard. The recommended best practice is to use the Deployment package as it greatly accelerates this process.

Using the Deployment package

To deploy configurations using the Deployment package:

  1. Open the Deployment window.
  2. Check in Common and Loot Boxes.
  3. Click Deploy Selection.

This deploys all the necessary items.

Using the Unity Cloud Dashboard

You can use the Unity Cloud Dashboard to manually configure your services by project and environment. Refer to the following sections to configure this sample.

Economy

Configure the following Currencies in the Unity Cloud Dashboard:

Resource typeResource nameIDDescription
CurrencyCoinCOINA loot box reward item.
CurrencyGemGEMA loot box reward item.
CurrencyPearlPEARLA loot box reward item.
CurrencyStarSTARA loot box reward item.

These comprise the potential reward pool for your loot box. For more information, see documentation on configuring Economy resources.

Cloud Code

Publish the following script in the Unity Cloud Dashboard:

ScriptParametersDescriptionLocation in project
GrantRandomCurrencyNonePicks 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/Cloud Code/GrantRandomCurrency.js

Note: The Cloud Code scripts included in the Cloud Code folder are local copies because you cannot view the sample project's dashboard. Changes to these scripts do not affect the behavior of this sample because they are not automatically uploaded to the Cloud Code service.

Economy

Configure the following resources in the Unity Cloud Dashboard:

Resource typeResource nameIDDescription
CurrencyCoinCOINA potential loot box reward.
CurrencyGemGEMA potential loot box reward.
CurrencyStarSTARA potential loot box reward.
CurrencyPearlPEARLA potential loot box reward.