#Property Attribute - Calling a method when a color field is changed
1 messages · Page 1 of 1 (latest)
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
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;}).
It can't which is why I used private Color _backgroundColor
as the actual field that the inspector uses
What's ColorUsage?
Right right...
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();
}
}
It is a built in attribute
basically lets me turn on alpha as editable in the inspector
for the color field
Oh what, that's "new"
Anyways, you can't stack property drwaers like that
ColorUsage is it's own propertydrawer
oh so you can't use more then one?
Nope
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
Doesn't your log indicate that it triggers?
when toggling Override Color From Parent only
Or does it trigger from the wrong field?
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()
// TRY A DEBUG MESSAGE HERE?
object[] objs = {targetObj};
method.Invoke(targetObj,objs);
}else method.Invoke(targetObj,null);```
Spitballing as best as I can here...
Add a breakpoint to your propertydrawer that it actually triggers
It never logged
I'm guessing it's not being used for some reason
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
Nah itshould still trigger the changecheck
if(method == null || method.GetParameters().Any()) return; is this possibly exiting early?
I can try a debug on the exit
wait
sorry
it is exiting
because
I have OnBackgroundColor
take it a color
which has an argument
mb
haha let me rewrite this but thanks for the help guys
Ookay, I'ma go eat an icecream sandwich and feel myself for a bit. (That was a bit tough to spot)
enjoy that sandwich honestly don't know how I overlooked that
I didn't fully make sense of it in my head to be honest.
It was just the nearest neighbour lol.