1. Unity 5.6 is now released.
    Dismiss Notice
  2. We've introduced thread tags, search within a thread and similar thread search. Read more here.
    Dismiss Notice
  3. 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
  4. 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
  5. The launcher is currently pulling earlier versions of the content packages which may have errors, see post.
    Dismiss Notice
  6. We've released our first Timeline Experimental Preview, our new tool for creating cutscenes and more! To check it out click here.
    Dismiss Notice
  7. Check out all the fixes for 5.5 in patch releases 1 & 2.
    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:
    64
    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:
    680
    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:
    680
    Just type (int)MyEnumBtn.Action = the number
     
  6. XiongGuiYang

    XiongGuiYang

    Joined:
    Sep 5, 2016
    Posts:
    11
    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:
    680
    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:
    362
    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 ;)
     
    eses likes this.
  13. akasurreal

    akasurreal

    Joined:
    Jul 17, 2009
    Posts:
    437
    +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.