Search Unity

How to load parts of a large tilemap like chunks?

Discussion in 'General Discussion' started by Qusdrok, Dec 16, 2020.

Thread Status:
Not open for further replies.
  1. Qusdrok

    Qusdrok

    Joined:
    Jun 28, 2020
    Posts:
    24




    I use more pictures so you can understand my opinion better Here is the video link I recorded



    My code has a problem that I use for to render it and cause it to "lag" for 1-5 seconds, so I need to optimize it, and I found one way is to use chunks, and why I don't use the tilemap and it drops my fps

    this is my code
    using System;using System.Collections.Generic;using System.IO;using UnityE - Pastebin.com

    please help me guys, I really need help :(
     
    Last edited: Dec 17, 2020
  2. Qusdrok

    Qusdrok

    Joined:
    Jun 28, 2020
    Posts:
    24
    please help me guys :(
     
  3. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,780
    Camera is always top down view right?
    You know height.
    You know size of the view field.
    You can calculate mi / max positions in view.
    Based on min max, you can calculate x / y positions of camera edges.
    Knowing x/y, you can calculate index offset for 6our tiles.
    When moving camera, you just remember previous index, or x/y positions, to evaluate direction move.
    And apply changes to visible tiling.
     
    Qusdrok likes this.
  4. Qusdrok

    Qusdrok

    Joined:
    Jun 28, 2020
    Posts:
    24
    Yes, the camera is always viewed from above, but I want to fix my code to make it smoother, because right now I think it's not good
     
  5. Qusdrok

    Qusdrok

    Joined:
    Jun 28, 2020
    Posts:
    24
    please help me guys :( i really need help :( I couldn't find solution in google search :(
     
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,780
    You need to tell us, which part you are struggling with.
    I suggest start from the point, that you detect chunks, which are ahead of camera movement.
    Pick x axis as an example.
    Write on papare, which chunk are you expecting to output and try achieve same via code.
     
  7. Qusdrok

    Qusdrok

    Joined:
    Jun 28, 2020
    Posts:
    24
    Don't you watch my video? I record when it renders the map, it will drop fps and increase again after a few seconds, I need to fix it, and it's in my create map function. And I think I have explained my code ?
     
  8. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,780
    I was on the phone and I missed that part.
    You see, the issue you may have is, that you load too many chunks and too far ahead.
    You should only focus on necessary (nearest) ones.
    You don't need to load whole rows and columns.

    Another thing, I suggest look into Unity Jobs. That may help you put things in parallel threads.
     
  9. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,160
    Why are you doing all this when Unity has a tilemap system already that is dramatically more performant than instantiating every tile at runtime? You have, quite literally, done this in the worst possible way, performance wise. You have dedicated over 200 lines of your code specifically to manually entering map data.

    What you have done is a prime example of inefficient overengineering, where you have implemented a system that is not only near impossible to extend, let alone work with, but also managed in the most performance intensive way short of instantiating an object for each pixel.

    If you want to know what you should do, you need to start at a much simpler level.

    https://learn.unity.com/tutorial/introduction-to-tilemaps
     
  10. Qusdrok

    Qusdrok

    Joined:
    Jun 28, 2020
    Posts:
    24
    oh i'm sorry, i think i have wrong way of solving my problem :(
     
  11. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,780
    There is no need be sorry. You tried bite on hard problem. Which is fine. Valuable experience.
    But you may now consider using existing solutions.
     
    Qusdrok likes this.
  12. Qusdrok

    Qusdrok

    Joined:
    Jun 28, 2020
    Posts:
    24
    thank you so much
     
    Antypodish likes this.
  13. JesusMartinPeregrina

    JesusMartinPeregrina

    Joined:
    Nov 2, 2013
    Posts:
    44
    Hi there guys!
    It looks like this isn't a very old post, so I hope you don't mind if extend it cause I'm on the same road.
    Im using unity tile maps for draw the level and and a json file for saving it. At the moment the json contains just an array where each element stores a vector3 position and a int ID.
    For contains all the tile types I have a collection of scriptable objects in the resources folder. Each of these files data have an ID and a TileBase reference (and it will contains other tile data as physics attributes) .

    For loading I put all that data in a <int, scriptableTile> dictionary, so when I read the json I use the ID for get my scriptable tile and from there set the corresponding TileBase in the right position in my tile map.

    For saving the tile types dictionary is indexed by TileBase, so for each TileBase in my tilemap I get my scritable tile and from there I write the position an Id as json and save it on disk.

    My next step is build the json using the chunks instead of the TileBase collection, a here are my questions:
    1. Is there a better way for storing and loading data so I don't need to load the full map (position and Id collection) in memory but load it chunk by chunk? Note that at this point I'm still not drawing or deleting tiles.
    2. Is there a better way for having my tile type database than a collection of scriptables objects in the resources folder? Is it worth for example to use addressables?
     
Thread Status:
Not open for further replies.