Unity Udexreal开发插件包
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
1.1 KiB

using UnityEditor;
namespace NaughtyAttributes.Editor
{
public class RequiredPropertyValidator : PropertyValidatorBase
{
private RequiredAttribute _cachedRequiredAttribute;
public override void ValidateProperty(SerializedProperty property)
{
if (_cachedRequiredAttribute == null)
_cachedRequiredAttribute = PropertyUtility.GetAttribute<RequiredAttribute>(property);
RequiredAttribute requiredAttribute = _cachedRequiredAttribute;
if (property.propertyType == SerializedPropertyType.ObjectReference)
{
if (property.objectReferenceValue == null)
{
string errorMessage = property.name + " is required";
if (!string.IsNullOrEmpty(requiredAttribute.Message))
{
errorMessage = requiredAttribute.Message;
}
NaughtyEditorGUI.HelpBox_Layout(errorMessage, MessageType.Error, context: property.serializedObject.targetObject);
}
}
else
{
string warning = requiredAttribute.GetType().Name + " works only on reference types";
NaughtyEditorGUI.HelpBox_Layout(warning, MessageType.Warning, context: property.serializedObject.targetObject);
}
}
}
}