Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Loading 2D Tilemaps prefabs

Discussion in '2D' started by unity_MNiY_PjuprEN7w, Feb 16, 2020.

  1. unity_MNiY_PjuprEN7w

    unity_MNiY_PjuprEN7w

    Joined:
    Dec 31, 2019
    Posts:
    3
    I am making a 2d game with tilemaps. I'm thinking of having a large world that is always loaded, but not always visible (there should be npc:s in different part of the world that can interract with other objects). As I have understood it by having many tilemap objects unity will cull out those not visible.

    So I have prefab objects that I call zones, they consist of tilemap chunks. One zone is nine chunks, each chunk is a 32x32 tilemap. I load these prefabs and then instantiate them roughly like this:

    Ocean = (GameObject)Resources.Load("Prefab/Zones/ZoneOcean"); // This is only loaded once

    {
    GameObject tmp_Ocean = Instantiate(Ocean);
    m_Zones[x].Add(tmp_Ocean.transform);
    m_Zones[x][y].parent = tmp_Grid;
    } // this is run for each zone to "generate"

    Right now the zones are instantiated in the Start() function.

    The problem I have is that instantiating a few of these prefabs crashes unity when running it. The problem seems to be memory related since the memory usage keeps increasing until the crash occures.

    How can I instantiate large prefabs during the start of the program? Should I make some kind of load function instead of doing this in the Start()? Can this be solved by instantiating them in a coroutine queue? Or will I always have this problem when trying to instantiate the objects in runtime?
     
  2. unity_MNiY_PjuprEN7w

    unity_MNiY_PjuprEN7w

    Joined:
    Dec 31, 2019
    Posts:
    3
  3. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    If your chunk is 32 x 32 it is1024 tiles - and if your one "zone" is 3x3, you will have 9216 tiles in one zone.

    So how many zones of this size you have, and how large textures you are using? Maybe check what your profiler says. And how much memory you have in your system?

    "The problem seems to be memory related since the memory usage keeps increasing until the crash occures."

    In general, if you fill your computers memory you'll run into problems... not much you can do other than not fill your memory. Yet I have no idea if your problem has anything to do with tile maps causing the crash, it might be as well something else.

    You said you are trying to split your world into chunks - so why are you trying to load all the chunks at once? This makes no sense. You could maybe create some sort of system that loads new level chunk when you get close to edge of current chunk, and then unload the previous one when you cross some area in your new chunk.
     
  4. PuppyPolice

    PuppyPolice

    Joined:
    Oct 27, 2017
    Posts:
    116
    Culling does not remove the object from memory, so all those chunks are still going to take up space in your memory even when they are not on screen.
     
  5. unity_MNiY_PjuprEN7w

    unity_MNiY_PjuprEN7w

    Joined:
    Dec 31, 2019
    Posts:
    3
    To clarify. I can have 9 zones in the game, if and only if they are created during compiletime. The problem is creating these zones during run time, not the existance of the zones themselves. As to why they are chunks is because different chunks within the zones will have different properties.
     
  6. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Yet you didn't address the issue of you generating everything at once...
     
  7. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Could you file a bug using the Unity Bug Reporter with your project? If there is a memory issue, we would need to fix it within the Unity Editor.