Having data virtualization in Unity3d UI panel

419 views Asked by At

I have a UIpanel in my application and i instantiate gameobject in it programmatically during runtime, the problem i have is i have to create more than 1000 gameobjects (sometimes up to 6000) in that panel and that takes a long time freezing the UI and trying it with Coroutines cause some lags as well.
is there any way to have a list of gameobjects and create them in UI panel during user scrolling that panel?
or can somebody suggest a better way of adding gameobjects to a panel without any lag or UI freeze?

UPDATE :
I added this to clear my goal.
i have a long list of items that i trying to put them in a list each item have a picture a some button and some texts, below there is a sample from a single item:

example of single item

now i need to instantiate this complex sample in a panel and each item change itself based on a ID i pass after instantiation.
but i have a panel capacity problem and even if i cut some item when i put that many object in a panel at once scrolling in panel make it very slow and painful
also i need a scroll bar for my panel so i have to have all of my object available in the panel

hope this information helped.

2

There are 2 answers

2
Mark Gossage On BEST ANSWER

When you describe it that way, its much clearer & much simpler.

Look at any mobile application: they do not put 6000 people records on a single page. Its normally paged or grouped, with a search option. So do that instead.

If you look at the facebook mobile app (a good standard) if you start scrolling to look at older posts, every few seconds it pauses for a second or two to load the next few posts. Its the same with the youtube app.

Because of this 65535 unity limit, just put in a paging limit (say 100-200 items).

8
Mark Gossage On

Ok, I just wrote a test program which generated 1000 Text UI elements.

public class MakeLoadsOfUI : MonoBehaviour {

    public GameObject prefab;
    // Use this for initialization
    void Start () 
    {
        MakeUI();

    }

    void MakeUI()
    {
        float startTime = Time.realtimeSinceStartup;
        for (int i = 0; i < 1000; i++)
        {
            int x=i/40, y=i%40;

            GameObject obj = (GameObject)Instantiate(prefab);
            obj.transform.SetParent(transform);   // attack to the canvas
            RectTransform rt = obj.GetComponent<RectTransform>();
            rt.anchoredPosition = new Vector2((x-10)*25, (y-10)*20);
            obj.GetComponent<Text>().text = "UI:" + i;
        }
        float endTime = Time.realtimeSinceStartup;
        Debug.Log("time taken:" + (endTime - startTime));
    }
}

It took 0.15 seconds for 1000 elements which was quite reasonable. If you had a different result please elaborate, explaining how you did it.