Search Unity

Optimize large number of sprite objects.

Discussion in 'Scripting' started by Derpnaut, Jun 18, 2018.

  1. Derpnaut

    Derpnaut

    Joined:
    Apr 2, 2017
    Posts:
    9
    Hey, so I'm working on a small game in the style of the sims. So the player can build stuff by clicking and dragging with the mouse and when they do the game shows a preview of how it's going to look (just some lowalpha version of the sprite). This however gets really laggy if the player should choose to build something larger which is understandable since I'm instantiating and deleting a large number of gameobjects (up to thousands if the player wants to go crazy). So my question is if anyone has an idea of how to optimize this without changing the functionallity. I have thought of doing a bit like prison architect does at times and just show a big red or green box with low-alpha (i.e. one gameobject that changes size) but thought it would be more interesting actually showing what's getting built.

    Thanks in advance,
    Derpnaut.
     
    Last edited: Jun 18, 2018
  2. 1Piotrek1

    1Piotrek1

    Joined:
    Mar 14, 2014
    Posts:
    130
    Your problem isn't probably the number of sprites, but instantiating and destroying objects.
    Try using Object Pooling.

    If it's still laggy then consider creating a List of all objects (structs with position and sprite texture) and go through them and draw each on a Texture2D. It's a lot of work but removing all these components should make your script really fast.
    But first try with object pooling, you probably don't need to do more than that (speaking from experience).
     
  3. Derpnaut

    Derpnaut

    Joined:
    Apr 2, 2017
    Posts:
    9
    Thanks for your reply! I've alredy begun toying around with some object pooling and it's a bit laggy, but I think I've found a few optimizations still to be done, if they don't work I'll try your other tip.