Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


DictionaryDisplayAttribute

Configures how the Inspector presents a serialized Dictionary field: its column layout, the key and value header labels, and the initial key-column width.
Read time 5 minutesLast updated 5 days ago

Definition

[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)]public class DictionaryDisplayAttribute : Attribute

Remarks

By default, the Inspector draws a serialized
Dictionary<TKey, TValue>
field as two columns with the headers Key and Value, and the key column takes up half of the available width. Apply
DictionaryDisplay
to a dictionary field to change the layout, override one or both header labels, or set the initial key-column width.
This attribute targets fields. It only takes effect when the field is a serialized dictionary that the built-in dictionary drawer renders in the Inspector.
Every setting is an optional named property:
  • layout selects the column layout. It defaults to DictionaryLayout.TwoColumns.
  • keyLabel sets the text above the key column. If empty, Unity uses the default label Key.
  • valueLabel sets the text above the value column. If empty, Unity uses the default label Value.
  • keyColumnFraction sets the initial key-column width as a fraction of the total dictionary width. It's clamped to the range [0.01, 0.99];
    NaN
    is treated as 0.5.
The layout, labels, and key-column fraction only set the initial presentation. After the user changes the layout from the header context menu or drags the column divider, Unity persists their choice and ignores the attribute on subsequent draws. To return to the attribute's values, the user selects Reset to Defaults from the header context menu.
To configure a dictionary that has no field to decorate, such as the inner dictionary of a
Dictionary<int, Dictionary<string, float>>
, use DictionaryDisplayForTypeAttribute at assembly level instead.

Examples

using System.Collections.Generic;using UnityEngine;public class LootTable : MonoBehaviour{ // Replace the default "Key" and "Value" column headers, and start with a wider key column. [DictionaryDisplay(keyLabel = "Item", valueLabel = "Drop chance", keyColumnFraction = 0.6f)] [SerializeField] private Dictionary<string, float> drops = new Dictionary<string, float>(); // Stack each value under its key in a single column, and keep every value visible. [DictionaryDisplay(layout = DictionaryLayout.OneColumnWithValueVisible, keyLabel = "Name", valueLabel = "Item")] [SerializeField] private Dictionary<int, GameObject> rewards = new Dictionary<int, GameObject>();}

Properties

Property

Description

keyColumnFractionThe initial width of the key column, as a fraction of the total dictionary width. Clamped to the range [0.01, 0.99].
keyLabelThe text displayed above the key column.
layoutThe column layout the Inspector uses to present the dictionary.
valueLabelThe text displayed above the value column.

Inheritance

Inherited Members