Tri-Inspector/Editor.Extras/Validators/AssetsOnlyValidator.cs
2022-06-10 15:34:49 +03:00

36 lines
1.1 KiB
C#

using TriInspector;
using TriInspector.Validators;
using UnityEditor;
using UnityEngine;
[assembly: RegisterTriAttributeValidator(typeof(AssetsOnlyValidator))]
namespace TriInspector.Validators
{
public class AssetsOnlyValidator : TriAttributeValidator<AssetsOnlyAttribute>
{
public override TriExtensionInitializationResult Initialize(TriPropertyDefinition propertyDefinition)
{
if (!typeof(Object).IsAssignableFrom(propertyDefinition.FieldType))
{
return "AssetsOnly attribute can be used only on Object fields";
}
return TriExtensionInitializationResult.Ok;
}
public override TriValidationResult Validate(TriProperty property)
{
var obj = property.TryGetSerializedProperty(out var serializedProperty)
? serializedProperty.objectReferenceValue
: (Object) property.Value;
if (obj == null || AssetDatabase.Contains(obj))
{
return TriValidationResult.Valid;
}
return TriValidationResult.Error($"{obj} is not as asset.");
}
}
}