How to make attributes to draw a button in a method? How to get SerializedProperty value?

1 - I think PropertyAttribute is only for Fields, how can I fix it?

2 - How do I get the value of SerializedProperty? With that it would be easy to get the MemberInfo and do the Invoke

[AttributeUsage(AttributeTargets.Method)]
    public sealed class ButtonAttribute : PropertyAttribute
    {
        public string Name { get; set; }
        public ButtonAttribute(string name)
        {
            Name = name;
        }
    }
    [CustomPropertyDrawer(typeof(ButtonAttribute))]
    public class ButtonAttributeDrawer : PropertyDrawer
    {
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            // TODO: How to get the MemberInfo?
            MethodInfo methodInfo = null;
            ButtonAttribute buttonAttribute = attribute as ButtonAttribute;
            if (GUILayout.Button(buttonAttribute.Name))
            {
                // TODO: this is right?
                object target = property.serializedObject;
                methodInfo.Invoke(target, null);
            }
        }
    }
    }

I just use things like this:

https://github.com/madsbangh/EasyButtons

I think that’s what you’re asking about … ? Not 100% sure…

That’s exactly what I’m trying to do! I actually did it the same way EasyButton did it, drawing buttons from using [CustomEditor(typeof(Object), true)]

I downloaded EasyButton and tested it, it has the same problem I’m going through. It does not support buttons inside Objects.

8711388--1177176--upload_2023-1-6_20-35-29.png

8711388--1177179--upload_2023-1-6_20-35-49.png

Do you know how can I draw buttons inside objects?

I think this is a limitation on Unity’s already very limited inspector system.

I notice that pretty much all of NaughtyAttribute’s… attributes also state they don’t work when nested inside classes and structs: BoxGroup — NaughtyAttributes 2.1.4 documentation

DISREGARD the following, I just looked at how CPDs work… Sorry, not sure how to solve your problem!

I think you could perhaps just wind it in with the Custom Property drawer concept we talked about in the other thread?

I think once you are in code that is drawing a custom property, you would just do an if (GUILayout.Button()) for that item… I gotta admit though, I’ve not gone to this level of Inception on the editor. :slight_smile:

I found a way to access methods and attributes with buttons. But I haven’t been able to include it in the correct item yet.

About the question 2: GetTargetObjectOfProperty

[CustomPropertyDrawer(typeof(object), true)]
    public class ObjectDrawer : PropertyDrawer
    {
        public override VisualElement CreatePropertyGUI(SerializedProperty property)
        {
            object serializedValue = GetTargetObjectOfProperty(property);
            PropertyField propertyField = new PropertyField(property);
            propertyField.bindingPath = property.propertyPath;
            propertyField.name = property.name;

            if (property.propertyType != SerializedPropertyType.Generic || serializedValue == null)
            {
                return propertyField;
            }

           VisualElement root = new VisualElement();
           root.Add(propertyField);

           Button button = new Button();
           SetButton(button, serializedValue);
           root.Add(button);

            return root.;
        }

        // GetTargetObjectOfProperty...

    }
}

In this way, it was possible to include a button, but not be able to insert it inside the PropertyField. Do you know why?

Using a delay I can put everything inside the PropertyField, before is not possible