Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: Vaei on January 20, 2014, 07:25:07 AM

Title: Dynamically Generated Buttons - UIEventListener (&Passing Args)
Post by: Vaei on January 20, 2014, 07:25:07 AM
Hi there,

I have a loop that generates a button for each instance that I have connected to my server. It's a join button.

What is the proper way to handle this?

I can get the button by:

  1. GameObject button = listing.transform.FindChild("Connect_Button").gameObject;

But of course it wont be stored in an array or anything, which I could do too.

Then I can initialize the UIEventHandler BUT I don't know if I can do this in the loop, or if it has to be in start, or what. AND I need to pass in an argument (the id of the instance) which I'm not sure how to do because they don't take arguments.

Please help :) Thank you.
Title: Re: Dynamically Generated Buttons - UIEventListener (&Passing Args)
Post by: ArenMook on January 20, 2014, 07:34:36 AM
My suggest... never use Find. Ever.

Also, never try to find something on children. Instead have children look up scripts on parents.

Reason being, a parent can exist without a child, but a child cannot exist without a parent.

So... have a script on your children that will either forward events to an appropriate script on the parent (NGUITools.FindInParents is handy!), or call the correct function directly.
Title: Re: Dynamically Generated Buttons - UIEventListener (&Passing Args)
Post by: Vaei on January 20, 2014, 08:04:49 AM
Ah.. of course! Thank you