Documentation

Support

Services

Services

Loot Boxes with Cooldown

Implement loot boxes with cooldown mechanics to limit player earning rates and manage game economy balance.
Read time 3 minutesLast updated 17 hours ago

Whether they're receiving items, currencies, extra lives, or power-ups, players love free stuff! Awarding daily gifts to players that return to your game boosts engagement, intrinsic motivation to check in regularly, and overall interest in your game. Daily rewards can also act as a precursor to introducing monetized in-app purchases in the future. This sample demonstrates how to grant randomized rewards on a timed cooldown. After a player claims their reward, they must wait a preset amount of time before claiming another reward.

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 With Cooldown. 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 With Cooldown, then double-click
    LootBoxesWithCooldownSample.unity
    to open the sample scene.
  2. Press Play to enter Play mode.

Initialization

The
DailyRewardsSceneManager.cs
script performs the following initialization tasks in its
Start
function:
  1. Initializes Unity Gaming Services.
  2. Retrieves and updates the player's currency balances from the Economy service.

Functionality

When you click the Claim Daily Reward button, you receive a random amount of rewards from the available pool (indicated in the currency HUD). For demonstration purposes, the cooldown is set to 60 seconds. The following occurs:
  1. The button's
    OnClick
    method calls Cloud Code to execute the
    GrantTimedRandomReward
    function, which picks random currencies and inventory items from internal lists to award.
  2. The Economy service directly grants the reward and returns the final results to the client.
This example also uses Cloud Code to access Cloud Save to implement a cooldown between rewards and returns:
  • A flag if the Claim Daily Rewards button should be enabled.
  • The current cooldown in seconds.
  • The default cooldown needed to reset the timer locally when a reward is claimed.
Note: This sample also includes enhanced error handling to catch and resolve issues that arise from calling the Economy service too frequently (more than five times per second), which triggers the EconomyException exception with the RateLimited reason. This sample catches the exception, pauses .1 seconds with exponential back-off, and then retries until it succeeds.

Setup

Requirements

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

Package

Role

AuthenticationAutomatically signs in the player as an anonymous user to keep track of their data server-side.
Cloud CodeAccesses the cooldown status, picks and grants random currency and inventory items through the Economy server, and then returns the result of the reward.
Cloud SaveStores and retrieves the last granted reward time to allow cooldown values to persist between sessions.
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 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 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 Dashboard

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

Cloud Code

Publish the following scripts in the Unity Dashboard:

Script

Parameters

Description

Location in project

LootBoxesWithCooldown_Claim
NoneChecks if the cooldown has expired (or was never set), then selects a random reward from the reward pool, grants the reward to the player, and then updates the cooldown timer.
Assets/Use Case Samples/Loot Boxes With Cooldown/Cloud Code/LootBoxesWithCooldown_Claim.js
LootBoxesWithCooldown_GetStatus
NoneChecks for the last grant time and returns a flag indicating if the player is eligible for a reward.
Assets/Use Case Samples/Loot Boxes With Cooldown/Cloud Code/LootBoxesWithCooldown_GetStatus.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 Dashboard:

Resource type

Resource name

ID

Description

CurrencyCoin
COIN
A currency for the randomized loot box reward pool.
CurrencyGem
GEM
A currency for the randomized loot box reward pool.
CurrencyPearl
PEARL
A currency for the randomized loot box reward pool.
CurrencyStar
STAR
A currency for the randomized loot box reward pool.
Inventory itemSword
SWORD
An inventory item for the randomized loot box reward pool.
Inventory itemShield
SHIELD
An inventory item for the randomized loot box reward pool.

Loot Boxes with Cooldown • Services • Unity Docs