Version 3.3.0.1

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)]
[Conditional("UNITY_EDITOR")]
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 condition, string defaultMessage = null, InfoMessageType messageType = InfoMessageType.Error)
Parameters
System.String condition

A resolved string that should evaluate to a boolean value, and which should validate the input. Note that in expressions, the $value named parameter, and in methods, a parameter named value, can be used to get the validated value instead of referring to the value by its containing member. This makes it easier to reuse validation strings.

System.String defaultMessage

Default message for invalid values.

InfoMessageType messageType

Type of the message.

Fields

Condition
A resolved string that should evaluate to a boolean value, and which should validate the input. Note that in expressions, the $value named parameter, and in methods, a parameter named value, can be used to get the validated value instead of referring to the value by its containing member. This makes it easier to reuse validation strings.
public string Condition
ContinuousValidationCheck
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 ContinuousValidationCheck
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
MessageType
The type of the message.
public InfoMessageType MessageType