DictionaryDisplayForTypeAttribute
Configures how the Inspector presents a serialized Dictionary identified by type, for dictionaries that have no field to decorate.
阅读时间6 分钟最后更新于 5 天前
Definition
- Type: Class
- Namespace: UnityEngine
- Assembly: UnityEngine.CoreModule
- Inherits from: DictionaryDisplayAttribute
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)]public sealed class DictionaryDisplayForTypeAttribute : DictionaryDisplayAttribute
Remarks
DictionaryDisplayForTypetypeofThis attribute targets assemblies, so you apply it with an assembly-level attribute declaration. Use it to:
- Configure a dictionary that has no field to attach a field attribute to, such as the inner dictionary of a .
Dictionary<int, Dictionary<string, StatModifier>> - Apply one presentation to every field of the same dictionary type from a single declaration, instead of repeating DictionaryDisplayAttribute on each field.
The targetType must be a fully specified , such as . The attribute applies to every dictionary of that exact type, wherever it appears, and sets the layout, labels, and key-column fraction. Unity ignores a target that isn't a fully specified dictionary type.
Dictionary<TKey, TValue>Dictionary<string, StatModifier>The dictionary's key type or value type must use a type defined in the same assembly as the attribute. Only one of the two needs to. The type can be the key or value type itself, or appear inside it as an array element type or a generic type argument. For example, if your assembly defines the struct, the attribute can target the , , , or type. It can't target the type, because that type uses no type from your assembly. This requirement prevents one assembly from changing the display of dictionaries that use only types from other assemblies. If the target uses no type from the attribute's assembly, Unity ignores the attribute and logs a warning in the console.
StatModifierDictionary<string, StatModifier>Dictionary<StatModifier, int>Dictionary<int, StatModifier[]>Dictionary<int, List<StatModifier>>Dictionary<int, int>When more than one source can set a dictionary's presentation, Unity uses the first source that applies, from highest precedence to lowest:
- The user's own choices: the layout from the header context menu and the column width from dragging the divider. These choices persist and override every attribute until the user selects Reset to Defaults.
- A field-level DictionaryDisplayAttribute on the dictionary field sets the layout, labels, and width.
- An assembly-level on the exact
DictionaryDisplayForTypesets the layout, labels, and width.Dictionary<TKey, TValue> - The built-in defaults: DictionaryLayout.TwoColumns layout, Key and Value labels, and a 0.5 key-column fraction.
Examples
using System.Collections.Generic;using UnityEngine;// One declaration configures every Dictionary<string, StatModifier> in the assembly: the nested// dictionary in Equipment and the field in Buff, without repeating [DictionaryDisplay]. This// assembly defines StatModifier, so the attribute is allowed to target this dictionary.[assembly: DictionaryDisplayForType(typeof(Dictionary<string, StatModifier>), layout = DictionaryLayout.OneColumnWithValueVisible, keyLabel = "Stat", valueLabel = "Modifier", keyColumnFraction = 0.4f)][System.Serializable]public struct StatModifier{ public float amount; public bool isPercentage;}public class Equipment : MonoBehaviour{ // Nested inner dictionary: a value, so it has no field to decorate. [SerializeField] private Dictionary<int, Dictionary<string, StatModifier>> modifiersPerSlot = new();}public class Buff : MonoBehaviour{ // A direct field of the same type, styled by the same declaration. [SerializeField] private Dictionary<string, StatModifier> statModifiers = new();}
Constructors
Constructor | Description |
|---|---|
| DictionaryDisplayForTypeAttribute(System.Type) | Creates an attribute that configures the dictionary type identified by |
Properties
Property | Description |
|---|---|
| targetType | The dictionary type this attribute configures. Must be a fully specified |