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; }
| 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]. |
| keyColumnLabel | The text displayed above the key column. |
| valueColumnLabel | The text displayed above the value column. |