Author Topic: Problems with UIGrid.Reposition()  (Read 3414 times)

keop

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
Problems with UIGrid.Reposition()
« on: January 29, 2015, 05:06:45 AM »
Hi,

I'm having some problems repositioning UIGrids in my project. When I finish attaching all my items into the grid I call grid.Reposition() but it doesn't work as expected and it doesn't reposition correctly, it shows moved out where it is supposed to be.

This is my code (called with StartCoroutine):

  1.         private IEnumerator reloadResourceItemsGrid() {
  2.                 if (resourcesGrid.transform.childCount > 0) {
  3.                         while (resourcesGrid.transform.childCount > 0) {
  4.                                 NGUITools.Destroy(resourcesGrid.transform.GetChild(0).gameObject);
  5.                         }
  6.                        
  7.                         yield return new WaitForEndOfFrame ();
  8.                 }
  9.  
  10.                 foreach (ShopResource resource in _resources) {
  11.                         GameObject shopResourceItem = NGUITools.AddChild(resourcesGrid.gameObject, resourceItem);
  12.                         shopResourceItem.GetComponent<ShopResourceItem>().setItem(resource.quantity,
  13.                                                                                   resource.price,
  14.                                                                                   resource.currency);
  15.                 }
  16.                
  17.                 yield return new WaitForEndOfFrame ();
  18.                 resourcesGrid.transform.parent.gameObject.GetComponent<UIScrollView> ().ResetPosition ();
  19.                 yield return new WaitForEndOfFrame ();
  20.                 resourcesGrid.Reposition ();
  21.         }
  22.  

Note 1: I have a UIScrollView as parent of UIGrid.
Note 2: This code is called from the Start method and there is an animation playing to show the panel (I use ActiveAnimation.Play).

What I'm doing wrong?

Thanks for your help.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Problems with UIGrid.Reposition()
« Reply #1 on: January 29, 2015, 01:19:26 PM »
Why are you doing any of those waiting for the end of frame? NGUITools.Destroy takes effect immediately, so does AddChild(), and so does Reposition(). Problem is, you never call Reposition() on the grid until after.

You need to Reposition() the grid before calling ResetPosition() on the scroll view.

keop

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
Re: Problems with UIGrid.Reposition()
« Reply #2 on: February 02, 2015, 03:19:20 AM »
Hi, thanks for the answer, I removed all those WaitForEndOfFrame and changed the order of the grid reposition so the code looks like this now:
  1.         private void reloadResourceItemsGrid() {
  2.                 if (resourcesGrid.transform.childCount > 0) {
  3.                         while (resourcesGrid.transform.childCount > 0) {
  4.                                 NGUITools.Destroy(resourcesGrid.transform.GetChild(0).gameObject);
  5.                         }
  6.                 }
  7.  
  8.                 foreach (ShopResource resource in _resources) {
  9.                         GameObject shopResourceItem = NGUITools.AddChild(resourcesGrid.gameObject, resourceItem);
  10.                         shopResourceItem.GetComponent<ShopResourceItem>().setItem(resource.quantity,
  11.                                                                                   resource.price,
  12.                                                                                   resource.currency);
  13.                 }
  14.  
  15.                 resourcesGrid.Reposition ();
  16.                 resourcesGrid.transform.parent.gameObject.GetComponent<UIScrollView> ().ResetPosition ();
  17.         }
  18.  

But unfortunately I'm still having the visualization problem (see attached screenshots). If I reposition the grid from the inspector (with execute in UIGrid component) it fits OK, but I cant find the proper way to do it within the code.

Please, note that as I said in the first message, I have an animation that shows the panel and maybe it is related with the problem.

Thanks.

PoN

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 4
  • Posts: 111
    • View Profile
Re: Problems with UIGrid.Reposition()
« Reply #3 on: February 02, 2015, 10:03:38 PM »
I would like to agree with author , last time i see wrong behavior of UIGrid reposition, too. In editor i setup UIGrid with elements then do Execute, then i play scene open UIPanel with UIGrid and it looks wrong. don't understand why.
Worked on Doc&DogAge Of Fury 3D. Actually working on WarMach.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Problems with UIGrid.Reposition()
« Reply #4 on: February 03, 2015, 10:58:27 AM »
UIScrollView's reposition uses the bounds of the widgets to calculate the content's bounds. The problem is that newly added widgets don't have their bounds there yet. Newly added widgets don't belong to the scroll view's panel until later. The best way to approach it would be to use NGUITools.ImmediatelyCreateDrawCalls(panelGameObject), or at least calling the widget's Start() functions prior to calling ResetPosition().
  1.     private void reloadResourceItemsGrid() {
  2.         if (resourcesGrid.transform.childCount > 0) {
  3.             while (resourcesGrid.transform.childCount > 0) {
  4.                 NGUITools.Destroy(resourcesGrid.transform.GetChild(0).gameObject);
  5.             }
  6.         }
  7.  
  8.         foreach (ShopResource resource in _resources) {
  9.             GameObject shopResourceItem = NGUITools.AddChild(resourcesGrid.gameObject, resourceItem);
  10.             shopResourceItem.GetComponent<ShopResourceItem>().setItem(resource.quantity,
  11.                                                                       resource.price,
  12.                                                                       resource.currency);
  13.         }
  14.  
  15.         resourcesGrid.Reposition ();
  16.         NGUITools.ExecuteAll<UIWidget>(resourcesGrid.gameObject, "Start");
  17.         resourcesGrid.GetComponentInParent<UIScrollView>().ResetPosition();
  18.     }