Loot boxes with cooldown

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.

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 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.
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.

Dashboard setup

To replicate this sample scene's setup in the Unity Cloud Dashboard, you must:

  • Publish two scripts in Cloud Code.
  • Create four Currencies and two Inventory Items for the Economy service.

Cloud Code

Publish the following scripts in the Unity Cloud Dashboard:

ScriptParametersDescriptionLocation in project
LootBoxesWithCooldown_ClaimNone

Checks 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_GetStatusNoneChecks 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 Cloud Dashboard:

Resource typeResource nameIDDescription
CurrencyCoinCOINA currency for the randomized loot box reward pool.
CurrencyGemGEMA currency for the randomized loot box reward pool.
CurrencyPearlPEARLA currency for the randomized loot box reward pool.
CurrencyStarSTARA currency for the randomized loot box reward pool.
Inventory itemSwordSWORDAn inventory item for the randomized loot box reward pool.
Inventory itemShieldSHIELDAn inventory item for the randomized loot box reward pool.