Author Topic: UIGrid dynamic number of rows and columns  (Read 1944 times)

BlueByLiquid

  • Guest
UIGrid dynamic number of rows and columns
« on: May 31, 2013, 01:29:32 AM »
I just picked up NGUI (been looking at it for a long time). I am trying to port something I prototyped in another non-unity framework and I am a fair bit stuck. I was hoping their would be more tutorials of creating the UI entirely in code but I digress.

I need to have a UIGrid that I can populate with sprites and labels (similar to the Example 7) however I will be changing the number of grids in x (which would change how many I can display in the y). For example they might say "show 5 items per screen" then change it to 7 later if they want to see more.

Since I don't see tutorials describing anything remotely dynamic or programmatic (involing object creation and modification) I don't really know where to start other than sifting through the docs at random (Which I am doing and learning a lot). Are there any tutorials (or even some basic guidance) on how to dynamically create and modify objects. I feel like there much be some documentation I am missing as I would expect a UI framework to have other than just the class docs such as how they objects work together, etc. I'm not being negative just trying to get up to speed :)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIGrid dynamic number of rows and columns
« Reply #1 on: May 31, 2013, 01:57:18 PM »
When adding items to a grid, consider creating a single item exactly how you want it to be laid out, then saving it as a prefab. You can then instantiate this prefab repeatedly, thus filling up your grid's contents. For ease of use, create a script on the prefab that will have public references to commonly used components -- labels, sprites, etc. This will let you GetComponent on your newly instantiated prefab instance, and set the values within without having to search for labels and sprites by name.