Author Topic: [Solved] Centering a UITable  (Read 6520 times)

seandanger

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 2
  • Posts: 32
    • View Profile
    • Bit By Bit Studios
[Solved] Centering a UITable
« on: August 15, 2012, 10:51:48 PM »
After struggling with it for way too long, I've come up with a way to center a UITable that I thought others might find useful.

The problem I had was getting a GameObject with a UITable component to center its contents.  I was adding items dynamically at runtime to the UITable, so manual positioning was not an option.  To solve the problem, I was trying to add the item, then get the dimensions of the object, calculate the center, and then set the position.

What tripped me up was really 2 things:

1.  You have to call UITable.Reposition() yourself.

Easy enough -- it makes total sense that if you add items in the editor and then have to click "Reposition Now" that you would definitely need to call the equivalent in your code after adding items.  Simple mistake I'm blaming on lack of sleep :)

2.  UITable.Reposition() might not (in my case, absolutely never) happens immediately.

Due to the way I was adding items to my UITable, Reposition() was actually setting a flag, exiting early, and waiting for a proper Reposition() to occur later.  Somewhat clear after stepping through the code, but I think it would be nice if the online reference docs included a note about this behavior, as that's what I was going off initially.  The solution I used was to add a delegate to the UITable's onReposition event, then I handle my reposition code.

How about the reposition code?  I went ahead and wrapped it up into a Component I called CenterUITable.  Here's the code if anyone is interested.  You can just drop this on the GameObject your UITable is in and it'll handle the rest.  You can also toggle whether x, y, or both gets centered.

Hope it helps!

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class CenterUITable : MonoBehaviour
  5. {
  6.         public bool centerOnX = true;
  7.         public bool centerOnY = false;
  8.         public GameObject uiTableGameObject;
  9.        
  10.         private UITable _uiTable;
  11.        
  12.         void Start ()
  13.         {
  14.                 // find UITable on this gameObject if not explicitly set
  15.                 if (uiTableGameObject == null)
  16.                         uiTableGameObject = this.gameObject;
  17.                
  18.                 _uiTable = uiTableGameObject.GetComponent<UITable>();
  19.                
  20.                 if (_uiTable == null)
  21.                 {
  22.                         Debug.LogWarning("uiTableGameObject does not have a UITable Component at Start() time!  Destroying.");
  23.                         Destroy(this);
  24.                         return;
  25.                 }
  26.                
  27.                 _uiTable.onReposition += Reposition;
  28.         }
  29.        
  30.         void Reposition()
  31.         {
  32.                 // do the actual repositioning
  33.                 Bounds bounds = NGUIMath.CalculateRelativeWidgetBounds(_uiTable.transform);
  34.                 Vector3 pos = _uiTable.transform.localPosition;
  35.                
  36.                 if (centerOnX)
  37.                         pos.x = -bounds.center.x;
  38.                
  39.                 if (centerOnY)
  40.                         pos.y = -bounds.center.y;
  41.                
  42.                 _uiTable.transform.localPosition = pos;
  43.         }
  44.        
  45.         void OnDestroy()
  46.         {
  47.                 if (_uiTable)
  48.                         _uiTable.onReposition -= Reposition;
  49.         }
  50. }
  51.  

ryan

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 90
    • View Profile
Re: [Solved] Centering a UITable
« Reply #1 on: August 16, 2012, 12:03:52 AM »
Not sure if this was intentional or not, but one important thing to notice is that the onReposition delegate only gets called if the repositioning was triggered by repositionNow.  If you call Reposition() on the table after Start() has been called on it, the delegate won't be called.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: [Solved] Centering a UITable
« Reply #2 on: August 16, 2012, 12:46:08 AM »
I'll change it to be inside the Reposition() function and see if I get any adverse side effects on my end.

ryan

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 90
    • View Profile
Re: [Solved] Centering a UITable
« Reply #3 on: August 16, 2012, 01:42:31 AM »
You don't have any circular references of tables that reposition each other, do you?    :)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: [Solved] Centering a UITable
« Reply #4 on: August 16, 2012, 10:04:15 AM »
I hope not!