Version: Unity 6.5 (6000.5)
LanguageEnglish
  • C#

PropertyAttribute.applyToCollection

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

public bool applyToCollection;

Description

Makes attribute affect collections instead of their items.

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.
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 } }; } }