Author Topic: UITable Reposition not working with children that have dynamic heights  (Read 7543 times)

Aurigan

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 21
    • View Profile
I'm adding a bunch of table items to a table where each item has a label set to resize height. I then call Reposition on the table and ... the end result is that the items all have incorrect spacing.

I believe the issue is that NGUI needs to calc the labels bounds, and then needs to update the surrounding frames of the labels (sprites set to update relative to the label on Update()) *before* it can know how to correctly space the items for the table.

I've hacked around this issue by running this after having repopulated the table:

  1. IEnumerator FixTable(){
  2.     yield return new WaitForEndOfFrame();
  3.     yield return new WaitForEndOfFrame();
  4.     table.Reposition();
  5. }

(the double frame wait is needed)

So, this works but it's pretty ugly, causes nasty flicker while waiting on the fames to sort out all the positions. Is there any way to not need this? Something like a force reset of all the bounds before calling Reposition() maybe?
« Last Edit: April 28, 2016, 08:25:54 PM by Aurigan »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Try calling NGUITools.ImmediatelyCreateDrawCalls(gameObject), then Reposition, then ImmediatelyCreateDrawCalls again.

Aurigan

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 21
    • View Profile
Try calling NGUITools.ImmediatelyCreateDrawCalls(gameObject), then Reposition, then ImmediatelyCreateDrawCalls again.

Thanks for the reply, sadly this isn't a solution. Using the ScrollView as the gameObject appears to update everything in one frame but the spacing is still wrong. Using the table as the gameObject causes flicker as bounds still work themselves out and the spacing is still wrong afterwards.

edit - I tried super hacking this by calling ImmediatelyCreateDrawCalls on every dynamic height label, then every table item that contained the dynamic height label, then the table then the scrollview ... same results.
« Last Edit: April 29, 2016, 01:23:09 PM by Aurigan »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Simply try changing the UITable's script execution order to be in the end of the script list so that it's updated after the label. Seems to work here.

Aurigan

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 21
    • View Profile
Simply try changing the UITable's script execution order to be in the end of the script list so that it's updated after the label. Seems to work here.

Hrm, this didn't appear to change the bad behavior at all - tried it with and without a bunch of scattered ImmediatelyCreateDrawCalls with no good result.

edit - ok I found a way to hack around this - after adding the item to the table, before calling Reposition(), I'm calling ResetAndUpdateAnchors() in order to force recalc width/height on the cell background sprite (which has anchors set to change on Update() relative to the dynamically sized label).

Without that hack, at the point the table repositions, the label has the right size but the sprite doesn't.

« Last Edit: April 30, 2016, 04:43:24 PM by Aurigan »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
The test I created was this:

1. New scene.
2. Added a game object with UITable on it and changed it to 1 column.
3. Added two labels underneath the object from #2 and changed them to "Resize Height" overflow.
4. Added this script referencing the 2 labels:
  1. using UnityEngine;
  2.  
  3. public class Test : MonoBehaviour
  4. {
  5.         public UILabel label0;
  6.         public UILabel label1;
  7.  
  8.         void Start ()
  9.         {
  10.                 label0.text = "Testing 123, this one time at band camp?";
  11.                 label1.text = "There was this bear? And the bear was hungry?";
  12.         }
  13. }
  14.  
The script order change made it work just fine for me... but if your work-around works, you can stick with it much the same.

Aurigan

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 21
    • View Profile
The issues start happening when you have table items who's sizes are dependent on the dynamic label. For example a label might have a background sprite that is sized to be 10px bigger than the label on all edges.

In this scenario, in order to correctly space the table items the order needs to be :

* update dynamic label size
* update dependent object size
* update table positioning

What actually seems to happen is undefined ... in my project the dependent object size was getting updated after the table repositioning and changing script execution order wasn't altering that behavior.