Add option to set default buttonstyle for methods with parameters

Issue #804 resolved
Christian Knaapen created an issue

The default buttonstyle currently is CompactBox, according to the docs, but I don’t really like the look of that one, so I often add ButtonStyle.FoldoutButton to my Button attributes. But now my code is full with [Button(ButtonStyle.FoldoutButton)] instead of a cleaner [Button]. It would be great if there was a global preference setting to set the default, like the many options under the general tab of the odin preferences.

Comments (7)

  1. Antonio Rafael Antunes Miranda

    You could use Odin’s composite attributes feature which makes this sort of specialized attribute usage very easy.

    using Sirenix.OdinInspector;
    using System;
    
    [IncludeMyAttributes]
    [Button(ButtonStyle.FoldoutButton)]
    public class FoldoutButtonAttribute : Attribute { }
    

  2. Christian Knaapen reporter

    That’s a good suggestion, I didn’t know about those. But when I try your code it gives me a compile error: Button is valid on Method declarations only.

  3. Antonio Rafael Antunes Miranda

    I think this should work from version 3.0.6.0 and up so if you’re below that, that might be the problem.

  4. Antonio Rafael Antunes Miranda

    In that case, if you want/need to stay at 3.0.5.0 you could also make use of Odin’s attribute processors which should be available starting with version 2.0.0.0.
    They require a bit more boilerplate code though:

    using Sirenix.OdinInspector;
    using Sirenix.OdinInspector.Editor;
    using System;
    using System.Collections.Generic;
    using System.Reflection;
    
    public class FoldoutButtonAttribute : Attribute { }
    
    public class FoldoutButtonAttributeProcessor : OdinAttributeProcessor
    {
        public override bool CanProcessChildMemberAttributes(InspectorProperty parentProperty, MemberInfo member)
        {
            return member.GetCustomAttribute<FoldoutButtonAttribute>() != null;
        }
    
        public override void ProcessChildMemberAttributes(InspectorProperty parentProperty, MemberInfo member, List<Attribute> attributes)
        {
            attributes.Add(new ButtonAttribute(ButtonStyle.FoldoutButton));
        }
    }
    

    Ultimately, I think that expanding the preferences window to have more Configurability for cases like this would be a good step to improve readability by removing extra boilerplate code, but this will probably be a long process. In the meantime, using one of the 2 provided alternative ways should help bridge that time.

  5. Christian Knaapen reporter

    I updated to the latest version, so the first way works now. But it’s nice that there are more options!

    This issue is solved for me. So you can mark it as resolved, unless you want to keep it open as a reminder to add the configurability later. Thanks for your help!

  6. Log in to comment