[SOLVED] Identifying the sender of an event in c# events (Applying this with UI buttons)

SETUP:

I have a single script (UIButtonScript) for all of the buttons of the UI system since each button behaves just like the others (they get selected while hovering the button, they trigger when clicked, etc.), so it makes sense to me to just have a single script that can be reused across all the buttons.

WHAT I WANT TO ACCOMPLISH:

Whenever a button is triggered I use a c# event (public static event Action) that is listened to by a ManagerScript that subscribes to these events coming from said button script (UIButtonScript). Now, in this ManagerScript, I want to do different things depending on the button that has been pressed. In order to do that, I’m actually sending a string parameter on the c# event from the button pressed, something like this: (public static event Action MyFunction;) and for emitting the event: (MyFunction?.Invoke(name);). As you can see, I’m passing in the name of the button as a string, so , when listening to the event in the ManagerScript, I can then compare strings with if statements and String.Equals(), and do something specific of said button functionality. This works fine, just as expected. The problem is that comparing strings is not efficient, apparently comparing enums, int or other stuff would be better. Also having a bunch of if statements doesn’t strike me as good code.

QUESTION:

So the question is, from a performance and code cleanliness standpoint, what would be the better way to accomplish this? (by this I mean to know which button has sent the event and then do something custom according to this information). I’ve tried to make a list of the GameObjects with buttons in order to compare GameObjects instead of strings, but that doesn’t strike me as a good solution to this. Also, I tried to store the buttons’ references and assign them a number so that I could compare ints instead, but I didn’t succeed with this (I don’t know yet how to implement interfaces or how to do it correctly). If you have a better idea of how to approach this, I would appreciate it if you could elaborate on it and share it with us all. Thank you!

Apparently, no one uses this so I’m shutting the question down.

Apparently, no one uses this so I’m shutting the question down.

Your UIButton script could have an int field and raise itself in the event, and then your button manager class could listen for when the button is pushed, get the value of the int field, and then use a switch-case table to determine what to do. switch-case are more performant that if-else after like 7 branches or something (the compiler generates a hash table lookup instead of successive comparisons).

public class UIButton
{
    public Action<UIButton> OnClick;
    public int intPayload;
    public string stringPayload;

    public void OnClick()
    {
         OnClick?.Invoke(this);
    } 
}

public class ButtonManager
{
    //subscribe to buttons in Start or something

    public void OnButtonPressed(UIButton button)
    {
         switch (button.intPayload)
         {
              // case 0 ...
              // case 1 ...
             // ....
         }

    }

}