Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Ability to add enum argument to button functions

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

  1. llamagod

    llamagod

    Joined:
    Sep 27, 2015
    Posts:
    65
    Well spotted and great fix. Thank you for sharing, I have implemented it and reuploaded the file.
     
    krav_tzar likes 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:
    892
    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:
    2
    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:
    892
    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:
    892
    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:
    544
    2 Years later, it is 2017 and we still can't use enums with UnityEvents. The technology just isn't there yet! :p
     
    rugbbyli and Noisecrime like this.
  9. DaniloB

    DaniloB

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

    Izzy2000

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

    Vatio

    Joined:
    Sep 26, 2012
    Posts:
    8
    @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:
    439
    +1 want this
     
  14. mgeorgedeveloper

    mgeorgedeveloper

    Joined:
    Jul 10, 2012
    Posts:
    51
    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:
    2,703
    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:
    892
    Same goes for Gameobject tags
     
  17. llamagod

    llamagod

    Joined:
    Sep 27, 2015
    Posts:
    65
    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.

    That's why I have released an asset (https://www.assetstore.unity3d.com/en/#!/content/79135) that allows you to serialize stuff in a JSON string automatically inside the class instead. You can make for example your MonoBehaviours inherit from CustomMonoBehaviour to enable the following features (these include automatic serialization of enums (no attribute needed) and more in events):
    • Polymorphism
    • Display properties
    • Display get-only properties
    • Display generic types
    • Dictionaries
    • Polymorphic dictionary keys
    • HashSets
    • Nest lists, arrays and dictionaries within each other to no limit (Multi-dimensional arrays and lists)
    • Display lambda Action delegates with ActionData. Allows up to 4 arguments
    • Display events with EventData
    • Create your own property drawers for these custom serialized variables
    • Enables use of built-in decorator drawers on custom serialized variables
    • Downcast variables to interfaces and abstract types with user-defined default types
    • Manage these custom serialized variables through a CustomSerializedObject in your custom inspectors

    This is obviously more extensive (406 kB) than the workaround I posted earlier, so if that's good enough for you and you just want to enable enum arguments just download that instead (or not, that's your decision). I only recommend my asset if you also want some of the other features too. These features are things that Unity probably won't introduce due to the serialization system as well.

    TL;DR:
    Unity will probably never fix this as well as a lot of other things because of their bad serialization system, so to fix this and said other things including polymorphism and serialization of dictionaries, you can buy my asset (https://www.assetstore.unity3d.com/en/#!/content/79135), or if you just need a fix to this specific thing, download the workaround I posted earlier.
     
    Lohoris2 likes this.
  18. Shizola

    Shizola

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

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

    jister

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

    krav_tzar

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

    skauth

    Joined:
    Jun 4, 2017
    Posts:
    15
    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:
    14
    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:
    925
    Bumped into this issue myself.
    @phil-Unity Any news?
     
  24. TheSkyWolf

    TheSkyWolf

    Joined:
    Feb 27, 2017
    Posts:
    6
    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:
    77
    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:
    19
    @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:
    23
    Damn bump this..
     
    St000 likes this.
  29. derdimi

    derdimi

    Joined:
    Mar 6, 2017
    Posts:
    32
    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:
    4
    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:
    56
    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:
    6
    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:
    12
  40. papercoilgames

    papercoilgames

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

    Sposito

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

    Little_Narwhal_13

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

    Bendixsa

    Joined:
    Sep 25, 2013
    Posts:
    1
    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!