Add TypeMismatch validator

This commit is contained in:
VladV 2022-05-09 10:14:06 +03:00
parent b286b66551
commit 538df6c2a5
2 changed files with 32 additions and 0 deletions

View File

@ -0,0 +1,29 @@
using TriInspector;
using TriInspector.Validators;
using UnityEditor;
[assembly: RegisterTriValueValidator(typeof(TypeMismatchValidator<>))]
namespace TriInspector.Validators
{
public class TypeMismatchValidator<T> : TriValueValidator<T>
where T : UnityEngine.Object
{
public override TriValidationResult Validate(TriValue<T> propertyValue)
{
if (propertyValue.Property.TryGetSerializedProperty(out var serializedProperty) &&
serializedProperty.propertyType == SerializedPropertyType.ObjectReference &&
serializedProperty.objectReferenceValue != null &&
(serializedProperty.objectReferenceValue is T) == false)
{
var displayName = propertyValue.Property.DisplayName;
var actual = serializedProperty.objectReferenceValue.GetType().Name;
var expected = propertyValue.Property.FieldType.Name;
var msg = $"{displayName} does not match the type: actual = {actual}, expected = {expected}";
return TriValidationResult.Warning(msg);
}
return TriValidationResult.Valid;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 510766d9f1764aae8c21d517ebb2c392
timeCreated: 1652079793