Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Ability to add enum argument to button functions

Discussion in 'UGUI & TextMesh Pro' started by Slev, Sep 26, 2014.

  1. llamagod

    llamagod

    Joined:
    Sep 27, 2015
    Posts:
    76
    Well spotted and great fix. Thank you for sharing, I have implemented it and reuploaded the file.
     
    Daerst and krav_tzar like this.
  2. Zapleaf

    Zapleaf

    Joined:
    Feb 22, 2013
    Posts:
    9
    I would just like to let the dev team know this is the thread we are sent to when asking this question in google. Would be a wonderful addition.
     
  3. Ironmax

    Ironmax

    Joined:
    May 12, 2015
    Posts:
    890
    Its a small world. I use only enums on my buttons. I am just that nice to my memory.
     
  4. Lhg

    Lhg

    Joined:
    Sep 1, 2014
    Posts:
    3
    Works as expected, thank you very much!

    This should definitely be a Unity built-in feature! =)

    It would be very nice too if we could see the actual Id number in the Editor.
    For instance: "EnumName - EnumID"
     
  5. Ironmax

    Ironmax

    Joined:
    May 12, 2015
    Posts:
    890
    Just type (int)MyEnumBtn.Action = the number
     
  6. XiongGuiYang

    XiongGuiYang

    Joined:
    Sep 5, 2016
    Posts:
    14
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using System;
    5. public class Test_Btn : MonoBehaviour {
    6.     public enum Btn_Name
    7.     {
    8.         Btn_A, Btn_B, Btn_C, Btn_D, Btn_E, Btn_AA, Btn_BB, Btn_CC, Btn_DD, Btn_EE,
    9.     }
    10.  
    11.     private GameObject gm_V;
    12.     private Transform btn_ParentLayout;
    13.     // Use this for initialization
    14.     void Start () {
    15.         gm_V = GameObject.Find("Viewport").gameObject;
    16.         if (gm_V!=null && gm_V.GetComponent<VerticalLayoutGroup>() == null)
    17.         {
    18.             gm_V.AddComponent<VerticalLayoutGroup>();
    19.         }
    20.         btn_ParentLayout = gm_V.transform.GetChild(0);
    21.         GameObject btn_Res = Resources.Load<GameObject>("Prefabs/Btn_");
    22.         Btn_Name[] btnAllEnum = (Btn_Name[]) Enum.GetValues(typeof(Btn_Name));
    23.         for (int i = 0; i < btnAllEnum.Length; i++)
    24.         {
    25.             GameObject gmIns = Instantiate(btn_Res) as GameObject;
    26.             gmIns.transform.SetParent(btn_ParentLayout);
    27.             Button btn_Gm = gmIns.GetComponent<Button>();
    28.             Btn_Name btnType = btnAllEnum[i];
    29.             gmIns.name = btnType.ToString();
    30.             gmIns.GetComponentInChildren<Text>().text = btnType.ToString();
    31.             btn_Gm.onClick.AddListener(delegate () {
    32.  
    33.                 OnClick_BtnEnum(btnType);
    34.             });
    35.         }
    36.     }
    37.  
    38.     void OnClick_BtnEnum(Btn_Name btn_Type)
    39.     {
    40.         Debug.Log("Click BtnEnum??"+ btn_Type);
    41.     }
    42.    
    43. }
    44.  
     

    Attached Files:

  7. Ironmax

    Ironmax

    Joined:
    May 12, 2015
    Posts:
    890
    The hole point with Enums is that they are fast and not string based, the code you just posted uses string in loops, hence the whole point of enum gone..
     
  8. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    1,123
    2 Years later, it is 2017 and we still can't use enums with UnityEvents. The technology just isn't there yet! :p
     
  9. DaniloB

    DaniloB

    Joined:
    Dec 19, 2016
    Posts:
    1
    So will it be added or not ?
     
  10. Izzy2000

    Izzy2000

    Joined:
    Dec 18, 2013
    Posts:
    49
    more like this? (just for kissUI)
     
    dizzy2003 and Lohoris2 like this.
  11. Vatio

    Vatio

    Joined:
    Sep 26, 2012
    Posts:
    11
    @Tim-C @phil-Unity Enums with UI events seem like something that should have been done long ago. Is it still on the TODO list? Any plans to implement it in the near future?
     
  12. solkar

    solkar

    Joined:
    Aug 15, 2012
    Posts:
    38
    This must be in the TODO list right after nested prefabs ;)
     
    BBIT-SOLUTIONS, Schneider21 and eses like this.
  13. akasurreal

    akasurreal

    Joined:
    Jul 17, 2009
    Posts:
    442
    +1 want this
     
  14. mgeorgedeveloper

    mgeorgedeveloper

    Joined:
    Jul 10, 2012
    Posts:
    317
    Yes please - the whole point of enum is type safety and to reduce the possibility of introducing errors due to typos or just not remembering that 1 = Dog and 2 = Cat. That's why we don't have strings "Monday", "Tuesday" and "Wednesday" in our code - but instead use named int values, i.e. enums.

    I don't want to pass strings or ints to OnClick events and then cast them to the typesafe enum. This is just asking for errors to creep in.
     
  15. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Obligatory monthly bump of this thread, I guess, but worth it all the same. Enum params on UGUI button events are a must. And frankly, with the number of custom inspector assets that are out there that are able to introduce things Unity doesn't include by default, this doesn't seem like something that requires a grand plan or anything.
     
  16. Ironmax

    Ironmax

    Joined:
    May 12, 2015
    Posts:
    890
    Same goes for Gameobject tags
     
  17. llamagod

    llamagod

    Joined:
    Sep 27, 2015
    Posts:
    76
    People think this is really easy to fix, but the problem lies deeper and this is one of the symptoms. The problem is Unity's God-awful serialization system that doesn't allow polymorphism. For example not being able to serialize a Giraffe or Elephant type in an Animal type field. It gets the job done for most unprofessional users, but if you look into the .dll files you can see all the terrible ways Unity tries to get around the issue. For example, in order to serialize a small range of simple types like integers, floats, strings, etc. in event arguments it has a field for each type! What a waste of space.

    So why can't it just add an Enum typed field?
    Because the Unity serialization system doesn't support polymorphism, meaning it would have to have a field for each enum type, which is obviously not a viable solution at all, so they just didn't include that feature. The best you can do without an extensive rehaul is probably the solution I posted earlier. Make a method with an integer argument and include the type of the enum in an attribute above the method, then the editor can display it in the inspector as that type.

    If Unity wants to fix this issue they will have to redo the serialization system, which they apparently have no intention of doing, so they just have their people run around and say "they'll get to it". It requires too much time to redo it and for what? A determined minority that is easily ignored. Instead, they want to work on bigger features for their main userbase and fixing bugs.

    I posted a workaround earlier that you can use.

    TL;DR:
    Unity will probably never fix this as well as a lot of other things because of their bad serialization system, but you can use the workaround I posted earlier.
     
    Last edited: Dec 10, 2018
  18. Shizola

    Shizola

    Joined:
    Jun 29, 2014
    Posts:
    470
    Almost 3 years later...

    I guess no one is working on this anymore.
     
  19. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    :(
     
    Schneider21 likes this.
  20. krav_tzar

    krav_tzar

    Joined:
    Sep 28, 2015
    Posts:
    4
    You guys rock! Thanks!
     
  21. skauth

    skauth

    Joined:
    Jun 4, 2017
    Posts:
    17
    As much as I would absolutely love to see this (This is a +1 on the feature request), I can see the difficulty here. Enums can be flag enums. And anyone who works with those knows that Unity doesn't implement them very well and we have to run to special property drawers and stuff. So... Unity, please figure out how to work with enums. It's been years. Enums are a good thing. Please include support for them by default without having to install extra editor classes just to see them.
     
    Noisecrime and Shizola like this.
  22. KingRecycle

    KingRecycle

    Joined:
    Jul 20, 2013
    Posts:
    26
    Well, if what @llamagod says is true then we may never see it or if we do it'll be when they feel like doing stuff that is on the back-burner. I am going to make a post here as I also want this feature and want to keep attention on this.
     
  23. Devil_Inside

    Devil_Inside

    Joined:
    Nov 19, 2012
    Posts:
    1,117
    Bumped into this issue myself.
    @phil-Unity Any news?
     
  24. Aquelas

    Aquelas

    Joined:
    Feb 27, 2017
    Posts:
    12
    Shame on Unity. 3 years and there is still nothing... I hope Unreal Engine brings C# support with a good documentation and mobile optimization. Then everybody will go for UE4. I am tired of dealing with Unity's problems. Everything about Unity is problem. The only thing why most people is using Unity is that it has no real alternative. I hope we will see one.
     
  25. TrentSterling

    TrentSterling

    Joined:
    Jan 4, 2013
    Posts:
    99
    Glad I found the fixes in this thread. A shame this isnt baked into Unity yet.
     
  26. neilmanuell

    neilmanuell

    Joined:
    Nov 11, 2015
    Posts:
    2
    Except although the actual fix for this is simple, Implementing the fix is not, its a hack that circumvents the compiled code, is imperfect (can't use in EventTriggers) and not future proof.

    The simplest "proper" solution is for Unity to add this to their code base
     
  27. AdamBL

    AdamBL

    Joined:
    Oct 27, 2016
    Posts:
    29
    @llamagod I had to add:

    using System;
    using UnityEngine;

    to the EnumActionAttribute script. Is that expected?
     
  28. daniel-griffiths

    daniel-griffiths

    Joined:
    Jun 14, 2016
    Posts:
    27
    Damn bump this..
     
    St000 likes this.
  29. derdimi

    derdimi

    Joined:
    Mar 6, 2017
    Posts:
    33
    Could we please get any update form a developer?
     
    St000 likes this.
  30. Alk

    Alk

    Joined:
    Jun 11, 2014
    Posts:
    1

    This is a very nice solution, thanks!

    One thing though, how can we also make it work for toggles?
     
  31. samuelchou

    samuelchou

    Joined:
    May 24, 2017
    Posts:
    1
    Just found this old post and I have to say, sir your script works f**king awesome!!!! Nice job man!!!!
    (And Unity still doesn't provide this feature in 2017.... guess there're really some big problems fixing this)

    also, as @AdamBL mentioned, if you want to use @llamagod 's script in a quick way, you have to add
    Code (csharp):
    1.  
    2. using System;
    3. using UnityEngine;
    4.  
    in EnumActionAttribute.cs
     
  32. Stamblew

    Stamblew

    Joined:
    Sep 3, 2014
    Posts:
    22
    I am currently facing this same old problem. Even though Unity serialization system has this flaw according to @llamagod , they must fix this ASAP.
     
  33. ForgottenCheese

    ForgottenCheese

    Joined:
    Dec 1, 2013
    Posts:
    40
    +1 This would be nice
     
  34. Whoaa

    Whoaa

    Joined:
    Apr 21, 2015
    Posts:
    8
    Seriosuly, we need an explanation...
     
  35. TomPo

    TomPo

    Joined:
    Nov 30, 2013
    Posts:
    86
    Almost 3 years later and still not implemented ? :/
    It's like passing int but instead of raw int we are passing int value from enum list.
    It can't be so hard to implement dear Unity team that it takes so long and still nothing :(
     
    dizzy2003 likes this.
  36. weitay

    weitay

    Joined:
    Aug 5, 2012
    Posts:
    10
  37. StrongCube

    StrongCube

    Joined:
    Nov 22, 2016
    Posts:
    50
    3 years later, not implemented
     
  38. Kithis

    Kithis

    Joined:
    Aug 5, 2018
    Posts:
    1
    +1, ran into this myself, going to be a real headache to work around
     
  39. AlCaTrAzzGames

    AlCaTrAzzGames

    Joined:
    May 17, 2017
    Posts:
    25
  40. papercoilgames

    papercoilgames

    Joined:
    Mar 8, 2016
    Posts:
    1
    Isn't 4 YEARS enough time Phil?
     
    VirtualPierogi likes this.
  41. Sposito

    Sposito

    Joined:
    May 4, 2014
    Posts:
    16
    I coming to the same thread 3 years later and nothing has changed. Impressive!
     
    VirtualPierogi likes this.
  42. Little_Narwhal_13

    Little_Narwhal_13

    Joined:
    Feb 6, 2017
    Posts:
    15
    This is definitely needed, it's simple, and basically everyone wants it...
     
  43. Bendixsa

    Bendixsa

    Joined:
    Sep 25, 2013
    Posts:
    5
    I put a button down on a form today and wanted to pass an enum through on its event.

    Found out I cannot do it.
    Googled it, saw people from 2014 saying: "HEY we want this"
    Saw devs' say "We shall do it!"

    RIP 2018.

    For all the shade you guys throw on enums in the docs and tuts over the years, it's pretty sad that this is not possible
    and that you guys don't reply or give updates on when it will be done.

    We are all developers on one level or another on here, so don't be afraid to say if it's hard
    THEN SAY listen guys: This gonna be hard but we will do it, or screw it too hard wont do it
    with a bit of an explanation. don't just go AFK. ffs.



    Now excuse me while i go apply an UGLY ASF BODGE to my button which can't use enums.
    YOU BASTARDS!
     
  44. lloydv

    lloydv

    Joined:
    Sep 15, 2015
    Posts:
    55
    My workaround: added a super-simple script that holds the enum as a public property, then I pass that component reference to onclick and pull the enum out in the handler.

    Gotta admit, it's kinda silly we can set components as on click parameters but not enums.

    Also gotta admit unity framework devs probably have more important things on their plate, so meh. whatever.
     
  45. dfjhde

    dfjhde

    Joined:
    May 23, 2015
    Posts:
    8
    easiest workaround I can ever think of:
    we can use ScriptableObject:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [CreateAssetMenu(fileName = "EnumTeam", menuName = "EnumTeam")]
    4. public class EnumTeam : ScriptableObject {
    5.  
    6.     public Team t;
    7. }
    8.  

     

    Attached Files:

    Noisecrime likes this.
  46. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    I agree, sorry really have dropped the ball on the forums. Its not as easy as people think it should be. I've attempted and failed 2-3 times over the years to add this but hopefully we can do it just dont know how atm. We are also getting requests internally for it so you're not alone.
     
  47. mptp

    mptp

    Joined:
    May 30, 2014
    Posts:
    29
    Thanks for the transparency Phil! Will keep an eye out for this :)
     
  48. DualSlash

    DualSlash

    Joined:
    Nov 27, 2018
    Posts:
    2
    Thank you for the update, greatly appreciate it.

    Definitely a feature that supports good coding practices all around, but I can completely understand that adding complex data structures in the Unity UI is quite challenging. Please keep us updated if you manage to add the feature.
     
  49. anatolyV

    anatolyV

    Joined:
    Nov 29, 2014
    Posts:
    63
    @llamagod Unfortunately there's an annoying quirk with your code - when selecting multiple buttons with an enum UnityEvent, they all set to the same int internally.
     
  50. llamagod

    llamagod

    Joined:
    Sep 27, 2015
    Posts:
    76
    I have replaced the files in my original post to use the namespaces required in EnumActionAttribute.cs as mentioned by @AdamBL and @samuelchou

    I made a very small change to UnityEventDrawer.cs to fix the problem you describe. I just disabled multi-editing since it's not particularly useful in this case and there are a lot of caveats to enable multi-editing that could break a lot of stuff.
     
    anatolyV and AdamBL like this.