Tri-Inspector/Editor.Extras/Validators/SceneObjectsOnlyValidator.cs
2022-07-12 13:34:59 +03:00

36 lines
1.2 KiB
C#

using TriInspector;
using TriInspector.Validators;
using UnityEditor;
using UnityEngine;
[assembly: RegisterTriAttributeValidator(typeof(SceneObjectsOnlyValidator))]
namespace TriInspector.Validators
{
public class SceneObjectsOnlyValidator : TriAttributeValidator<SceneObjectsOnlyAttribute>
{
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} cannot be an asset.");
}
}
}