Skip to content

Collection Dropdown Attribute

Toto Christensen edited this page Nov 12, 2024 · 13 revisions

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

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")]
public FieldType fieldName;

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")]
public 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.

Note: Example image to be added here soon.

Example 2: Using with a Parameterless Method

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

[CollectionDropdown("GetSpawnPoints")]
public GameObject spawnPoint;

private List<GameObject> GetSpawnPoints()
{
    // Generate or retrieve a list of spawn points
    return new List<GameObject> { spawnPoint1, spawnPoint2, spawnPoint3 };
}

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

Note: Example image to be added here soon.

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)]
public 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.

Note: Example image to be added here soon.

Clone this wiki locally