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
- Type: Class
- Namespace: UnityEngine
- Assembly: UnityEngine.CoreModule
- Inherits from: Attribute
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)]public class DictionaryDisplayAttribute : Attribute
Remarks
By default, the Inspector draws a serialized field as two columns with the headers Key and Value, and the key column takes up half of the available width. Apply to a dictionary field to change the layout, override one or both header labels, or set the initial key-column width.
Dictionary<TKey, TValue>DictionaryDisplayThis 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]; is treated as 0.5.
NaN
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 , use DictionaryDisplayForTypeAttribute at assembly level instead.
Dictionary<int, Dictionary<string, float>>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 |
|---|---|
| keyColumnFraction | The initial width of the key column, as a fraction of the total dictionary width. Clamped to the range [0.01, 0.99]. |
| keyLabel | The text displayed above the key column. |
| layout | The column layout the Inspector uses to present the dictionary. |
| valueLabel | The text displayed above the value column. |