Search Unity

Build very slow at startup

Discussion in 'Editor & General Support' started by UMSDev, Sep 9, 2020.

  1. UMSDev

    UMSDev

    Joined:
    Dec 17, 2017
    Posts:
    24
    Hi,

    Unity 2019.3.15f1

    I'm making a 3d sport game where my main scene contains my UI and my empty arena (floor + goals, low poly models).

    I also instantiate a lot of objects at startup to create a pool a 3d objects to be used later so there is almost no loading time since i'm going to reuse all the objects all the time:

    Code (CSharp):
    1. void Start () {
    2.         StartCoroutine(this.initPools());
    3.     }
    4. private IEnumerator initPools()
    5.     { // let's wait for a few frames
    6.  
    7.         yield return null;
    8.         yield return new WaitForSeconds(0.2f);
    9.  
    10.         this.poolDictionary = new Dictionary<string, ObjectPoolItem>();
    11.  
    12.  
    13.         int nbPooled = 0;
    14.         foreach (var pool in pools)
    15.         {
    16.  
    17.             Queue<GameObject> queue = new Queue<GameObject>();
    18.             for (int i = 0; i < pool.amountToPool; i++)
    19.             {
    20.                 GameObject obj = (GameObject)Instantiate(pool.objectToPool);
    21.                 obj.transform.SetParent(this.transform);
    22.                 obj.tag = tagPooledObject+ pool.typeObject.ToString();
    23.                 obj.SetActive(false);
    24.                 queue.Enqueue(obj);
    25.             }
    26.             pool.ListPooledObjects = queue;
    27.             this.poolDictionary.Add(pool.typeObject.ToString(), pool);
    28.  
    29.             nbPooled += pool.amountToPool;
    30.             if (nbPooled > 1000)
    31.             {
    32.                 nbPooled = 0;
    33.                 yield return null;
    34.             }
    35.         }
    36.  
    37.     }
    It works very well in the editor but I experience a "slow" start with the built game (windows/mac) (I guess some objects are already loaded in the editor which explains why playing is fast) so I was wondering if I was doing anything wrong with my approach or which tools I could use to figure out what actually takes time when loading the game (black screen that could freeze the game for like 5-10 s).

    My pooled objects could be small 3D assets, more detailed 3D objects (just a few of those) or UI components (buttons to be used in tables for instance) so there could be quite a lot of components. Delaying the load doesn't really seem to make the game start much faster but maybe I haven't delayed enough or I should put more yield in between instantiations. I thought that putting my coroutine in the Start and wait for a few frames would help but it doesn't seem to really improve the start.

    I guess it would just be easier if there is a tool somewhere that I could use to see in detail what is slow with the built version of the game.

    Thank you for your help!
     
  2. DiegoDePalacio

    DiegoDePalacio

    Unity Technologies

    Joined:
    Oct 28, 2009
    Posts:
    507
    Hi @UMSDev,

    The tool that you're looking for is the Unity Profiler: https://docs.unity3d.com/Manual/Profiler.html

    In your specific case and only if you find (using the Profiler) that your bottleneck is filling up your pools, what you can also do is to use an overlay image on the top of your scene with some loading message that you can remove once all of your pools are completely populated.


    Good luck with it!
     
    MartinTilo likes this.