Random Selector – Spawn and Loot randomizer Unity Asset

Random Selector – Spawn and Loot randomizer Screenshot

Overview

Random Selector is a utility package which provides an efficient, weight-based, random selector. The selector can be used to randomly spawn enemies, generate loot, etc. while being extremely efficient and fast.

Features

Random Selector includes the following features:

  • Weighted randomness, based on .Net’s Random class, Unity’s Random class, a thread-safe .Net Random variant, or any user-provided random number generator (such as a web-based generator)
  • Supports multiple usage scenarios, including:
    • Editor-only
    • Code-only
    • Multi-level selections
  • Fully documented code base in C#

Purchase the Random Selector Unity Asset from here >>

Screenshots

Documentation

Code base

The main class of the package is “RandomSelectorScript”. In provides a simple mechanism to select random items from a provided list of items. The class can optionally be provided with an “IRandom” implementation, for greater control. Internally, the class implements the Alias Method algorithm – this results in an O(N2) one-time preparation, and then on O(1) complexity for random items selection.

The package provides three implementations for the IRandom interface:

  • SystemRandom – IRandom implementation for the System.Random class
  • ThreadSafeRandom – IRandom implementation for a thread-safe, thread-random, System.Random class
  • UnityEngineRandom – IRandom implementation for the UnityEngine.Random class

Using the RandomSelectorScript

The RandomSelectorScript can be used in multiple ways.

Unity Editor Only

As can be seen in the sample SpawnItemScript script and the sample EditorSample scene, the package can be used in an Editor-Only mode. In this mode, a MonoBehaviour implementation that contains an instance of RandomSelectorScript is used. The list of items, and their probabilities is filled from the Unity Editor.

A Spawn method can be used to select a random item, and a Unity Event can be used to respond to the item selection, as seen in the SetSelectedItemTextScript script.

Code Only

As an alternative, a code-only mode can be used to have more control, as seen in the sample SpawnItemByCodeScript script and the sample CodeSample scene. In this mode, an instance of RandomSelectorScript is created and populated with items via code. A Spawn method can again be used to select a random item and respond to the selection.

Multi-Level selection

For a multi-level random selection can be achieved by nesting multiple RandomSelectorScript levels. As seen in the sample GetLootScript script and the sample LootSample scene, a RandomSelectorScript instance is used to select sub-RandomSelectorScript instances. In the sample, we used the main RandomSelectorScript instance for the rarity of the loot cards (common, rare or legendary) with different probabilities. Once the rarity is selected, a sub-RandomSelectorScript instance is used to select specific cards with different probabilities (e.g., Swordman, Spearman, etc.).

Subscribe now

and receive the latest updates and news.