Collection Dropdown

Overview

The [CollectionDropdown] attribute allows you to display a dropdown selection in the Unity Inspector for a non-collection field, with options populated from a collection (such as an array or list) or a method that returns a collection. This attribute helps select from predefined options in the Inspector, making field assignment more organized and user-friendly.

Capabilities

The [CollectionDropdown] attribute supports the following capabilities:

  • Supported Field Types: Can be applied to fields of types such as string, int, and other primitive types, as well as custom serializable classes.
  • Supported Collections: The dropdown options can be populated from any collection that implements IEnumerable, such as arrays, lists, or even results from methods returning IEnumerable.
  • Method Support: You can use parameterless methods or methods with parameters to dynamically generate the collection for the dropdown options.
  • Dynamic Parameter Passing: For methods with parameters, [CollectionDropdown] allows you to specify values for each parameter, providing flexibility in generating collections based on input values.

Syntax and Examples

Note: These examples use Unity’s [SerializeField] on private fields to promote encapsulation. This approach can help protect internal data from unintended access by other scripts and maintain clean, organized code. For more details, see Unity’s documentation on SerializeField.

Applying [CollectionDropdown] is straightforward. You specify the name of a collection field or method as a string in the attribute. Here’s a breakdown of the different ways to use it.

Basic Syntax

[CollectionDropdown("collectionOrMethodName")]
[SerializeField]
private FieldType fieldName;

Additional Constructor Syntax

// Using a method with parameters
[CollectionDropdown("memberName", param1, param2, ...)]

Example 1: Using with a Field Reference

In this example, selectedColor is a string field that allows you to select from a predefined array of color names.

[CollectionDropdown("colors")]
[SerializeField]
private string selectedColor;

private string[] colors = { "Red", "Blue", "Green", "Yellow", "Purple", "Orange" };

Result: In the Inspector, selectedColor will display a dropdown with the values from colors, allowing easy selection from the predefined list.

Gif of Example 1 result

Example 2: Using with a Parameterless Method

Here, spawnPoint is a Vector3 field that allows selection from dynamically generated spawn points by calling a parameterless method GetSpawnPoints.

[CollectionDropdown("GetSpawnPoints")]
[SerializeField]
private Vector3 spawnPoint;

private List<Vector3> GetSpawnPoints() 
{
    // Generate or retrieve a list of spawn points
    return new List<Vector3> { new Vector3(0, 4, 2), new Vector3(5, 2, 27), new Vector3(8, 2, 5) };
}

Result: The dropdown for spawnPoint in the Inspector will display the items returned by GetSpawnPoints.

Gif of Example 2 result

Example 3: Using with a Method with Parameters

In cases where you want to generate options based on specific parameters, [CollectionDropdown] can also work with methods that accept arguments. Here, selectedLevel allows selecting a level from a generated range of integers.

[CollectionDropdown("GetLevelRange", 1, 10)]
[SerializeField]
private int selectedLevel;

private int[] GetLevelRange(int start, int end)
{
    // Generate a range of levels
    return Enumerable.Range(start, end - start + 1).ToArray();
}

Result: In the Inspector, selectedLevel will show a dropdown with integers from 1 to 10, as specified in the [CollectionDropdown] attribute.

Gif of Example 3 result