Version 2.1.0.0

Odin has a dedicated attribute overview with examples

ValidateInputAttribute class

Namespace: Sirenix.OdinInspector
Assembly: Sirenix.OdinInspector.Attributes
[DontApplyToListElements]
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Module | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Interface | AttributeTargets.Parameter | AttributeTargets.Delegate | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter | AttributeTargets.All, AllowMultiple = true, Inherited = true)]
public sealed class ValidateInputAttribute : Attribute, _Attribute

ValidateInput is used on any property, and allows to validate input from inspector.

Use this to enforce correct values.

Inheritance
  • System.Object
  • System.Attribute
  • ValidateInputAttribute
Remarks

note

ValidateInput refuses invalid values.

note

ValidateInput only works in the editor. Values changed through scripting will not be validated.

Example

The following examples shows how a speed value can be forced to be above 0.

public class MyComponent : MonoBehaviour
{
	[ValidateInput("ValidateInput")]
	public float Speed;

	// Specify custom output message and message type.
	[ValidateInput("ValidateInput", "Health must be more than 0!", InfoMessageType.Warning)]
	public float Health;

	private bool ValidateInput(float property)
	{
		return property > 0f;
	}
}
Example

The following example shows how a static function could also be used.

public class MyComponent : MonoBehaviour
{
	[ValidateInput("StaticValidateFunction")]
	public int MyInt;

	private static bool StaticValidateFunction(int property)
	{
		return property != 0;
	}
}

Constructors

ValidateInputAttribute(String, String, InfoMessageType)
Initializes a new instance of the ValidateInputAttribute class.
public ValidateInputAttribute(string memberName, string defaultMessage = null, InfoMessageType messageType = InfoMessageType.Error)
Parameters
System.String memberName

Name of callback function to validate input. The function must have at least one parameter of the same type as the property.

System.String defaultMessage

Default message for invalid values.

InfoMessageType messageType

Type of the message.

Fields

ContiniousValidationCheck
If true, the validation method will not only be executed when the User has changed the value. It'll run once every frame in the inspector.
public bool ContiniousValidationCheck
DefaultMessage
Default message for invalid values.
public string DefaultMessage
IncludeChildren
Whether to also trigger validation when changes to child values happen. This is true by default.
public bool IncludeChildren
MemberName
Name of callback function to validate input. The function must have at least one parameter of the same type as the property.
public string MemberName
MessageType
The type of the message.
public InfoMessageType MessageType

This website is brand new and built with the Odin 2.1 beta in mind. This means you might discover some features such as attribute expressions that are only available in the beta.

You can download the 2.1 beta from here and read the patch-notes here.