Version 2.1.0.0

Odin has a dedicated attribute overview with examples

ValueDropdownAttribute class

Namespace: Sirenix.OdinInspector
Assembly: Sirenix.OdinInspector.Attributes
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Module | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Interface | AttributeTargets.Parameter | AttributeTargets.Delegate | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter | AttributeTargets.All, AllowMultiple = false, Inherited = true)]
public class ValueDropdownAttribute : Attribute, _Attribute

ValueDropdown is used on any property and creates a dropdown with configurable options.

Use this to give the user a specific set of options to select from.

Inheritance
  • System.Object
  • System.Attribute
  • ValueDropdownAttribute
Remarks

note

Due to a bug in Unity, enums will sometimes not work correctly. The last example shows how this can be fixed.

Example

The following example shows a how the ValueDropdown can be used on an int property.

public class MyComponent : MonoBehaviour
{
	[ValueDropdown("myValues")]
	public int MyInt;

	// The selectable values for the dropdown.
	private int[] myValues = { 1, 2, 3 };
}
Example

The following example shows how ValueDropdownList can be used for objects, that do not implement a usable ToString.

public class MyComponent : MonoBehaviour
{
	[ValueDropdown("myVectorValues")]
	public Vector3 MyVector;

	// The selectable values for the dropdown, with custom names.
	private ValueDropdownList<Vector3> myVectorValues = new ValueDropdownList<Vector3>()
	{
		{"Forward",	Vector3.forward	},
		{"Back",	Vector3.back	},
		{"Up",		Vector3.up		},
		{"Down",	Vector3.down	},
		{"Right",	Vector3.right	},
		{"Left",	Vector3.left	},
	};
}
Example

The following example shows how the ValueDropdown can on any member that implements IList.

public class MyComponent : MonoBehaviour
{
	// Member field of type float[].
	private float[] valuesField;

	[ValueDropdown("valuesField")]
	public float MyFloat;

	// Member property of type List<thing>.
	private List<string> ValuesProperty { get; set; }

	[ValueDropdown("ValuesProperty")]
	public string MyString;

	// Member function that returns an object of type IList.
	private IList<ValueDropdownItem<int>> ValuesFunction()
	{
		return new ValueDropdownList<int>
		{
			{ "The first option",	1 },
			{ "The second option",	2 },
			{ "The third option",	3 },
		};
	}

	[ValueDropdown("ValuesFunction")]
	public int MyInt;
}
Example

Due to a bug in Unity, enums member arrays will in some cases appear as empty. This example shows how you can get around that.

public class MyComponent : MonoBehaviour
{
	// Make the field static.
	private static MyEnum[] MyStaticEnumArray = MyEnum[] { ... };

	// Force Unity to serialize the field, and hide the property from the inspector.
	[SerializeField, HideInInspector]
	private MyEnum MySerializedEnumArray = MyEnum[] { ... };
}
See Also

Constructors

ValueDropdownAttribute(String)
Creates a dropdown menu for a property.
public ValueDropdownAttribute(string memberName)
Parameters
System.String memberName

Name of any field, property or method member that implements IList. E.g. arrays or Lists.

Fields

AppendNextDrawer
If true, instead of replacing the drawer with a wide dropdown-field, the dropdown button will be a little button, drawn next to the other drawer.
public bool AppendNextDrawer
DisableGUIInAppendedDrawer
Disables the the GUI for the appended drawer. False by default.
public bool DisableGUIInAppendedDrawer
DisableListAddButtonBehaviour
False by default.
public bool DisableListAddButtonBehaviour
DoubleClickToConfirm
By default, a single click selects and confirms the selection.
public bool DoubleClickToConfirm
DrawDropdownForListElements
True by default. If the ValueDropdown attribute is applied to a list, then disabling this, will render all child elements normally without using the ValueDropdown. The ValueDropdown will still show up when you click the add button on the list drawer, unless DisableListAddButtonBehaviour is true.
public bool DrawDropdownForListElements
DropdownHeight
Gets or sets the height of the dropdown. Default is zero.
public int DropdownHeight
DropdownTitle
Gets or sets the title for the dropdown. Null by default.
public string DropdownTitle
DropdownWidth
Gets or sets the width of the dropdown. Default is zero.
public int DropdownWidth
ExcludeExistingValuesInList
If the ValueDropdown attribute is applied to a list, and IsUniqueList is set to true, then enabling this, will exclude existing values, instead of rendering a checkbox indicating whether the item is already included or not.
public bool ExcludeExistingValuesInList
ExpandAllMenuItems
If the dropdown renders a tree-view, then setting this to true will ensure everything is expanded by default.
public bool ExpandAllMenuItems
FlattenTreeView
By default, the dropdown will create a tree view.
public bool FlattenTreeView
HideChildProperties
Whether to draw all child properties in a foldout.
public bool HideChildProperties
IsUniqueList
False by default.
public bool IsUniqueList
MemberName
Name of any field, property or method member that implements IList. E.g. arrays or Lists.
public string MemberName
NumberOfItemsBeforeEnablingSearch
The number of items before enabling search. Default is 10.
public int NumberOfItemsBeforeEnablingSearch
SortDropdownItems
False by default.
public bool SortDropdownItems

This website is brand new and built with the Odin 2.1 beta in mind. This means you might discover some features such as attribute expressions that are only available in the beta.

You can download the 2.1 beta from here and read the patch-notes here.