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.
ValidateInput refuses invalid values.
ValidateInput only works in the editor. Values changed through scripting will not be validated.
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;
}
}
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;
}
}
public ValidateInputAttribute(string condition, string defaultMessage = null, InfoMessageType messageType = InfoMessageType.Error)
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. |
public string Condition
public bool ContinuousValidationCheck
public string DefaultMessage
public bool IncludeChildren
public InfoMessageType MessageType