Author Topic: Dynamic button placement and creation  (Read 3427 times)

Paniku

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Dynamic button placement and creation
« on: October 02, 2013, 08:44:07 AM »
I'm making a server browser for a multiplayer a game. Currently, I'm using the default UITextList script to display active servers, however, I need a way to interact with them. Each line (server) in the text list should be able to be double clicked (or have a button placed beside them).

What would be a good way to do this? Is the UITextList too basic to handle this kind of thing? How would I handle possibly dozens of buttons/clickable labels (keeping in mind the server list can be refreshed with new servers appearing and old ones disappearing).

For reference, here's a screenshot of the current (barebones) implementation of the server browser:

Each line in the list (representing a room) should be individually interact-able.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Dynamic button placement and creation
« Reply #1 on: October 02, 2013, 05:19:58 PM »
Best way to do it? Use UITextList as an example (and that's what it is -- an example) to create your own script.

Paniku

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: Dynamic button placement and creation
« Reply #2 on: October 02, 2013, 07:54:28 PM »
I'm just wondering how I'd go about handling each gameobject. UITextList only uses a single label because you can only interact with the label itself (to scroll). However, if I wanted a button beside each listing I'd need to create a new button object for each listing unless I'm missing something.

One way I'm thinking of doing it is, on start, create (and set inactive) X gameobjects with a button component (where X is the number of servers the browser can display) and store it in an array. Then, whenever the server list is updated (which returns an array), set active the matching index in the button array. All other buttons would be set inactive.

Somewhat pseudocode to help visualize what I just said:
  1. private CustomizedUIButton[] buttons;
  2.  
  3. private void Start()
  4. {
  5.         buttons = new CustomizedUIButton[maxServersDisplay];
  6.         for (int i = 0; i < buttons.Length; i++) buttons[i] = MakeAButtonAtTheProperLocation();
  7.         RefreshServers();
  8. }
  9.  
  10. public void RefreshServers()
  11. {
  12.         SetAllInactive(buttons);
  13.        
  14.         Server[] servers = GetServers();
  15.         for (int i = 0; i < servers.Length; i++) buttons[i].SetActive(true);
  16. }

Am I on the right track here or am I completely off the mark? It just seems wrong to make ~50 or so individual button objects, even if they are mostly inactive.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Dynamic button placement and creation
« Reply #3 on: October 03, 2013, 04:16:56 AM »
It's fine, and is technically better performance than creating those objects each time you need to refresh the list. Just use NGUITools.SetActive instead. GameObject's SetActive is bugged and activates objects in the wrong order (children get activated before parents).