Add OnValueChanged attribute

This commit is contained in:
VladV 2022-05-11 14:15:12 +03:00
parent fb8041857a
commit de2582134f
5 changed files with 45 additions and 0 deletions

View File

@ -277,6 +277,17 @@ namespace TriInspector
void ITriPropertyParent.NotifyValueChanged(TriProperty property)
{
if (_definition.OnValueChanged != null)
{
_serializedProperty?.serializedObject.ApplyModifiedProperties();
for (var targetIndex = 0; targetIndex < PropertyTree.TargetObjects.Length; targetIndex++)
{
_definition.OnValueChanged.InvokeForTarget(this, targetIndex);
EditorUtility.SetDirty(PropertyTree.TargetObjects[targetIndex]);
}
}
_parent.NotifyValueChanged(property);
}

View File

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using JetBrains.Annotations;
using TriInspector.Resolvers;
using TriInspector.Utilities;
using UnityEngine;
@ -62,6 +63,11 @@ namespace TriInspector
IsArray = true;
ArrayElementType = elementType;
}
if (Attributes.TryGet(out OnValueChangedAttribute onValueChangedAttribute))
{
OnValueChanged = ActionResolver.Resolve(this, onValueChangedAttribute.Method);
}
}
public MemberInfo MemberInfo { get; }
@ -81,6 +87,8 @@ namespace TriInspector
public bool IsArray { get; }
[CanBeNull] public ActionResolver OnValueChanged { get; }
public IReadOnlyList<TriPropertyHideProcessor> HideProcessors
{
get

View File

@ -19,6 +19,7 @@ public class BasicSample : MonoBehaviour
public float unityField;
[Required]
[OnValueChanged(nameof(OnMaterialChanged))]
public Material mat;
[ValidateInput(nameof(ValidateTexture))]
@ -61,6 +62,11 @@ public class BasicSample : MonoBehaviour
public float rotation;
}
private void OnMaterialChanged()
{
Debug.Log("Material changed!");
}
private TriValidationResult ValidateTexture()
{
if (tex == null) return TriValidationResult.Error("Tex is null");

View File

@ -0,0 +1,17 @@
using System;
using System.Diagnostics;
namespace TriInspector
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
[Conditional("UNITY_EDITOR")]
public sealed class OnValueChangedAttribute : Attribute
{
public OnValueChangedAttribute(string method)
{
Method = method;
}
public string Method { get; }
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: ae1fa081898a4d13b8612da57bc9a349
timeCreated: 1652265504