Tri-Inspector/Editor.Integrations/Odin/OdinObjectValidator.cs

65 lines
1.8 KiB
C#
Raw Normal View History

using System;
using Sirenix.OdinInspector.Editor;
using Sirenix.OdinInspector.Editor.Validation;
using TriInspector.Editor.Integrations.Odin;
using UnityEditor;
[assembly: RegisterValidator(typeof(OdinObjectValidator<>))]
namespace TriInspector.Editor.Integrations.Odin
{
public class OdinObjectValidator<T> : ValueValidator<T>, IDisposable
where T : UnityEngine.Object
{
2022-06-06 12:17:31 -04:00
private bool _initialized;
private TriPropertyTreeForSerializedObject _propertyTree;
private SerializedObject _serializedObject;
public override RevalidationCriteria RevalidationCriteria { get; }
= RevalidationCriteria.OnValueChangeOrChildValueChange;
public override bool CanValidateProperty(InspectorProperty property)
{
if (!property.IsTreeRoot)
{
return false;
}
2022-06-06 12:17:31 -04:00
var type = property.Info.TypeOfValue;
if (type == null)
{
return false;
}
2022-08-28 08:29:49 -04:00
if (!TriOdinUtility.IsDrawnByTri(type))
{
return false;
}
return true;
}
public void Dispose()
{
2022-06-06 12:17:31 -04:00
_propertyTree?.Dispose();
_serializedObject?.Dispose();
}
protected override void Validate(ValidationResult result)
{
2022-06-06 12:17:31 -04:00
if (!_initialized)
{
_initialized = true;
_serializedObject = new SerializedObject(ValueEntry.SmartValue);
_propertyTree = new TriPropertyTreeForSerializedObject(_serializedObject);
}
_serializedObject.Update();
_propertyTree.Update();
_propertyTree.RunValidation();
_propertyTree.CopyValidationResultsTo(result);
}
}
}