diff --git a/Editor.Extras/Validators/RequiredValidator.cs b/Editor.Extras/Validators/RequiredValidator.cs index b3bca90..7116164 100644 --- a/Editor.Extras/Validators/RequiredValidator.cs +++ b/Editor.Extras/Validators/RequiredValidator.cs @@ -1,5 +1,7 @@ -using TriInspector.Validators; +using JetBrains.Annotations; +using TriInspector.Validators; using TriInspector; +using TriInspector.Resolvers; [assembly: RegisterTriAttributeValidator(typeof(RequiredValidator), ApplyOnArrayElement = true)] @@ -7,6 +9,18 @@ namespace TriInspector.Validators { public class RequiredValidator : TriAttributeValidator { + [CanBeNull] private ActionResolver _fixActionResolver; + + public override TriExtensionInitializationResult Initialize(TriPropertyDefinition propertyDefinition) + { + if (Attribute.FixAction != null) + { + _fixActionResolver = ActionResolver.Resolve(propertyDefinition, Attribute.FixAction); + } + + return TriExtensionInitializationResult.Ok; + } + public override TriValidationResult Validate(TriProperty property) { if (property.FieldType == typeof(string)) @@ -15,7 +29,7 @@ namespace TriInspector.Validators if (isNull) { var message = Attribute.Message ?? $"{GetName(property)} is required"; - return TriValidationResult.Error(message); + return MakeError(message, property); } } else if (typeof(UnityEngine.Object).IsAssignableFrom(property.FieldType)) @@ -24,7 +38,7 @@ namespace TriInspector.Validators if (isNull) { var message = Attribute.Message ?? $"{GetName(property)} is required"; - return TriValidationResult.Error(message); + return MakeError(message, property); } } else @@ -35,6 +49,23 @@ namespace TriInspector.Validators return TriValidationResult.Valid; } + private TriValidationResult MakeError(string error, TriProperty property) + { + var result = TriValidationResult.Error(error); + + if (_fixActionResolver != null) + { + result = AddFix(result, property); + } + + return result; + } + + private TriValidationResult AddFix(TriValidationResult result, TriProperty property) + { + return result.WithFix(() => _fixActionResolver?.InvokeForAllTargets(property), Attribute.FixActionName); + } + private static string GetName(TriProperty property) { var name = property.DisplayName; diff --git a/Editor.Samples/Validators/Validators_RequiredSample.cs b/Editor.Samples/Validators/Validators_RequiredSample.cs index 5787d8a..c3f986d 100644 --- a/Editor.Samples/Validators/Validators_RequiredSample.cs +++ b/Editor.Samples/Validators/Validators_RequiredSample.cs @@ -1,8 +1,14 @@ -using TriInspector; +using System.Linq; +using TriInspector; using UnityEngine; public class Validators_RequiredSample : ScriptableObject { - [Required] - public Material mat; + [Required(FixAction = nameof(FixMaterial), FixActionName = "Find in Resources")] + public Material material; + + private void FixMaterial() + { + material = Resources.FindObjectsOfTypeAll().FirstOrDefault(); + } } \ No newline at end of file diff --git a/README.md b/README.md index 762770f..060d4f2 100644 --- a/README.md +++ b/README.md @@ -143,11 +143,19 @@ Tri Inspector has some builtin validators such as `missing reference` and `type #### Required -![Required](https://user-images.githubusercontent.com/26966368/168233232-596535b4-bab8-462e-b5d8-7a1c090e5143.png) +![Required](https://github.com/codewriter-packages/Tri-Inspector/assets/26966368/56a8d0ef-c88b-4b4b-8121-388b94d47841) ```csharp [Required] -public Material mat; +public Material material; + +[Required(FixAction = nameof(FixTarget), FixActionName = "Assign self")] +public Transform target; + +private void FixTarget() +{ + target = GetComponent(); +} ``` #### ValidateInput diff --git a/Runtime/Attributes/RequiredAttribute.cs b/Runtime/Attributes/RequiredAttribute.cs index 0808e6a..cd19963 100644 --- a/Runtime/Attributes/RequiredAttribute.cs +++ b/Runtime/Attributes/RequiredAttribute.cs @@ -6,5 +6,8 @@ namespace TriInspector public class RequiredAttribute : Attribute { public string Message { get; set; } + + public string FixAction { get; set; } + public string FixActionName { get; set; } } } \ No newline at end of file diff --git a/Runtime/TriValidationResult.cs b/Runtime/TriValidationResult.cs index 78d18cd..d0be755 100644 --- a/Runtime/TriValidationResult.cs +++ b/Runtime/TriValidationResult.cs @@ -25,7 +25,7 @@ namespace TriInspector public TriValidationResult WithFix(Action action, string name = null) { - return new TriValidationResult(IsValid, Message, MessageType, action, new GUIContent(name)); + return new TriValidationResult(IsValid, Message, MessageType, action, new GUIContent(name ?? "Fix")); } public static TriValidationResult Info(string error)