ValidateInputAttribute

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

Use this to enforce correct values.

namespace Sirenix.OdinInspector
{
[DontApplyToListElementsAttribute]
[AttributeUsageAttribute((AttributeTargets)384, AllowMultiple = True, Inherited = True)]
public sealed class ValidateInputAttribute : Attribute
}

Requirements

Namespace: Sirenix.OdinInspector
Assembly: Sirenix.OdinInspector
Version: 1.0.4.1

See Also

Constructors

Definition Description
ValidateInputAttribute(string, string, InfoMessageType) Initializes a new instance of the ValidateInputAttribute class.
ValidateInputAttribute(string, string, InfoMessageType, bool) Obsolete. Rejecting invalid input is no longer supported. Use the other constructors instead.

Properties

Return Type Definition Description
string DefaultMessage Default message for invalid values.
bool IncludeChildren Whether to also trigger validation when changes to child values happen. This is true by default.
string MemberName Name of callback function to validate input. The function must have at least one parameter of the same type as the property.
InfoMessageType MessageType The type of the message.

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