Author Topic: RepositionNow has no effect in code, but does work on selection in editor?  (Read 7705 times)

Tiktaalik

  • Newbie
  • *
  • Thank You
  • -Given: 7
  • -Receive: 0
  • Posts: 39
    • View Profile
Hi,

I'm trying to dynamically create a bunch of items and add them to a UITable. I'm seeing an issue right now where my items are correctly added, but they're stacked on top of each other and in the wrong position. After I've added my items I tell the table repositionNow = true so that in the late update it'll reposition, but it's not having any effect. After the fact if I select the UITable in the editor and manually select repositionNow then everything repositions as expected.

Does instantiating a prefab and adding it to the table take so long that I have to wait beyond the LateUpdate? What may be some things I could be doing wrong or in the wrong order that could cause this not to work?

I have the following bit of code being called. The Table object is a UITable.

  1. foreach (var inventoryWidgetData in inventoryData.InventoryWidgetDataCollection)
  2.                 {
  3.                        GameObject item = NGUITools.AddChild(Table.gameObject, ItemPrefab);
  4.                         NGUITools.AddWidgetCollider(item);
  5.                         item.AddComponent<UIDragPanelContents>();
  6.  
  7.                         InventoryWidget w = item as InventoryWidget;
  8.                         if (w != null)
  9.                         {
  10.                                 w.Populate(inventoryWidgetData);
  11.                         }
  12.                 }
  13. Table.repositionNow = true;
  14.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: RepositionNow has no effect in code, but does work on selection in editor?
« Reply #1 on: November 19, 2013, 08:24:08 PM »
To do it immediately, call the Reposition() function.

Tiktaalik

  • Newbie
  • *
  • Thank You
  • -Given: 7
  • -Receive: 0
  • Posts: 39
    • View Profile
Re: RepositionNow has no effect in code, but does work on selection in editor?
« Reply #2 on: November 20, 2013, 03:08:34 PM »
Well great simply adding Reposition() at the end of that foreach loop fixed things.

What I don't understand is why that made any difference. After all,  repositionNow = true will still call Reposition(), just later on. Why is it that calling Reposition() in LateUpdate() doesn't work, but calling it immediately does?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: RepositionNow has no effect in code, but does work on selection in editor?
« Reply #3 on: November 20, 2013, 03:12:43 PM »
Setting the flag delays the operation until next frame. It will also not work if the table script is disabled.

Tiktaalik

  • Newbie
  • *
  • Thank You
  • -Given: 7
  • -Receive: 0
  • Posts: 39
    • View Profile
Re: RepositionNow has no effect in code, but does work on selection in editor?
« Reply #4 on: November 20, 2013, 05:00:11 PM »
I am getting inconsistent results.

The first time I run this, calling UITable.Reposition() immediately after creating my gameObjects and adding them as children of the UITable it looks correct. If destroy these items and I disable the screen and then go through the same process again then the UITable will now be off in the top left corner. This is shown in the below image. If I manually select "Reposition Now" in the editor then everything will move to its expected position. I know that UITable.Reposition() is being called in code as I have a print statement on that function in the NGUI codebase and so I'm left wondering why this function sometimes works and why it sometimes doesn't and if there are any timing issues or other things I am unaware of that can prevent its correct operation?

I'm hoping you can provide some clarity on the best practices of how and when UITable.Reposition() should be called to get the correct results when populating a UITable with dynamically created GameObjects. In addition if there are additional functions that should be called in a certain order to get the correct effect (e.g.. UIDraggablePanel.ResetPosition()) I would appreciate clarity on what other functions need to be called.

I am assuming from the documentation of UIDraggablePanel.ResetPosition() that the transform position of the clipping panel should be set such that it is the same position as the desired top left corner of the UITable?


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: RepositionNow has no effect in code, but does work on selection in editor?
« Reply #5 on: November 20, 2013, 07:30:47 PM »
Draggable panel repositioning was changed in 3.0.6 (eliminated actually). I'd suggest trying it again after the update goes live -- chances are the issue you were having will be gone altogether.

Tiktaalik

  • Newbie
  • *
  • Thank You
  • -Given: 7
  • -Receive: 0
  • Posts: 39
    • View Profile
Re: RepositionNow has no effect in code, but does work on selection in editor?
« Reply #6 on: November 26, 2013, 05:18:07 PM »
A coworker figured out what our problem ended up being. The panel we were trying to populate with data was scaled to 0,0,0 at the time where we were trying to add items to the UITable. Changing it so that it was scaled very small (0.1, 0.1, 0.1) fixed the issue.