1. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  2. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  3. We’re giving 2017.1 beta testers a chance to win t-shirts and a Nintendo Switch. Read more on the blog.
    Dismiss Notice
  4. Unity 2017.1 beta is now available for download.
    Dismiss Notice
  5. Unity 5.6 is now released.
    Dismiss Notice
  6. Get further faster with the Unity Plus Accelerator Pack, free for new Unity Plus subscribers for a limited time. Click here for more details.
    Dismiss Notice
  7. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  8. Learn how you'll soon be able to publish your games to China in four simple steps with Xiaomi. Sign up now for early access.
    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.
     
  2. Zapleaf

    Zapleaf

    Joined:
    Feb 22, 2013
    Posts:
    8
    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:
    708
    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:
    708
    Just type (int)MyEnumBtn.Action = the number
     
  6. XiongGuiYang

    XiongGuiYang

    Joined:
    Sep 5, 2016
    Posts:
    12
    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:
    708
    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:
    363
    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:
    47
    more like this? (just for kissUI)
     
  11. Overal

    Overal

    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:
    438
    +1 want this
     
  14. mgeorgedeveloper

    mgeorgedeveloper

    Joined:
    Jul 10, 2012
    Posts:
    12
    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,178
    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:
    708
    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.