Version: Unity 6.7 Alpha (6000.7)
LanguageEnglish
  • C#

DictionaryDisplayAttribute

class in UnityEngine

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

Configures how the Inspector presents a serialized Dictionary<TKey, TValue> field: its column layout, the key and value header labels, and the initial key-column width.

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>> or a third-party dictionary type, use DictionaryDisplayForTypeAttribute at assembly level instead.

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.