mirror of
https://github.com/codewriter-packages/Tri-Inspector.git
synced 2025-01-22 08:18:49 -05:00
Add TypeMismatch validator
This commit is contained in:
parent
b286b66551
commit
538df6c2a5
29
Editor.Extras/Validators/TypeMismatchValidator.cs
Normal file
29
Editor.Extras/Validators/TypeMismatchValidator.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
3
Editor.Extras/Validators/TypeMismatchValidator.cs.meta
Normal file
3
Editor.Extras/Validators/TypeMismatchValidator.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 510766d9f1764aae8c21d517ebb2c392
|
||||
timeCreated: 1652079793
|
Loading…
Reference in New Issue
Block a user