How to make random weight rewards with Loot Boxes in Unity

Unity
Assets
Author image
Johannes Fornaeus5 min read
Cover image

Everybody likes a present. And if you want to give your players assets in-game, then loot boxes are the answer. Use them to reward players through purchases, via in-game triggers or by manually gifting them.

There are lots of different ways to use loot boxes in your games. But with LootLocker it’s really straightforward to get set up and start giving out those all-important rewards.

Before we get into that

Before we begin, make sure you've created your free LootLocker account, and installed and configured the Unity SDK into your game project.

You’ll also need to understand a few other LootLocker concepts before you can get started with loot boxes. Those are:

Assuming you’ve got those under your belt, let’s get going.

Loot box basics

A loot box is a container that rewards a player with a random asset from one or more item groups. Loot boxes can contain multiple groups which means players get random different rewards. But they can only have one asset. Don’t worry – that doesn’t mean you can’t reward players with more than one asset. In fact, it’s easy to do this. Just combine several loot boxes into one big loot box. For example, if you want your loot box to contain five items, you just need to add five loot boxes to the trigger (the thing the player has to do to get a reward). Boom – your player gets multiple assets.

If you have a hierarchy of rewards, for example from common to legendary, you can also configure your loot boxes to guarantee they’ll get a legendary item when they buy a legendary loot box.

Loot boxes in action

Let’s have a look at how to set up a loot box containing one random item chosen from a group, and give it to a player in-game. The good news is that it’s really simple. There are four steps:

  1. Create groups
  2. Add weights to them
  3. Add assets
  4. Reward your player

Easy, right?

Setting it up

Okay, just to be on the safe side, let’s have a look at an example.

First, go to Triggers, which is where you set up the event that will reward the player with the asset. In this example the asset using the loot box complexity is called Standard Chest.

Next, navigate to the loot box asset under Content, then Assets. You’ll see an extra tab called Loot Box Contents (you won’t see this for generic assets). This is where you can change the specific settings for the loot box complexity.

In this example we’ve got three groups set up:

The group is chosen randomly from the three weights. In this example that means players have a 90% chance of getting the warrior helmet, a 10% chance of getting the power bracelet, and 0.1% chance of getting the legendary sword.

Implementing loot boxes in Unity

You can use loot boxes in lots of different ways. For example, you might want them to open automatically when players get them. Or you might want to let players store them and then open them later. You can even give a player a loot box inside a loot box… the list goes on.

Let’s have a look at an example where the player stores the loot box in their inventory. We’re also going to let them see the chance they have of getting different items.

The code

When a player presses the loot box icon, the trigger we configured in the LootLocker dashboard is called (we set this up to reward the player with a standard chest asset in the previous example).

When we open up the inventory, we can see that a loot box has been added. In this example it sits with all the other items, so we’ll need to check it is a loot box first (if you set them up to open automatically, you don’t need to do this).

We now run the InspectALootBoxForAssetInstances function, which returns the different weights of the loot box. We can use these to work out the chance of the loot box returning an item.

When the player then clicks the “Open” button, we run OpenALootBoxForAssetInstances. This will put the item in the player’s inventory. We also want to show the player which item they just got. So we execute GetAssetNotification(), which returns the new assets we haven’t seen before. We then use the information we get from this call to show a new loot box item to the player.

And we’re done.

Need some help?

Hopefully, you’re now an expert on loot boxes. But don’t forget about LootLocker’s other features – they’re all as quick and easy to set up and implement as this one. In case you need some help, we have lots of guides available.

Any questions?

Get in touch on Twitter or Discord. And if you like these guides, please let me know by tweeting me directly – I’d love to hear from you.

Stay up to date.

Join our newsletter and get these posts sent directly to your inbox.