Author Topic: NGUI Inflate Looping issue  (Read 4047 times)

doncarlos91

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 27
    • View Profile
NGUI Inflate Looping issue
« on: May 17, 2016, 09:08:20 AM »
Hi Guys, I have an issue with my grid items  -  located in UIScrollView -> UIGrid .I have a shop view with dotted buttons (pagination) there are all works good, except one thig - when i`m trying to scroll a little bit fast  - the x positions if items in grid becoming broken and offest becomes very big. but when im touching on scrollpanel area - the positions issue are solving automatically. I have no idea what im doing wrong, any suggestions?

Thanks in advance


here is my snippet of code

  1.  
  2. private bool isUpdatingList = false;
  3.         private float gridCellWidth = -1f;
  4.         private int invisibleCount = 0;
  5.         private readonly int SKIP_COUNT = 3;
  6.         public IEnumerator ItemIsInvisible(int itemNumber) {
  7.                 bool skip = false;
  8.                 if(invisibleCount > 0) {
  9.                         if(invisibleCount < SKIP_COUNT) {
  10.                                 skip = true;
  11.                         }
  12.                         if(scrollPanel.currentMomentum.x < 0) {
  13.                                 invisibleCount = 0;
  14.                         }
  15.                 }
  16.                 else if(invisibleCount < 0) {
  17.                         if(-invisibleCount < SKIP_COUNT) {
  18.                                 skip = true;
  19.                         }
  20.                         if(scrollPanel.currentMomentum.x > 0) {
  21.                                 invisibleCount = 0;
  22.                         }
  23.                 }
  24.                 if(scrollPanel.currentMomentum.x > 0) {
  25.                         invisibleCount++;
  26.                 }
  27.                 else if(scrollPanel.currentMomentum.x < 0) {
  28.                         invisibleCount--;
  29.                 }
  30.                 if(!skipItemIsInvisibleCall && !skip) {
  31.                         if(isUpdatingList) yield return null;
  32.                         isUpdatingList = true;
  33.                         if(dataList.Count > poolSize) {
  34.                                 scrollPanel.UpdatePosition();
  35.                                 if(gridCellWidth == -1f) {
  36.                                         GameObject firstObject = itemsPool[0];
  37.                                         GameObject secondObject = itemsPool[1];
  38.                                         gridCellWidth = secondObject.transform.position.x - firstObject.transform.position.x;
  39.                                 }
  40.                                 if(scrollPanel.currentMomentum.x > 0) {
  41.                                         GameObject firstItem = itemsPool[0];
  42.                                         FlingerItem flingerItem = firstItem.GetComponent<FlingerItem>();
  43.                                         int itemDataIndex = flingerItem.itemDataIndex;
  44.                                         if(itemDataIndex == 0) {
  45.                                                 isUpdatingList = false;
  46.                                                 return false;
  47.                                                 itemDataIndex = dataList.Count;
  48.                                         }
  49.  
  50.                                         int dotIndex = 0;
  51.                                         int j = 0;
  52.                                         while(j < itemDataIndex && dotIndex < dotCount - 1) {
  53.                                                 j+=itemsPerDot;
  54.                                                 if(j < itemDataIndex) {
  55.                                                         dotIndex++;
  56.                                                 }
  57.                                         }
  58.                                         if(dotIndex != curDotIndex) {
  59.                                                 curDotIndex = dotIndex;
  60.                                                 for(int i = 0; i < pagingDots.Count; i++) {
  61.                                                         bool value = (i == curDotIndex);
  62.                                                         pagingDots[i].GetComponent<DotItem>().dotToggleBtn.value = value;
  63.                                                 }
  64.                                         }
  65.  
  66.                                         GameObject lastItem = itemsPool[itemsPool.Count - 1];
  67.                                         Vector2 newPos = firstItem.transform.position;
  68.                                         newPos.x -= gridCellWidth;
  69.                                         lastItem.transform.position = newPos;
  70.                                         lastItem.GetComponent<FlingerItem>().InitFlingerItem(dataList[itemDataIndex - 1]);
  71.                                         lastItem.GetComponent<FlingerItem>().itemDataIndex = itemDataIndex - 1;
  72.  
  73.                                         for(int i = poolSize - 2; i >= 0; i--) {
  74.                                                 itemsPool[i].GetComponent<FlingerItem>().itemNumber++;
  75.                                                 itemsPool[i+1] = itemsPool[i];
  76.                                         }
  77.                                         itemsPool[0] = lastItem;
  78.                                         lastItem.GetComponent<FlingerItem>().itemNumber = 0;
  79.  
  80.                                 }
  81.                                 else if(scrollPanel.currentMomentum.x < 0) {
  82.                                         GameObject lastItem = itemsPool[poolSize - 1];
  83.                                         FlingerItem flingerItem = lastItem.GetComponent<FlingerItem>();
  84.                                         int itemDataIndex = flingerItem.itemDataIndex;
  85.                                         if(itemDataIndex == dataList.Count - 1) {
  86.                                                 isUpdatingList = false;
  87.                                                 return false;
  88.                                                 itemDataIndex = -1;
  89.                                         }
  90.  
  91.                                         int dotIndex = 0;
  92.                                         int j = 0;
  93.                                         while(j < itemDataIndex && dotIndex < dotCount - 1) {
  94.                                                 j+=itemsPerDot;
  95.                                                 if(j < itemDataIndex) {
  96.                                                         dotIndex++;
  97.                                                 }
  98.                                         }
  99.                                         if(dotIndex != curDotIndex) {
  100.                                                 curDotIndex = dotIndex;
  101.                                                 for(int i = 0; i < pagingDots.Count; i++) {
  102.                                                         bool value = (i == curDotIndex);
  103.                                                         pagingDots[i].GetComponent<DotItem>().dotToggleBtn.value = value;
  104.                                                 }
  105.                                         }
  106.  
  107.                                         GameObject firstItem = itemsPool[0];
  108.                                         Vector2 newPos = lastItem.transform.position;
  109.                                         newPos.x += gridCellWidth;
  110.  
  111.                                         firstItem.transform.position = newPos;
  112.                                        
  113.                                         firstItem.GetComponent<FlingerItem>().InitFlingerItem(dataList[itemDataIndex + 1]);
  114.                                         firstItem.GetComponent<FlingerItem>().itemDataIndex = itemDataIndex + 1;
  115.  
  116.                                         for(int i = 1; i < poolSize; i++) {
  117.                                                 itemsPool[i].GetComponent<FlingerItem>().itemNumber--;
  118.                                                 itemsPool[i-1] = itemsPool[i];
  119.                                         }
  120.                                         itemsPool[poolSize - 1] = firstItem;
  121.                                         firstItem.GetComponent<FlingerItem>().itemNumber = poolSize - 1;
  122.                                 }
  123. //                              scrollPanel.ResetPosition();
  124.                                 scrollPanel.UpdatePosition();
  125.                         }
  126.                         isUpdatingList = false;
  127.                 }
  128.         }
  129.  
  130.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI Inflate Looping issue
« Reply #1 on: May 18, 2016, 05:10:15 AM »
All UIGrid does was gives you a way to reposition its children so that they're lined up in a grid-like pattern. I am not sure what your code is supposed to do. Are you trying to create an endless scroll view of items that you populate yourself? If so, there's a dedicated component for that. Check the 14th example that comes with NGUI. Every time an item gets repositioned, you get a callback that lets you set its data. This way components get reused.

doncarlos91

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 27
    • View Profile
Re: NGUI Inflate Looping issue
« Reply #2 on: May 18, 2016, 08:53:38 AM »
Thanks for your reply Aren, i have found the easiest way to do my endless scroll using UIWrapContent script :)
for now this is my structure :
UIScrollView
      UIGrid (attached UIWrapContent script)
                 item
                 item
                 item
                 etc.

and i have about 100 items in grid - the performance and functionality works perfect, but i have one issue and want solve it , When im click on button to open that list of items it takes about 2 seconds to open it :(  - and then it works perfect . Do you have any idea to make items load a little bit quick ?

Thanks in advance

r.pedra

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 20
  • Posts: 131
    • View Profile
Re: NGUI Inflate Looping issue
« Reply #3 on: May 19, 2016, 04:03:22 AM »
You could make a loading indicator and load all the things in a Coroutine. Let's say you load them 10 per 10 (using yield every i %10 == 0).
What I also saw is that if you disable all the content of your prefab before repositioning grid and activate all just after, the reposition is faster.
Your item should look like this:
item
    parent
        content
        content
        content

You disable parent in your prefab and activate it after reposition.
I have lists of 400 items that take 2 or 3 seconds to load almost without freeze on mobile.

doncarlos91

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 27
    • View Profile
Re: NGUI Inflate Looping issue
« Reply #4 on: May 19, 2016, 06:06:38 AM »
thanks for you answer r.pedra, do you mean something like this ?

  1.  
  2. public IEnumerator CreateShopItems() {
  3.        
  4.         for (int i = 0; i < dataList.Count; i++) {
  5.                 yield return new WaitForSeconds(0.1f);                                 
  6.                 GameObject item = NGUITools.AddChild(uiGrid.gameObject, flingerItemPrefab);
  7.                 Debug.Log(Time.deltaTime);
  8.         }
  9. }
  10.  
  11.  

r.pedra

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 20
  • Posts: 131
    • View Profile
Re: NGUI Inflate Looping issue
« Reply #5 on: May 20, 2016, 03:28:29 AM »
More something like that
  1. public IEnumerator CreateShopItems() {
  2.        
  3.         for (int i = 0; i < dataList.Count; i++) {
  4.                 if(i%10 == 0){
  5.                          yield return null;
  6.                 }                                
  7.                 GameObject item = NGUITools.AddChild(uiGrid.gameObject, flingerItemPrefab);
  8.                 Debug.Log(Time.deltaTime);
  9.         }
  10. }

Then you reposition and after repositioning, you activate all the item/parent in a loop.(If you used the hierarchy i gave you)
I tried to activate childrens in a Coroutine but it was slower and I had more freezes than if I make it in one frame.

doncarlos91

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 27
    • View Profile
Re: NGUI Inflate Looping issue
« Reply #6 on: May 20, 2016, 05:16:14 AM »
I have tried like that and it loads perfect the items in grid , Thanks a lot
For now there is one issue regarding repositions items , because for now they loading on the same position , as i see there is now some Repositon method in UIWrapContent class, what do you think?

Thanks

r.pedra

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 20
  • Posts: 131
    • View Profile
Re: NGUI Inflate Looping issue
« Reply #7 on: May 21, 2016, 11:46:10 AM »
I've never used UIWrapContent so I don't know how it works.
Another advice, remember to remove your Debug.Log instructions in loop, especially when there are a lot if items. Debug.Log take a loooooooot of time to execute(Because it spams the log file multiple time a frame in your case)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile