Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Ability to add enum argument to button functions

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

  1. guicamarotto

    guicamarotto

    Joined:
    Jun 23, 2020
    Posts:
    13
    Hello all, I was looking for an example to how do this enum on click button and came into this thread and want to share my solution.

    First I create this script
    Code (CSharp):
    1. public enum StateList
    2. {
    3.     Move,
    4.     Attack,
    5.     Skill,
    6.     Item,
    7.     Wait
    8. }
    9.  
    10. public class ChooseActionButtons : MonoBehaviour
    11. {
    12.     public StateList stateList;
    13.     protected StateController sm { get { return StateController.instance; } }
    14.  
    15.     public void ActionButtons()
    16.     {
    17.         switch (stateList)
    18.         {
    19.             case StateList.Move:
    20.                 //sm.ChangeTo<MoveTargetState>();
    21.                 break;
    22.             case StateList.Attack:
    23.                 //sm.ChangeTo<ChooseTargetState>();
    24.                 break;
    25.             case StateList.Skill:
    26.                 //sm.ChangeTo<ChooseTargetState>();
    27.                 break;
    28.             case StateList.Item:
    29.                 //sm.ChangeTo<ItemSelectionState>();
    30.                 break;
    31.             default:
    32.                 //sm.ChangeTo<WaitState>();
    33.                 break;
    34.         }
    35.     }
    36.  
    37.  
    38. }
    So I added this script to each button I have, and chose my enum on the script editor. Drag my own button to the click event, and on the event list I chose my script and the method "ActionButtons". and works, I get the enum item chosen on the script editor.

    enumbutton.PNG
     
  2. issac8a

    issac8a

    Joined:
    Aug 29, 2017
    Posts:
    2
    This should make it work, at least in Unity 2019.4 works fine, I just removed those lines and gave directly a new Unity Event(), actually this feature is quite easy to implement, thanks @llamagod for the code, I am using it quite often.
     

    Attached Files:

    JOni likes this.
  3. Isaac_Marovitz

    Isaac_Marovitz

    Joined:
    Jun 30, 2018
    Posts:
    1
    UNITY HOW IS THIS FEATURE STILL MISSING
     
unityunity