NotNullOrEmpty

  06. Attributes No Comments

When added before a class member, indicates that its value can not be null or empty.

If the value is null or empty, target will be highlighted with red color in the inspector view.

Attribute Target

Field, property, indexer, method return value or method parameter.
Target class must implement either the ICollection or IEnumerable interface in order to be validated based on being empty or not. If the current value of target does not implement neither interface, then validation will be based on only on whether or not the value is null or not.

Example

using Sisus.Attributes;
using UnityEngine;

public class ComponentsLister : MonoBehaviour
{
	[NotNullOrEmpty(Target.Collection), NotNullOrEmpty(Target.Members), ShowInInspector, ReadOnly]
	private string[] componentNames;

	// called when the script is loaded or a value is changed in the inspector
	void OnValidate() 
	{
		UpdateComponentNames();
	}

	void UpdateComponentNames()
	{
		var components = GetComponents<Component>();
		int count = components.Length;
		componentNames = new string[count];
		for(int n = 0; n < count; n++)
		{
			var component = components[n];
			componentNames[n] = component == null ? "" : component.GetType().Name;
		}
	}
}

Example Result