#Property Attribute - Calling a method when a color field is changed

1 messages · Page 1 of 1 (latest)

keen verge
#

Inspector? EditorWindow? 😄

#

Sorry, I trust lol. It's nice to see people using the thread feature.

timid spade
#

I made a PropertyAttribute that is intended to pick up changes from the Inspector and call the specified method by it's name and it works for a bool but won't pick up Color changes

namespace CustomAttributes {
    public class OnChangedAttribute : PropertyAttribute {
        // The name of the method to call when the property is changed
        public readonly string MethodName;
        // Whether or not to invoke the target property with the value of itself
        public readonly bool InvokeProperty;

        public OnChangedAttribute(string methodName,bool invokeProperty = false) {
            MethodName = methodName;
            InvokeProperty = invokeProperty;
        }
    }
    
#if UNITY_EDITOR
    [CustomPropertyDrawer(typeof(OnChangedAttribute))]
    public class OnChangedAttributePropertyDrawer : PropertyDrawer {
        public override void OnGUI(Rect position,SerializedProperty property,GUIContent label) {
            EditorGUI.BeginChangeCheck();
            EditorGUI.PropertyField(position,property,label,false);
            if(!EditorGUI.EndChangeCheck()) return;
            var attr = attribute as OnChangedAttribute;
            if(attr == null) return;
            Object targetObj = property.serializedObject.targetObject;
            MethodInfo method = targetObj.GetType().GetMethod(attr.MethodName);
            if(method == null || method.GetParameters().Any()) return;
            if(attr.InvokeProperty) {
                object[] objs = {targetObj};
                method.Invoke(targetObj,objs);
            }else method.Invoke(targetObj,null);
        }
    }
#endif
}
#

This is the field that uses the PropertyAttribute

// [Attribute] BackgroundColor
        [SerializeField,ColorUsage(true,false),OnChanged("OnBackgroundColorChange",true)]
        private Color _backgroundColor;
        public Color BackgroundColor {
            get => _backgroundColor;
            set => OnBackgroundColorChange(value);
        }
#

This is the method that the PropertyAttribute targets

public void OnBackgroundColorChange(Color color) {
            Debug.Log("OnBackgroundColorChange");
            PreviousColor = imageComponent.color;
            _backgroundColor = color;
            imageComponent.color = color;
        }
#

When I change Background Color in the Inspector to any color it doesn't invoke the method but when I change Override Color From Parent it does invoke it's method

keen verge
#

This is a bit spooky, but I have a strong feeling that the attribute isn't performing right because the inspector can't recognize properties like (BackgroundColor {get;set;}).

timid spade
#

It can't which is why I used private Color _backgroundColor

#

as the actual field that the inspector uses

velvet sorrel
#

What's ColorUsage?

keen verge
#

Right right...

velvet sorrel
#

Also, what's the definition for the override color field?

timid spade
# velvet sorrel Also, what's the definition for the override color field?
// [Attribute] OverrideColorFromParent
        [SerializeField,OnChanged("OnOverrideColorChange",false),Tooltip("When true this will use the BackgroundColor of the parent Image component instead of the BackgroundColor attribute")]
        private bool _overrideColorFromParent = false;
        public bool OverrideColorFromParent {
            get => _overrideColorFromParent;
            set {
                _overrideColorFromParent = value;
                OnOverrideColorChange();
            }
        }
timid spade
#

It is a built in attribute

#

basically lets me turn on alpha as editable in the inspector

#

for the color field

velvet sorrel
#

Oh what, that's "new"

#

Anyways, you can't stack property drwaers like that

#

ColorUsage is it's own propertydrawer

timid spade
#

oh so you can't use more then one?

velvet sorrel
#

Nope

timid spade
#

That may be the issue let me remove that and see if it picks up changes

#

I don't see the point of that PropertyDrawer to be honest because after I removed it and inspected the BackgroundColor property I still see alpha as editable

#

also apparently even after removing ColorUsage it still doesn't pick up any changes for BackgroundColor

// [Attribute] BackgroundColor
        [SerializeField,OnChanged("OnBackgroundColorChange",true)]
        private Color _backgroundColor;
        public Color BackgroundColor {
            get => _backgroundColor;
            set => OnBackgroundColorChange(value);
        }
#

but it does work when toggling Override Color From Parent

velvet sorrel
#

Doesn't your log indicate that it triggers?

timid spade
#

when toggling Override Color From Parent only

velvet sorrel
#

Or does it trigger from the wrong field?

timid spade
#

it triggers internally

#

not because of inspector

#
public void OnOverrideColorChange() {
            Debug.Log("OverrideChanged");
            if(OverrideColorFromParent && gameObject.transform.parent.TryGetComponent(out Image imgComponent)) {
                BackgroundColor = imgComponent.color;
            }
            else BackgroundColor = PreviousColor;
        }
#

by me setting BackgroundColor

#

it calls OnBackgroundColorChange()

keen verge
#
                // TRY A DEBUG MESSAGE HERE?
                object[] objs = {targetObj};
                method.Invoke(targetObj,objs);
            }else method.Invoke(targetObj,null);```
Spitballing as best as I can here...
velvet sorrel
#

Add a breakpoint to your propertydrawer that it actually triggers

velvet sorrel
#

I'm guessing it's not being used for some reason

timid spade
#

My guess originally was that the color editor is different and it's not registering the change

#

compared to something as simple as a bool

velvet sorrel
#

Nah itshould still trigger the changecheck

keen verge
timid spade
#

I can try a debug on the exit

velvet sorrel
#

If it can't find your method sure

#

But I doubt it

timid spade
#

wait

#

sorry

#

it is exiting

#

because

#

I have OnBackgroundColor

#

take it a color

#

which has an argument

#

mb

velvet sorrel
#

Oh lol

#

Missed that too

timid spade
#

haha let me rewrite this but thanks for the help guys

keen verge
#

Ookay, I'ma go eat an icecream sandwich and feel myself for a bit. (That was a bit tough to spot)

timid spade
#

enjoy that sandwich honestly don't know how I overlooked that

keen verge
#

It was just the nearest neighbour lol.