I’m trying to make a reusable “options” pop-up window. I’m using a Panel that has 5 buttons.
I’m envisioning reusing this UI Element whenever the player has to select different options in the game, like if I give a list of names then the player can click on a name, or a list of responses, or a list of items to pick from.
So I need a function that just returns what button was clicked. I can’t tie a specific function, e.g. “do this action when this button is pressed” to the buttons because I want to be able to reuse the Panel for many different functions depending on the situation.
I’m trying to write a function that will detect which button was clicked by the user and return an int that corresponds to that button. The int will then be used to indicate which option was chosen, and a different function will act on that int.
Something like:
public int selectButton()
{
if (button0 pressed)
return 0;
if (button1 pressed)
return 1;
... .etc
}
I have a UIManager script and populated it with each button in the inspector.
I’m pretty new to coding/unity, and this has me stumped.
I’ve looked at the EventListener stuff, but I’m not sure if that is what I need? Doesn’t the button.OnClick in the inspector already provide that function?
Is there an easier/better way to do this than writing a separate functions for each button?
e.g:
public int clickedButton0()
return 0;
public int clickedButton1()
return 1;
etc...
That will do what I need it to do, but there must be a cleaner way?