Search Unity

Spawning platforms in an endless runner | performance

Discussion in 'Scripting' started by marvpaul, Sep 15, 2017.

  1. marvpaul

    marvpaul

    Joined:
    May 9, 2016
    Posts:
    32
    I have an endless runner where I've created several platforms (about 50 different prefabs). This platforms are divided into color groups:
    • green - 10 platforms
    • orange - 10 platforms
    • blue - 10 platforms
    • purple - 10 platforms
    • colorful - 10 platforms
    I want to spawn some green platforms at first, then some orange, then some blue platforms ...
    Platforms can repeat, so it could happen that one green platform is spawned 2 or 3 times.
    I don't wanna to mix the colors of the platforms. At first some green platforms, then some orange platforms and no more green platforms ...

    Here's a video of my game to understand what exactly I mean with platforms and color groups:


    I tried to load each platform when my spawning algorithm decided to spawn the platform (each time a platform should spawn I loaded this platform prefab with Resources.load(...)). This solution hasn't good performance. I think basically because of two reasons:
    • The resources to spawn a platform were used at runtime, so there are less resources for other stuff and my FPS was going down --> lags
    • In case a platform is used several times, I loaded it multiple times (instead of loading it one time and reuse it)
    I've tried to use another solution where I load all platforms at the beginning of the game (Before the player can play) via Resources.load(...) and stored them in an array. Each time the spawn algorithm needs a platform, I instantiate the platform via Instantiate(...) from the platform array I loaded at the beginning of the game.
    This gave me some performance increasements during runtime, but loading time at the beginning of the game is to long.

    What can I do to get better loading times (compared to second solution) and a better performance (compared to first solution)?
     
  2. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    The standard solution imo is to have a pool of platforms initialised at the start and simply get one from the pool when it's needed, and then return it to the pool when it's finished with (off screen?). This is what I did for our Top Gear: Race the Stig game.

    Use instantiate to create multiple instances of the prefabs and add them to the pool. Obviously you need references to the prefabs to do this, and currently you're using Resources.Load, but you could have them serialised in some data structure like a list, either on a MonoBehaviour or a ScriptableObject.
     
    marvpaul likes this.
  3. marvpaul

    marvpaul

    Joined:
    May 9, 2016
    Posts:
    32
    What exactly do you mean with serializing in this case?
    I thought about putting all the information of my platforms into an XML / JSON (Position of the cubes, attached script s...), read data from this instead of reading data from the prefabs. But I thought it is to complex to implement ... Also not sure if there is a huge difference in loading time.
     
  4. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    When I say serialised I mean

    Code (CSharp):
    1. public List<Platform> prefabs;
    Or as I prefer

    Code (CSharp):
    1. [SerializeField]
    2. List<Platform> prefabs;
    Either in a MonoBehaviour or a ScriptableObject. You add the prefabs to the list in the Editor. At runtime you go through the list and instantiate multiple instances of each prefab (as many as needed) and put them in a pool ready for use. When I say pool I simply mean some class that keeps track of the instances and knows which ones are being used at any one time.