Author Topic: Fast scrolling Grid [add and remove row dynamically]  (Read 4852 times)

matteo.piccioni

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 8
    • View Profile
Fast scrolling Grid [add and remove row dynamically]
« on: July 04, 2014, 06:26:03 AM »
Hello,
first of all sorry for my bad english.
I have a list of 2 hundred items, and I need to instantiate only partial content at a time and remove the previous rows as they disappears to user.

Im new to unity, I successfully created a scene with
  1. Main Camera
  2. UI Root
  3.    Scroll View
  4.       Grid
  5.  

I attach 'Manager.cs' to MainCamera, and  at start it load list of items into an array, then add the first 20 items to Grid
This script should also add more rows as the Grid scroll

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class Manager : MonoBehaviour {
  6.  
  7.     public Transform panel;
  8.     public int total = 200;
  9.     public GameObject parent;
  10.  
  11.     public GameObject itemPrefab;
  12.  
  13.     public UIScrollView ScrollView;
  14.     public UIGrid Grid;
  15.  
  16.     public float cellWidth = 150;
  17.     public float cellHeight = 40;
  18.  
  19.     public int bundle = 20;
  20.     public float posY = 0f;
  21.     List<GameObject> preLoadedItemList;
  22.  
  23.     GameObject[] itemList;
  24.  
  25.     // Use this for initialization
  26.     void Start () {
  27.  
  28.         // Preloaded list of items, loaded in some way
  29.         preLoadedItemList = new List<GameObject>();
  30.         for (int i = 0; i<= total; i++) {
  31.             preLoadedItemList.Add(itemPrefab);
  32.         }
  33.  
  34.         // list of visible items
  35.         itemList = new GameObject[total];
  36.  
  37.         // the first 20 visible items
  38.         for(int i = 0; i<= bundle; i++)
  39.         {
  40.             AddItem(i);
  41.         }
  42.     }
  43.  
  44.  
  45.     void AddItem(int seq){
  46.  
  47.         if (seq < 0 || seq >= total) {
  48.             Debug.Log("Exit 1");    
  49.             return;
  50.         }
  51.         if (itemList[seq]) {
  52.             Debug.Log("Exit 2 seq: "+seq);  
  53.             return;
  54.         }
  55.  
  56.         GameObject item = NGUITools.AddChild(parent, preLoadedItemList[seq]);
  57.  
  58.         Grid.GetComponent<UIGrid>().Reposition();
  59.         ScrollView.ResetPosition();
  60.  
  61.         itemList[seq] = item;
  62.     }
  63.  
  64.  
  65.  
  66.     // check if I need to add another item into grid
  67.     void Update ()
  68.     {
  69.         //  Debug.Log ("height panel: "+panel.localPosition.y); // all times 236
  70.  
  71.         /* not working */
  72.         /*
  73.         posY = panel.localPosition.y;
  74.         int pos = Mathf.Abs(Mathf.FloorToInt(posY / cellHeight));
  75.  
  76.         Debug.Log ("POS: "+pos);
  77.  
  78.  
  79.         for (int i = -1; i < bundle; i++)
  80.         {          
  81.             int seq = i + pos;
  82.             AddItem(i + pos);
  83.         }
  84.     */
  85.     }
  86. }
  87.  

I Attach a second script to my prefabs, 'Item.cs'
The rule of Item.cs should be to remove itself (the row) when it disappear from the camera
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Item : MonoBehaviour {
  5.         Transform tr;
  6.         private float min = -400, max = 30;
  7.        
  8.         [HideInInspector]
  9.         public bool on = false;
  10.         Transform panel;
  11.        
  12.         void Start()
  13.         {
  14.                 tr = transform;
  15.                 panel = transform.parent;
  16.         }
  17.        
  18.         void OnOutside()
  19.         {
  20.                 Destroy(gameObject);
  21.         }
  22.        
  23.         void Update()
  24.         {
  25.  
  26.                 Vector3 pos = tr.localPosition;
  27.  
  28.                 if (pos.y > max || pos.y < min)
  29.                 {
  30.                         OnOutside();
  31.                 }
  32.         }
  33. }
  34.  

In the Update method I am not able to calculate when I should add a new row, could someone help me with this problem ?

Any help would be appreciated..

Thanks in advance!!
« Last Edit: July 04, 2014, 10:31:57 AM by matteo.piccioni »


matteo.piccioni

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 8
    • View Profile
Re: Fast scrolling Grid [add and remove row dynamically]
« Reply #2 on: July 06, 2014, 05:05:48 PM »
Unfortunately UIWrapContent is not entirely suitable for my needs
The way it remove and add row is really fast and good,
but I need that at the end of the grid the items will not wrap.

Have you some suggestions ?
Thanks

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Fast scrolling Grid [add and remove row dynamically]
« Reply #3 on: July 07, 2014, 03:23:27 AM »
I've thought about this for a bit, then gone ahead and added this feature to UIWrapContent.

In the next version you will be able to specify the allowed range on the UIWrapContent. For example -5 to 5, and it will limit the scrolling as you'd expect.

matteo.piccioni

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 8
    • View Profile
Re: Fast scrolling Grid [add and remove row dynamically]
« Reply #4 on: July 07, 2014, 03:44:03 AM »
Thanks a lot!
The support you provide is great