Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


applyToCollection

Makes attribute affect collections instead of their items.
Read time 1 minuteLast updated 5 days ago

Definition

  • Type: Property
  • Namespace: UnityEngine
  • Assembly: UnityEngine.CoreModule
public bool applyToCollection { get; }

Remarks

When you apply a
PropertyAttribute
to a collection, by default Unity applies it to each element of the collection. When
applyToCollection
is
true
, the attribute is applied to the collection itself instead.
You can use this in scenarios where the attribute functionality needs to apply to the collection as a whole rather than per element. Some typical scenarios include:
  • A reorderable list header or collection-level label. For example, when creating a drawer that adds a summary line stating the total number of items in the list, or a custom add or remove toolbar.
  • An attribute that makes a list non-reorderable, or locks its size.
  • Validation across the collection. For example, an attribute that checks that a list contains unique entries or that it has at least three items.

Examples

using UnityEditor;using UnityEditor.UIElements;using UnityEngine;using UnityEngine.UIElements;public class Collection : MonoBehaviour{ public int before; [GreenCollectionDrawer] public int[] collection; public int after;}public class GreenCollectionDrawerAttribute : PropertyAttribute{ public GreenCollectionDrawerAttribute() : base(true) { }}[CustomPropertyDrawer(typeof(GreenCollectionDrawerAttribute))]public class GreenCollectionDrawer : PropertyDrawer{ public override VisualElement CreatePropertyGUI(SerializedProperty property) { return new PropertyField(property) { style = { backgroundColor = Color.green } }; }}