Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
1
Question by Fikule · Jan 08, 2017 at 03:22 PM · uibuttondelegatelisteners

Adding a list of delegates to AddListener

Basically I know you can do the following:

 obj.GetComponent<Button>().onClick.AddListener(delegate {Use;});


What I want to do is use an array of delegates to create multiple listeners on buttons.

something like this:

 List myList<Delegates> = new List<Delegates>();
 
 myList.add(Use);
 myList.add(Destroy);
 myList.add(Etc);
 
 foreach(Delegate delegate in myList
 {
             GameObject obj = (GameObject)Instantiate(MyButton, myTransform);
             obj.GetComponent<Button>().onClick.AddListener(delegate);
 }

Is this possible? The end result is a UI with buttons for each method stored this way on an object. So different objects will have different methods stored and generate a different set of buttons for the UI. If it isn't possible, what would be the best way to achieve this?

Comment
Add comment · Show 1
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 hoekkii · Jan 08, 2017 at 03:40 PM 0
Share

A delegate is sort of list. So you can just use.

 Action myDelegates;
 
 myDelegates += Use;
 myDelegates += Destroy;
 
 Button.onClick.AddListener(myDelegates);
 


Sorry understood the question wrong... Yeah it should work after fixing some syntax stuff, but why shouldn't it?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Fikule · Jan 08, 2017 at 07:07 PM

Got it working. My issue was simply that I wasn't properly using UnityAction ><

Now I have (in UnableObject):

 public List<UnityAction> buttonCommandList = new List<UnityAction>();

 public virtual void CreateUIInterface(Text titleText, GameObject buttonMenu, GameObject toolTipButton, PlayerInteraction playerInteraction)
 {
     Debug.Log("Creating Interface");
     titleText.text = GetName();
     foreach (UnityAction command in buttonCommandList)
     {
         GameObject obj = (GameObject)Instantiate(toolTipButton, buttonMenu.transform);
         obj.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
         obj.GetComponent<Button>().onClick.AddListener(command);
         Debug.Log("command: " + command);
         Debug.Log("obj: " + obj.name);
     }
 }

And in my inheriting class:

 public override void CreateUIInterface(Text titleText, GameObject buttonMenu, GameObject toolTipButton, PlayerInteraction playerInteraction)
 {
     buttonCommandList.Add(delegate { this.Use(1); });
     buttonCommandList.Add(this.Destroy);
     buttonCommandList.Add(this.Switch);

     base.CreateUIInterface(titleText, buttonMenu, toolTipButton, playerInteraction);
 }

All good! :) (Just need to clear the list afterwards ^^)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How am I able to click UI buttons while using the FPScontroller? 1 Answer

How to play different animations with different UI button presses. 1 Answer

How to switch cameras with ui buttons? 0 Answers

find an especific multitouch wich is on a image in the canvas 0 Answers

How to find an UI button by name? 2 Answers


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