Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Instantiating many UI elements is incredibly slow

Discussion in 'UGUI & TextMesh Pro' started by BreadWeek, Oct 15, 2014.

  1. BreadWeek

    BreadWeek

    Joined:
    Aug 12, 2014
    Posts:
    45
    I want to instantiate many items to be placed in a ScrollRect. Each item is a button, has six Panel children, and each of those has a Text child. It's basically a list of people's attributes (Name, Gender, Age, etc). I have a prefab of this item that, within a for loop, I want to instantiate many times as a RectTransform and attach to the ScrollRect's content.

    However, this gets to be very slow. I try to instantiate 100 list items and the game freezes for a couple seconds while it works. This doesn't even include parenting the item to the content panel or finding the item's children to modify text values.

    Is there a different way I should be going about this?
     
  2. JAKJ

    JAKJ

    Joined:
    Aug 17, 2014
    Posts:
    185
    You'd probably have to profile it to figure out which functions are actually taking up the bulk of the time to figure out what's going on. There are way too many moving parts to just sift through logically.
     
  3. BreadWeek

    BreadWeek

    Joined:
    Aug 12, 2014
    Posts:
    45
    There's no extra logic to it, it's simply instantiating these UI elements. The problem is there are just too many objects I guess. And at some point Unity reaches it's vertex limit. I can sort of circumvent these problems by populating the scrollview in a coroutine, and disabling components of list items outside the viewport, but eventually it will get to be too much and the FPS will drop dramatically.

    Is there another method to create large detailed lists in Unity?
     
  4. mog-mog-mog

    mog-mog-mog

    Joined:
    Feb 12, 2014
    Posts:
    266
    Ditto, I'm in the same. A grid with multiple buttons- 100-200. Coroutine is one option for splitting into frames.

    Have you tested your grid on low-end android devices? It appears that dynamic grid/scroll-rect item creation does not work on low-end devices.
     
  5. Foxxis

    Foxxis

    Joined:
    Jun 27, 2006
    Posts:
    1,108
    Instantiating is "slow" for all objects. By your accounts it seems to be even slower for UI elements, which makes sense as there are probably more things going on behind the scenes than for a simple mesh.

    The best way to handle situations when you know you will be instantiating a lot of items is to instantiate a pool of them ahead of time when a pause is not as noticeable, or to create the pool over a second or two.
     
  6. BreadWeek

    BreadWeek

    Joined:
    Aug 12, 2014
    Posts:
    45
    I haven't tested it on Android, but it's slow on my relatively powerful machine so I imagine it won't fare much better on a phone. It does seem like this isn't an easily solvable issue in Unity, especially if you list changes constantly through filters and sorting, as I would like mine to do.

    For now I've put a hard cap on the number of items in my lists, though it's a shame that the knowledge of that cap will dominate my design process as I continue.

    One idea I haven't yet tried is to set the size of the ScrollRect's content manually (so that the scrollbar is correct), and only instantiate list items that would be present given the scrollbar's current value... but I haven't worked out the specifics, or whether that would even be a better method.
     
  7. xucian

    xucian

    Joined:
    Mar 7, 2016
    Posts:
    753
    Last edited: Aug 27, 2016