Search Unity

Create list of items as they come into view

Discussion in 'Scripting' started by john-essy, May 13, 2019.

  1. john-essy

    john-essy

    Joined:
    Apr 17, 2011
    Posts:
    464
    Hi there,

    Im here again asking for advice as the last time i did it worked out so well so hoping for more advice.

    I have an app i am creating which reads json from an API, the problem is there are over 2000 items in this list which i have to create entries in my scroll view for. Now in no way do i want to create 2000 items all at once.

    Is there a smart way to create items in this list as the user drags down? I just cant figure out the best way to handle this. Here i have the list in which i control how many items i want to create but i want to only create around 12 at any time and as they come out of view get rid of them, if the user scrolls back up show them again?

    I do have some solutions in my mind but again i want to see other peoples ideas and see if there is a simple smart solution to this.

    Thanks

    Code (CSharp):
    1. public void CreateItems(AllItems items, int maxAmount)
    2.     {
    3.         ClearShopData();
    4.         if (maxAmount < 0)
    5.         {
    6.             for (int i = 0; i < items.listedItems.Count; i++)
    7.             {
    8.                 GameObject newEntry = Instantiate(ItemEntryPrefab, ItemParent);
    9.                 ShopItem newItem = newEntry.GetComponent<ShopItem>();
    10.                 newItem.SetShopItem(items.listedItems[i]);
    11.                 newItem.SetItemName();
    12.                 newItem.SetItemImage();
    13.             }
    14.             return;
    15.         }
    16.         else
    17.         {
    18.             for (int i = maxAmount; i > 0; i--)
    19.             {
    20.                 GameObject newEntry = Instantiate(ItemEntryPrefab, ItemParent);
    21.                 ShopItem newItem = newEntry.GetComponent<ShopItem>();
    22.                 newItem.SetShopItem(items.listedItems[i]);
    23.                 newItem.SetItemName();
    24.                 newItem.SetItemImage();
    25.             }
    26.             maxAmount += maxAmount;
    27.         }
    28.     }
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Perhaps it would help to figuer out why you don't want to create the 2000 items. It is a performance issue, and if so, where's the bottleneck?

    For example, in iOS (and later also in macOS), the scroll view optimizes the view cell. The underlying data structure is present in Memory (i.e. the array or list of items is there), but the view cells, i.e. the 20 cells that fit on-Screen are constantly re-used, which optimizes performance and minimizes GC.

    Since it seems you are also looking at the scroll view Display items, and I assume that you have a continuous list, to display, you can implement a similar pattern where you have a sliding window of n+2 (when you have n slots to display) display items that get updated whenever they go off-Screen. So you only optimize the view parts, but the 2000 items themselves are in-Memory.

    I'm sure that there are complete solutions for this on the asset store, so a few bucks can save you a day of work.
     
  3. john-essy

    john-essy

    Joined:
    Apr 17, 2011
    Posts:
    464
    This does seem like what im after. The reason i dont want to create 2000 items was 2000 GO's as i instantiate them, but with what you are saying i get what you mean about the limit of 20.

    The issue i have right now though is as i am scrolling through the list how would you go into the list of items and pick the right ones that you would want? Something like LoadItem(scrollViewDistance + x) etc?

    Sorry if im not explaining properly here.