Search Unity

Ability to add enum argument to button functions

Discussion in 'Unity UI (uGUI) & TextMesh Pro' started by Slev, Sep 26, 2014.

  1. Romaleks360

    Romaleks360

    Joined:
    Sep 9, 2017
    Posts:
    17
  2. huulong

    huulong

    Joined:
    Jul 1, 2013
    Posts:
    48
    I'm using @llamagod 's solution, which works fine, but I'm worried about the fact that UnityEventDrawer.cs is taken from Unity source code and adds a patch in the middle of the DrawEventListener function...

    See original code on GitHub

    This code looks slighty different because:
    - llamagod is using a reverse-engineered version
    - the code has changed with the iterations for 2017, 2018, etc.
    - we must use properties and reflection to access private/internal members

    When Unity is upgraded, the original code will go further and further from our custom implementation. In the best case we'll lost some extra features and fixes from new versions, in the worst case compatibility will break and the project will stop compiling (in the editor).

    For now, I will copy-paste the patch (including reflection usage) inside the code that matches my version of Unity, but I wonder if there is not a solution involving overriding some nice method...
    Like setting a custom drawer for some IntAsEnum field, which would be used by PropertyField.

    The patching part:

    Code (CSharp):
    1.  
    2.             // Try to find Find the EnumActionAttribute
    3.             var method = GetMethod(m_DummyEvent, methodName.stringValue, listenerTarget.objectReferenceValue, GetMode(mode), desiredType);
    4.             object[] attributes = null;
    5.             if (method != null)
    6.                 attributes = method.GetCustomAttributes(typeof(EnumActionAttribute), true);
    7.             if (attributes != null && attributes.Length > 0)
    8.             {
    9.                 // Make an enum popup
    10.                 var enumType = ((EnumActionAttribute)attributes[0]).enumType;
    11.                 var value = (Enum)Enum.ToObject(enumType, argument.intValue);
    12.                 argument.intValue = Convert.ToInt32(EditorGUI.EnumPopup(argRect, value));
    13.             }
    14.             else
    15.             {
    16.                 EditorGUI.PropertyField(argRect, argument, GUIContent.none);
    17.             }
    18.  
    Also, don't forget to make the CustomPropertyDrawer work on `typeof(UnityEvent)`, not `typeof(UnityEventBase)`


    The history of my file to understand how I re-applied the patch from the released Unity code:
    https://github.com/hsandt/LD44/commits/master/Assets/Game/Scripts/Editor/UnityEventDrawer.cs
     
  3. llamagod

    llamagod

    Joined:
    Sep 27, 2015
    Posts:
    73
    @huulong Yes, your observations and concerns are valid. This is a hack. To my knowledge, this is about as good as it's going to get without being able to modify the Unity source code as an employee or overhauling the editor serialization system and inspector. I only really changed DrawEventListener, but it's a big method and it's private and it's called by another private method so it cannot be overridden without some serious reflection and low-level programming magic.

    The reason why I use typeof(UnityEventBase) is that I have to go a step up in the inheritance hierarchy such that my custom drawer will be picked for UnityEvent instead of the built-in Unity one. I can't think of any problems this could create.

    A proper fix for issues like this would require overhauling the editor serialization system and inspector. I don't know the internal workflow of Unity staff. But having attempted something like this before (also attempted by others, for example, https://assetstore.unity.com/packages/tools/utilities/odin-inspector-and-serializer-89041), I'm guessing this would take Unity 4 to 6 months for a small team dedicated to it (keep in mind this is for a brilliantly crafted solution that looks nice and is refactored many times and includes very thorough testing).
     
    Last edited: May 17, 2019
  4. fluffycritter

    fluffycritter

    Joined:
    Jun 2, 2019
    Posts:
    1
    I just want to thank you for posting this solution; I just tried it out and it works wonderfully. It'd be great if Unity were to fix it in the editor themselves but it's not all that onerous to use this workaround.
     
  5. JeevSiewnath

    JeevSiewnath

    Joined:
    Dec 31, 2013
    Posts:
    1
    Ridiculous, 2019 and still waiting for this basic functionality...
     
  6. Rathlord

    Rathlord

    Joined:
    Oct 13, 2013
    Posts:
    14
    C'mon Unity team we'd really love to have this...
     
  7. llamagod

    llamagod

    Joined:
    Sep 27, 2015
    Posts:
    73
    If you look at the Unity roadmap for 2019.3.0 "Polymorphic Serialization" is there which means they will have fixed the serialization system at that point. So I'm guessing this feature will be added relatively soon after, maybe 2020.
     
  8. Letail

    Letail

    Joined:
    May 22, 2017
    Posts:
    10
    Just found out this isn't a feature. Please implement it!
     
    tarahugger likes this.
  9. tarahugger

    tarahugger

    Joined:
    Jul 18, 2014
    Posts:
    101
    You've got to be kidding me... We still can't select an enum value from a button click? :mad:
     
  10. Braza

    Braza

    Joined:
    Oct 11, 2013
    Posts:
    86
    Meh... A bump from me. Surprised it is not there.
     
  11. Hassurunous

    Hassurunous

    Joined:
    Apr 6, 2017
    Posts:
    5
    Another humble bump. I realize there are underlying complexities that make this more difficult than just "turn it on", but man it would make sense and would make my life easier.
     
  12. achimmihca

    achimmihca

    Joined:
    Feb 13, 2016
    Posts:
    17
    I agree. Useful feature, old thread, should be implemented. Also note that you can already select enum values for public variables from a drop-down. I would expect the same drop down to select the value for the parameter of a callback function.
     
  13. Zecrah

    Zecrah

    Joined:
    Nov 1, 2016
    Posts:
    1
    +1 for this