Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Random map fragments - Best Practice

Discussion in 'General Discussion' started by nksG, Feb 20, 2019.

  1. nksG

    nksG

    Joined:
    Aug 20, 2016
    Posts:
    4
    Hello all,

    i'm not sure if this is the right section to post..

    I am developing a mobile game, where it uses an infinite map.
    I have separated the map in fragments(Prefabs), so lets say 1 map(theme based) is consisted by 5 Prefabs, which they include the main "road" and the environment(trees, etc.).

    So far, what i have done, is to place an invisible box collider, and when the player hits that, then i pick randomly a map prefab to continue the gameplay and i destroy the previous fragment.

    So, i always have 2 map prefabs at the same time, the one the player is running and the next one.

    Is this the best practice i can follow?
    Do you have anything else to propose in terms of performance?

    Thank you,
    Nikos
     
  2. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,431
    Instantiation of GameObjects will always come at a cost and can trigger the GC which would lead to a noticeable drop in performance. If you have a finite set of potential fragment prefabs and enough memory bandwidth to keep all of them in memory simultaneously, you should pool them instead. See: https://unity3d.com/learn/tutorials/topics/scripting/object-pooling for more info on object pooling.
     
  3. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,431
    it might make more sense to pool the objects that make up the map fragment instead of the fragments themselves. Then take these objects from the pool as you construct the map fragments based on some configurations that you supply as data, e.g. through ScriptableObjects or based on some algorithm (procedural generation).
    It depends on how much of potential variations on the fragments you have. The more variations, the more likely pooling the objects themselves is the more beneficial approach. Additionally, pooling the objects themselves lends itself better to procedural generation of the fragments.
     
  4. nksG

    nksG

    Joined:
    Aug 20, 2016
    Posts:
    4
    Thank you Martin,

    i will try to follow this implementation and see how it is going.
     
    MartinTilo likes this.