Skip to content

Virtual Methods

OnInitialize

Description

Called once when initializing PropertyValidator.

Very useful for make some initializations of properties and get view attribute.

void OnInitialize(SerializedProperty property, ValidatorAttribute validatorAttribute, GUIContent label)
{
    // Some initialization...
}

Parameters

Parameter Description
property Serialized property with ValidatorAttribute.
validatorAttribute ValidatorAttribute of serialized property.
label Label of serialized property.

Example

[ValidatorTarget(typeof(ExampleAttribute))]
public class ExampleValidator : PropertyValidator
{
    private ExampleAttribute exampleAttribute;

    // Called once when initializing PropertyValidator.
    public override void OnInitialize(SerializedProperty property, ValidatorAttribute validatorAttribute, GUIContent label)
    {
        // Getting ExampleAttribute attribute from field.
        exampleAttribute = validatorAttribute as ExampleAttribute;

        // Some other initializations here...
    }
}

Validate

Description

Called before drawing property.

void Validate(SerializedProperty property)
{
    // Property validation here...
}

Parameters

Parameter Description
property Serialized property with ValidatorAttribute.

Example

[ValidatorTarget(typeof(ExampleAttribute))]
public class ExampleValidator : PropertyValidator
{
    // Called before drawing property.
    public override void Validate(SerializedProperty property)
    {
        if(property.floatValue < 0)
        {
            property.floatValue = 0;
        }
    }
}

ModifyPropertyPosition

Description

Called before OnValidatorGUI() for modify property position.

void ModifyPropertyPosition(Rect originalPosition, ref Rect modifiedPosition)
{
    // Modify property position here.
}

Parameters

Parameter Description
originalPosition Stored original position of the property.
modifiedPosition Current position which has been modified by other validators, if this property contains other validator attributes.

Example

[ValidatorTarget(typeof(ExampleAttribute))]
public class ExampleValidator : PropertyValidator
{
    // Called before OnValidatorGUI() for modify property position.
    public override void ModifyPropertyPosition(Rect originalPosition, ref Rect modifiedPosition)
    {
        const float offset = 5.0f;
        modifiedPosition.x += offset;
        modifiedPosition.width -= offset;
    }
}

BeforePropertyGUI

Description

Called before drawing property and before OnValidatorGUI().

void BeforePropertyGUI(Rect position, SerializedProperty property, GUIContent label)
{
    // Before property GUI calls here...
}

Parameters

Parameter Description
position Position of the serialized property.
property Serialized property with ValidatorAttribute.
label Label of serialized property.

Example

[ValidatorTarget(typeof(ExampleAttribute))]
public class ExampleValidator : PropertyValidator
{
    // Called before drawing property and before OnValidatorGUI().
    public override void BeforePropertyGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        EditorGUI.BeginDisabledGroup(true);
    }
}

OnValidatorGUI

Description

Called for rendering and handling GUI events.

void OnValidatorGUI(Rect originalPosition, Rect validatorPosition, SerializedProperty property, GUIContent label)
{
    // Property GUI here...
}

Parameters

Parameter Description
originalPosition Stored original position of the property.
validatorPosition Rectangle on the screen to use for the validator GUI.
property Serialized property with ValidatorAttribute.
label Label of serialized property.

Example

[ValidatorTarget(typeof(ExampleAttribute))]
public class ExampleValidator : PropertyValidator
{
    // Called for rendering and handling GUI events.
    public override void OnValidatorGUI(Rect originalPosition, Rect validatorPosition, SerializedProperty property, GUIContent label)
    {
        GUI.Label(validatorPosition, "Custom validator message");
    }
}

AfterPropertyGUI

Description

Called after drawing property and after OnValidatorGUI().

void AfterPropertyGUI(Rect position, SerializedProperty property, GUIContent label)
{
    // After property GUI calls here...
}

Parameters

Parameter Description
position Position of the serialized property.
property Serialized property with ValidatorAttribute.
label Label of serialized property.

Example

[ValidatorTarget(typeof(ExampleAttribute))]
public class ExampleValidator : PropertyValidator
{
    // Called after drawing property and after OnValidatorGUI().
    public override void AfterPropertyGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        EditorGUI.EndDisabledGroup();
    }
}

GetValidatorHeight

Description

Get the height of the validator, which required to display it.

Calculate only the size of the current validator, not the entire property.

The validator height will be added to the total size of the property.

// Return height which needed to draw validator.
float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
    // Return height of validator.
}

Parameters

Parameter Description
property Serialized property with ValidatorAttribute.
label Label of serialized property.

Example

[ValidatorTarget(typeof(ExampleAttribute))]
public class ExampleValidator : PropertyValidator
{
    // Return height which needed to draw validator.
    public virtual float GetPropertyHeight(SerializedProperty property, GUIContent label)
    {
        return 20;
    }
}