Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Any Ideas on how to make this load faster?

Discussion in 'Scripting' started by gononono64, Aug 14, 2019 at 1:19 PM.

  1. gononono64

    gononono64

    Joined:
    Mar 1, 2015
    Posts:
    11
    Hey guys,

    Fairly new to unity, trying to make a card organizer for my friend. I have a list of ~9300 cards which im trying to load into a scene and will have persist through other scenes. I would like to be able to organize the information based on whats in its card properties. The problem I am having is that it takes ages for it to initially load. Do you guys have any ideas on how to speed this up?

    Thank you in advance for any feedback.


    Code (CSharp):
    1.  public void LoadCards()
    2.     {
    3.      
    4.         float posx = 0, posy = 0;
    5.  
    6.         for (int i = 1; i < numberCardsToLoad; i++) //9298
    7.         {
    8.          
    9.        
    10.             GameObject cardGO = Instantiate(cardPrefab); // create gameobject from card prefab
    11.  
    12.             Transform trans = cardGO.GetComponent<Transform>(); //get transform to set card position
    13.             trans.position = new Vector3(posx, posy, 0);
    14.  
    15.             Card cardComp = cardGO.GetComponent<Card>(); //update Card class
    16.  
    17.             string index = i.ToString();
    18.             cardComp.SetCardDataFromIni(index); //uses static ini class to pull card data
    19.             cardComp.SetCardDescriptionFromIni(index); // ^ different ini file to pull description data
    20.  
    21.             cards.Add(cardGO); //Array of all card gameobjects
    22.  
    23.          
    24.             if(posx >= numberCardsPerLineSize) //Setting position
    25.             {
    26.                 posy -= imageSizeY;
    27.                 posx = startXpos;
    28.             }
    29.             else
    30.             {
    31.                 posx += imageSizeX;
    32.             }
    33.  
    34.         }
    35.  
    36.      
    37.     }
    Edit: I should also note that I am pulling images from the resource folder and setting it when SetCardDataFromIni is called.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    4,598
    Do you really need to load 9300 cards up at once?
    Do you really need to populate the images all at once?

    There are things that might be going on in the SetCard calls, but the important thing is why are you doing things all at once for that many objects?
     
  3. Lysander

    Lysander

    Joined:
    Feb 24, 2013
    Posts:
    1,648
    It's almost definitely because of the GameObject instantiations- there are some other optimizations you can do, like setting the list size right off the bat for the 'cards' member (or using an array) so that it's not having to re-size the contents every 4^i iterations, but that's really a micro-optimization. I have to wonder if you actually need 9000 GameObjects in the scene though. If you're using them to store information, instead of just being the visual representation of that information when actually needed, then I'd argue you're doing it wrong.

    Instead, have the cards you instantiate only be the number you need to be visible simultaneously, and assign them to reflect the internal card values you need as you need it. In order words, if you make 5 GameObject cards, those cards can represent literally any in the 9000+ card deck at any time through a simple assignment. That would be several orders of magnitude more efficient.
     
    Suddoha likes this.
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,022
    Move your data from game objects to scriptable objects and load them without additional instantiation. This will be much faster.
     
  5. gononono64

    gononono64

    Joined:
    Mar 1, 2015
    Posts:
    11
    Aka hold x number of cards showing on screen and changing the card data and image as needed. Ill try that
    Thank you

    I cant say i understand it based on the unity api documentation. Ill have to look more into it.
    Thank you
     
  6. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,022
    Watch this
     
  7. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    4,104
    Open the profiler, set it to deep profile, and record what's happening. That'll give a lot better results than us guessing.

    But, yeah, you probably don't want all of the cards loaded at once if you can help it.