- Home /
Best way to handle multiple buttons?
Hey guys, I am creating a piano type system that will use the keyboard to use notes while playing the game. I do not want to have 10+ if checks to see if a player has pressed a button so is there any way I can make a manager that can handle events like this?
Answer by Inaetaru · Aug 23, 2015 at 06:38 PM
Sorry for not providing complete example code, but this is how I would solve the problem:
Create a prefab (http://docs.unity3d.com/Manual/Prefabs.html) as a template for the button.
In GUI, create horizontal layout group (http://docs.unity3d.com/460/Documentation/Manual/script-HorizontalLayoutGroup.html) as a parent for all these buttons.
Implement the manager (http://blog.codinghorror.com/i-shall-call-it-somethingmanager/) which contains references (http://docs.unity3d.com/ScriptReference/SerializeField.html) for the prefab of the button and parent for button instance (the manager component can be put on the parent itself).
In Awake (https://unity3d.com/learn/tutorials/modules/beginner/scripting/awake-and-start) method I would instantiate the prefab for each button (http://docs.unity3d.com/Manual/InstantiatingPrefabs.html) and change their parents to the parent with layout (http://docs.unity3d.com/ScriptReference/Transform.SetParent.html)
For each instance, you can find the button script (
Button button = instance.GetComponent<Button>()
) and add listener to it's onClick method (http://docs.unity3d.com/460/Documentation/ScriptReference/UI.Button-onClick.html).
Notes:
- If you want to change content of the button, there are multiple ways:
Find the item (for example Text (http://docs.unity3d.com/ScriptReference/UI.Text.html) with GetComponentInChildren (http://docs.unity3d.com/ScriptReference/Component.GetComponentInChildren.html)) and change it
Implement special component for the button which is put on root and have instances for components which needs to change (that's how I would do it)
For adding listener for the button, you can use C#'s anonymous function (https://msdn.microsoft.com/cs-cz/library/Bb882516%28v=VS.120%29.aspx) for example:
public void CreateInstances() { for(int i = 0; i < 10; ++i) { GameObject prefabInstance = Object.Instantiate(buttonPrefab); Button button = prefabInstance.GetComponent<Button>(); button.onClick.AddListener(() => { OnButtonClicked(i); }); } } public void OnButtonClicked(int buttonIndex) { // ... }
Answer by viktorkadza · Sep 08, 2019 at 06:58 PM
for (int i = 0; i <= 20; i++)
{
GameObject button = Instantiate(buttonTemplate);
image = Resources.Load<Sprite>("iconsCPL/"+i.ToString());
texturelist.Add(Resources.Load<Texture>("iconsCPL/" + i.ToString()));
var prefabsimage = button.gameObject.transform.GetChild(0);
prefabsimage.GetComponent<Image>().sprite = image;
button.SetActive(true);
button.GetComponent<ButtonListButt>().SetIndex(i);
button.transform.SetParent(buttonTemplate.transform.parent,false);
button.gameObject.GetComponent<Button>().onClick.AddListener(delegate () { PrintText(button); });
}
}
public void PrintText(GameObject g)
{
float i = g.GetComponent<ButtonListButt>().GetIndex();
int index = g.GetComponent<ButtonListButt>().GetIndex();
i = i / 25;
Your answer
Follow this Question
Related Questions
Interacting with models in AR 0 Answers
Event Trigger For Key Pressed 4 Answers
Detecting moment when GameObiect is created in hierarchy. 1 Answer
Precise Timing 2 Answers
Changing SpriteRenderer color with UnityEvent.Event 1 Answer