Best way to handle multiple buttons? (C#)
Hello, I have a question: What would be the best way to handle multiple buttons?
I am making a directory book. It has 14 tabs(A-B, C-D, E-F, etc.) and on each tab, there are multiple names which effectively are buttons. Each button is activating the same panel but with different text depending on what button was clicked (Name Adrian will open a panel with Adrian text, and name Beatrice would open the same panel but text would be Beatrice). The problem is that this Directory is not going to be populated once, it's going to have different names, and sometimes the same names will open different text depending on a level you are in. I am looking for the best way to handle all the buttons, number of which would be like 100+.
The best thing I did come up with is making huge switch statement and comparing button texts in it. But that's kind of tedious and I don't know how's the performance of such abomination. I was also thinking about making Vertical Layout Group and populating it with buttons, but I can't quite wrap my head around that as I would need separate scripts for each level, also every button will have different text so wouldn't that be as much hassle as switch statement?
Answer by Ryuzaki92 · May 15, 2018 at 06:40 PM
Actually... now when I think of it, it's enough for me to check the name of the button and store it in a string, and then just load text from the file of the same name and assign it to be a text of my panel. 3 lines of code, that's it. Thanks, my rubber duck. I will leave it as it is, maybe it will help someone in the future. I still have to manually make all the buttons, but I think it's fine. Unless someone got any suggestion.
public void OnClickButton()
{
Text panelText = panel.GetComponentInChildren<Text>();
string buttonName = EventSystem.current.currentSelectedGameObject.name;
TextAsset txtAsset = (TextAsset)Resources.Load(buttonName);
panelText.text = txtAsset.text;
if (!panel.activeInHierarchy)
{
panel.SetActive(true);
}
}
Answer by Dwakers92 · Feb 28, 2019 at 11:22 PM
This may help!
https://www.youtube.com/watch?v=2TYLBusJKjc
instantiates buttons, renames them according to your requirement etc. My only issue which ive posted at the moment is ensuring the right button enables the correct game object visible. (see my thread) but i think that will do exactly what you want!
Answer by estani-berlin · May 09 at 03:00 PM
You can add the listener manually and pass the button itself in a delegate. This is what I did for a keypad:
private void Awake()
{
foreach (Button btn in GetComponentsInChildren<Button>())
{
btn.onClick.AddListener(delegate{ ButtonClicked(btn); });
}
}
private void ButtonClicked(Button btn)
{
string btnText = btn.gameObject.GetComponentInChildren<Text>().text;
// ....
}
Your answer
Follow this Question
Related Questions
How To input text from a button? 1 Answer
[resolved] How do I deactivate a button once its height is < 40 in c# 0 Answers
Instantiated Buttons to have separate On-click functions 0 Answers
How to make ui only activate once 1 Answer
Is there a way to detect if a button is pressed if it isn't created programatically? 2 Answers