Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jmgek · Aug 23, 2015 at 05:07 PM · event

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2

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:

  1. Create a prefab (http://docs.unity3d.com/Manual/Prefabs.html) as a template for the button.

  2. In GUI, create horizontal layout group (http://docs.unity3d.com/460/Documentation/Manual/script-HorizontalLayoutGroup.html) as a parent for all these buttons.

  3. 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).

  4. 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)

  5. 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)
         {
           // ...
         }
    
    
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges