Our Verification Attributes are a powerful way to enforce constraints and perform validations on fields within your Unity scripts. These attributes can be used to ensure that data and configurations meet specific conditions, which is essential for maintaining data integrity and preventing errors. These attributes help developers by catching mistakes early, at the editor level, before they propagate into runtime errors or unexpected behavior.
How They Work
Custom Verification Attributes are added to fields in your scripts. These attributes contain rules defining what is considered valid for the respective fields. When the script is used within the Unity editor, the attributes perform checks based on these rules and provide warnings or errors if the data in the fields doesn’t conform to the rules.
How to Use Them
To use these custom verification attributes, simply add the attribute above the field you want to apply validation to, and pass in any necessary parameters to configure the rule.
Example:
[VStringCheck(StringCheck.Email, minLength = 5, maxLength = 30)]
public string emailAddress;
[VRequired]
public GameObject prefab;
This example ensures that the emailAddress
field contains a valid email and that it is between 5 and 30 characters long. Also, it ensures that the prefab is always assigned – if not, a notification will be thrown in the system.
Now, let’s take a look at the available attributes and how to use them.
List of attributes
⭐ This is a list of the attributes our system currently provides. You can extend it with your own logic using the VCustomValidationAttribute or let us know if something is missing!
VAssetsOnlyAttribute
- Description: Validates that a field assigned with a UnityEngine.Object only contains references to assets, not scene objects.
- Parameters: None.
- Example:
[VAssetsOnly]
public GameObject prefab;
VCustomValidationAttribute
- Description: Allows for custom validation logic by providing a custom validation method. That way, you can extend the verify functions with your own logic.
- Parameters:
validationType
(Type): The type that contains the custom validation method.validationMethodName
(string): The name of the custom validation method.
- Example:
public static class MyCustomValidators
{
public static bool ValidatePositive(int value)
{
return value > 0;
}
}
public class MyScript : MonoBehaviour
{
[VCustomValidation(typeof(MyCustomValidators), "ValidatePositive")]
public int MyValue;
}
VDisallowTypeAttribute
- Description: Validates that a field does not contain an instance of a specified type.
- Parameters:
disallowedType
(Type): The type that should not be assigned to the field.
- Example:
[VDisallowType(typeof(Sprite))]
public Object myObject;
VElementCountAttribute
- Description: Validates that a collection contains a specific number of elements.
- Parameters:
min
(int): The minimum number of elements (inclusive).max
(int): The maximum number of elements (inclusive).
- Example:
[VElementCount(1, 3)]
public int[] numbers;
[VElementCount(max: 3)]
public int[] numbers;
VNonNullElementsAttribute
- Description: Validates that all elements in a collection (array, list, etc.) are non-null.
- Parameters: None.
- Example:
[VNonNullElements]
public GameObject[] gameObjects;
VObsoleteAttribute
- Description: Marks a field as obsolete, so it can be flagged in the editor.
- Parameters: None.
- Example:
[VObsolete]
public int oldField;
VRangeAttribute
- Description: Validates that the value of a numeric field is within a specified range.
- Parameters:
minValue
(float): The minimum value of the range (inclusive).maxValue
(float): The maximum value of the range (inclusive).
- Example:
[VRange(1, 5)]
public int myNumber;
VRequiredAttribute
- Description: Validates that a field is not null or empty (for strings and collections).
- Parameters: None.
- Example:
[VRequired]
public string name;
VRequireChildObjectAttribute
- Description: Validates that a GameObject field is a child of the GameObject the script is attached to.
- Parameters:
includeSelf
(bool): indicated if a reference to the object itself is allowed (default false)
- Example:
[VRequireChildObject]
public GameObject childObject;
VRequireComponentAttribute
- Description: Validates that a GameObject or MonoBehaviour field has a specific component attached. This is similar to the
RequireComponent
provided by Unity, but for fields, not classes. - Parameters:
requiredComponentType
(Type): The type of the required component.
- Example:
[VRequireComponent(typeof(Rigidbody))]
public GameObject myGameObject;
VSceneObjectsOnlyAttribute
- Description: Validates that a field assigned with a
UnityEngine.Object
only contains references to scene objects, not assets. - Parameters: None.
- Example:
[VSceneObjectsOnly]
public GameObject sceneObject;
VStringCheckAttribute
- Description: Provides multiple string validation options such as ensuring non-emptiness, validating URLs, email addresses, checking minimum/maximum length, and more.
- Parameters:
checks
(StringCheck flags): The validation checks to be performed on the string.disallowPattern
(string): A regex pattern that the string should not match. Default is an empty string, which means no pattern checking.minLength
(int): Minimum length of the string. Default is 0.maxLength
(int): Maximum length of the string. Default is int.MaxValue.
- StringCheck Flags:
NotEmpty
: Ensure the string is not empty.URL
: Ensure the string is a well-formed URL.NoWhiteSpace
: Ensure the string does not contain white spaces.Numeric
: Ensure the string is numeric.Email
: Ensure the string is a valid email address.Alphabetic
: Ensure the string is alphabetic.Alphanumeric
: Ensure the string is alphanumeric.URLAbsolute
: Ensure the string is an absolute URL.URLRelative
: Ensure the string is a relative URL.MinLength
: Check that the string is at least the specified minimum length.MaxLength
: Check that the string is at most the specified maximum length.FilePath
: Ensure the string is a valid file path.DirectoryPath
: Ensure the string is a valid directory path.
- Example:
[VStringCheck(StringCheck.NotEmpty | StringCheck.Email, minLength = 5, maxLength = 30)]
public string emailAddress;
[VStringCheck(StringCheck.FilePath)]
public string filePath;
[VStringCheck(StringCheck.DirectoryPath)]
public string directoryPath;
[VStringCheck(StringCheck.Numeric | StringCheck.MinLength, minLength = 10)]
public string numericString;
VUniqueElementsAttribute
- Description: Validates that a collection only contains unique elements.
- Parameters: None.
- Example:
[VUniqueElements]
public int[] numbers;