Version: Unity 6.6 Alpha (6000.6)
LanguageEnglish
  • C#

DictionaryHeaderAttribute

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

Customizes the column header labels and the initial key-column width of a serialized Dictionary<TKey, TValue> field in the Inspector.

By default, a serialized Dictionary<TKey, TValue> field is drawn with the column headers Key and Value, and the key column takes up half of the available width. Apply DictionaryHeader to override one or both header labels, and to set the initial key-column width as a fraction of the total dictionary 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.

Set keyColumnLabel for the text displayed above the key column, valueColumnLabel for the value column, and keyColumnFraction for the initial key-column width as a fraction of the total dictionary width. All three properties are optional.

If keyColumnLabel or valueColumnLabel is empty, Unity falls back to the default label (Key or Value) for that column. keyColumnFraction is clamped to the range [0.01, 0.99]; NaN is treated as 0.5.

keyColumnFraction only sets the initial ratio. After the user drags the column divider in the Inspector, Unity persists their preferred width and ignores the value supplied by the attribute on subsequent draws.

using System.Collections.Generic;
using UnityEngine;

public class Example : MonoBehaviour { // Replace the default "Key" and "Value" column headers. [DictionaryHeader(keyColumnLabel = "Name", valueColumnLabel = "Score")] [SerializeField] public Dictionary<string, int> playerScores;

// Replace the headers and start with a narrower key column (30% of the dictionary width). [DictionaryHeader(keyColumnLabel = "Item", valueColumnLabel = "Quantity", keyColumnFraction = 0.3f)] [SerializeField] public Dictionary<string, int> inventory;

// Only override the key column header; the value column keeps the default "Value" label. [DictionaryHeader(keyColumnLabel = "Slot")] [SerializeField] public Dictionary<int, GameObject> equipmentSlots; }

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].
keyColumnLabelThe text displayed above the key column.
valueColumnLabelThe text displayed above the value column.