Search Unity

  1. We want you to join us at GDC this year! Take a peek at all of the events we will be hosting during the week of GDC.
    Dismiss Notice
  2. Tell us about your experience here and you’ll get early access to the 2018 Game Studios report + more goodies.
    Dismiss Notice
  3. Unity 2017.3 has arrived! Read about it here.
    Dismiss Notice
  4. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  5. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    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:
    448
    2 Years later, it is 2017 and we still can't use enums with UnityEvents. The technology just isn't there yet! :p
     
    Noisecrime likes 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)
     
    Lohoris2 likes 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 ;)
     
    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:
    31
    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,624
    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:
    117
    Almost 3 years later...

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

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,611
    :(
     
    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:
    11
    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:
    909
    Bumped into this issue myself.
    @phil-Unity Any news?
     
  24. TheSkyWolf

    TheSkyWolf

    Joined:
    Feb 27, 2017
    Posts:
    4
    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:
    9
    @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:
    13