Free script: TableLayoutGroup

Hey guys!

Here’s a free TableLayoutGroup script for anyone to use in their projects.

Features:

  • Arrange child RectTransforms in a tabular format with fixed column-widths and flexible row-sizes
  • Rows can optionally grow to fit the maximum preferred size of any of its cells (Yay for cells with multiline text :smile:)
  • Define a minimum height for rows, so the cells aren’t one dimensional (like 70% of all hollywood movies are)
  • Allow for cell arrangement to start from any corner (I’m not entirely sure where this might be useful, but I could see myself scrambling for something like that in the future, so I added that anyway)

Background:
I was working on a table view for one of my database driven projects during which I found that I needed a non-uniform grid layout to represent my data on screen. The closest that I got to achieving what I wanted was a collection of HorizontalLayoutGroups (to hold all the cells) nested inside a VerticalLayoutGroup (to hold all the rows). That wouldn’t work very well for me, since there was no way to enforce column widths or make rows with variable heights. I couldn’t find a good enough solution online (or maybe I didn’t search far enough), so I wrote a TableLayoutGroup component. It works well for me and I figured I might as well share it for anyone else who might need something similar some day.

How to Use:
Pretty much like you would any other layout group, I guess:

  1. Add it to a game object whose children you’d like arranged in a tabular layout.
  2. Set padding, if any
  3. Define column widths (this is what makes the table layout work, so you probably don’t want to skip it)
  4. Define a minimum row height (or input ‘0’ to rely on only the child transform’s preferred widths)
  5. Tick “Flexible Row Height” if you want the row to expand to the height of its tallest cell (Absolutely necessary if you set “Minimum Row Height” to ‘0’)
  6. Enter a “cell spacing” for horizontal spacing between cells (columns)
  7. Enter a “row spacing” for vertical spacing between rows
  8. Done

Note:
I’ve submitted a pull request with the script to the Unity UI Extensions repo managed by @ddreaper, in the hope that it will be added to the collection of useful scripts. This is the reason for the recognizable namespace and header.

Hope someone finds it useful

Cheers! :smile:

Edits:
Update (28/12/2016):

  • Improved performance of SetLayoutHorizontal and SetLayoutVertical
  • Renamed certain variables, removed certain others and refactored code to be more sensible
  • Fixed behaviour for a specific configuration of TableLayoutGroup and ContentSizeFitter

2899276–213474–TableLayoutGroup.cs (9.2 KB)

3 Likes

Added, merged and now released in the UI Extensions Update 1.2 :smile:
Thanks for the contribution @RahulOfTheRamanEffect :smile: