Skip to main content

Class TypeReferencePropertyDrawer

Namespace: RealityCollective.ServiceFramework.Editor.PropertyDrawers

Custom property drawer for SystemType properties.

public class TypeReferencePropertyDrawer : PropertyDrawer

Inheritance

PropertyDrawer ← TypeReferencePropertyDrawer

Fields

TypeReferenceUpdated

public const string TypeReferenceUpdated = "TypeReferenceUpdated"

Field Value

string

Properties

CreateNewTypeOverride

Gets or sets a Type to be created using the "Create new ..." menu.

public static Type CreateNewTypeOverride { get; set; }

Property Value

Type

Remarks

This property must be set immediately before presenting a class type reference property field using EditorGUI.PropertyField(Rect,SerializedProperty) or EditorGUILayout.PropertyField(SerializedProperty,UnityEngine.GUILayoutOption[]) since the value of this property is reset to null each time the control is drawn.

ExcludedTypeCollectionGetter

Gets or sets a function that returns a collection of types that are to be excluded from drop-down. A value of null specifies that no types are to be excluded.

public static Func<ICollection<Type>> ExcludedTypeCollectionGetter { get; set; }

Property Value

Func<ICollection<Type>>

Examples

Exclude a specific type from being selected:

private SerializedProperty someTypeReferenceProperty;

public override void OnInspectorGUI()
{
serializedObject.Update();

ClassTypeReferencePropertyDrawer.ExcludedTypeCollectionGetter = GetExcludedTypeCollection;
EditorGUILayout.PropertyField(someTypeReferenceProperty);

serializedObject.ApplyModifiedProperties();
}

private ICollection<Type> GetExcludedTypeCollection()
{
var set = new HashSet<Type>();
set.Add(typeof(SpecialClassToHideInDropdown));
return set;
}

Remarks

This property must be set immediately before presenting a class type reference property field using EditorGUI.PropertyField(Rect,SerializedProperty) or EditorGUILayout.PropertyField(SerializedProperty,UnityEngine.GUILayoutOption[]) since the value of this property is reset to null each time the control is drawn. Since filtering makes extensive use of Contains it is recommended to use a collection that is optimized for fast look ups such as HashSet%601 for better performance.

FilterConstraintOverride

Gets or sets a function that returns if the constraint has been satisfied.

public static Func<Type, bool> FilterConstraintOverride { get; set; }

Property Value

Func<Type, bool>

Examples

Override the constraint for the property:

private SerializedProperty someTypeReferenceProperty;

public override void OnInspectorGUI()
{
serializedObject.Update();

ClassTypeReferencePropertyDrawer.FilterConstraintOverride = IsConstraintSatisfied;
EditorGUILayout.PropertyField(someTypeReferenceProperty);

serializedObject.ApplyModifiedProperties();
}

private bool IsConstraintSatisfied(Type type)
{
return !type.IsAbstract && type.GetInterfaces().Any(interfaceType => interfaceType == ServiceConstraint);
}

Remarks

This will override any attribute filter already set. This property must be set immediately before presenting a class type reference property field using EditorGUI.PropertyField(Rect,SerializedProperty) or EditorGUILayout.PropertyField(SerializedProperty,UnityEngine.GUILayoutOption[]) since the value of this property is reset to null each time the control is drawn.

GroupingOverride

This will override any grouping drawn for the drop-down. This property must be set immediately before presenting a class type reference property field using EditorGUI.PropertyField(Rect,SerializedProperty) or EditorGUILayout.PropertyField(SerializedProperty,UnityEngine.GUILayoutOption[]) since the value of this property is reset to null each time the control is drawn.

public static TypeGrouping? GroupingOverride { get; set; }

Property Value

TypeGrouping?

SelectedType

The currently selected Type in the dropdown menu.

public static Type SelectedType { get; }

Property Value

Type

Methods

DisplayDropDown(Rect, IEnumerable<Type>, Type, TypeGrouping, Type)

Displays the type picker dropdown.

public static void DisplayDropDown(Rect position, IEnumerable<Type> types, Type selectedType, TypeGrouping grouping, Type newType = null)

Parameters

position Rect

types IEnumerable<Type>

selectedType Type

grouping TypeGrouping

newType Type

GetPropertyHeight(SerializedProperty, GUIContent)

public override float GetPropertyHeight(SerializedProperty property, GUIContent label)

Parameters

property SerializedProperty

label GUIContent

Returns

float

OnGUI(Rect, SerializedProperty, GUIContent)

public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)

Parameters

position Rect

property SerializedProperty

label GUIContent