Author Topic: Populating dynamic lists, best practice  (Read 3880 times)

AeornFlippout

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 46
    • View Profile
Populating dynamic lists, best practice
« on: April 27, 2013, 01:06:57 PM »
Hey all, I'm wondering if anyone has a system for populating lists of data into object instances in UIGrid or UITables?

What I've been doing is something like:
- Make an object a child of a UIGrid/UITable.
- On launch, save a reference to this object as a "prefab", and unparent/hide it.
- Write some code to manually instance this prefab for every item I want in my list.
- Write some code to find the various labels, icon textures, etc. and stuff the values into it.

This seems to work alright, but it feels like there's probably a generic way to handle this?
Maybe there's something built into NGUI that I've missed?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Populating dynamic lists, best practice
« Reply #1 on: April 27, 2013, 04:14:33 PM »
Make a real prefab out of it. That's what I do everywhere. Instantiation script references the actual prefab.

AeornFlippout

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 46
    • View Profile
Re: Populating dynamic lists, best practice
« Reply #2 on: April 27, 2013, 04:24:30 PM »
That's probably good advice; however it doesn't really help with how to actually populate the list with dynamic data.

I'm putting together a helper class for this purpose that I think might be useful for folks. It's not totally complete yet, but here's what I've got so far. Feel free to use it however you'd like:

http://pastebin.com/Y95SVbXT

Here's how it works:

1. attach an NGUI_ListPopulator component to an object that also has a UIGrid or a UITable on it.
2. Setup an item as a child to act as the list item template, and setup your list of keys in the populator's Item Lookup array.
 For instance, if you have a "name" label element in your list item template, add a reference to this with the id "name"
3. Now all your code needs to do is find a reference to the ListPopulator, and pass a list of Hashtables in with the list item data. The populator will take care of instancing objects and assigning your data to the various sub-objects in each list item.

It probably also wouldn't be hard to make a subclass that grabbed the data directly from a json feed or something, for things like highscores etc.

This just seemed a lot nicer to me than having to manually instance every item in a list, when they're all mostly identical.

-Aaron