# How to load parts of a large tilemap like chunks in Unity 2d?

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 function I use to create clone game object
if (Vector3.Distance(target.position, topLeft) < 25f
|| Vector3.Distance(target.position, topRight) < 25f
|| Vector3.Distance(target.position, bottomLeft) < 25f
|| Vector3.Distance(target.position, bottomRight) < 25f)
{
// My code is currently running "fine", but when it renders the
// map in 1-2s it will drop fps because of the for loop, then it will increase again
xMin = xMin - 1;
xMax = xMax + 1;

yMin = yMin - 1;
yMax = yMax + 1;

GetCircumjacent();
for (int x = xMin; x < xMax; x++)
{
for (int y = yMin; y < yMax; y++)
{
if (x == xMin || y == yMin || x == xMax - 1 || y == yMax - 1)
{
try
{
int mapX = xCenter + x;
int mapY = yCenter + y;

// this is when I get the prefabs from the array prefabs I added
GameObject clone = Instantiate(prefabs[map[mapX, mapY]], this.transform);
clone.transform.position = zero + new Vector3(x, y);
}
catch (Exception e)
{
Debug.Log("Error: Index out of range");
}
}
}
}
}

private void GetCircumjacent()
{
topLeft = zero + new Vector3(xMin, yMax - 1);
topRight = zero + new Vector3(xMax - 1, yMax - 1);

bottomLeft = zero + new Vector3(xMin, yMin);
bottomRight = zero + new Vector3(xMax - 1, yMin);
}

• Your question would be even better if you post the code that you currently use Dec 16, 2020 at 8:15
• oh yeah, i know that, but i'm not at home, i can't upload it, i will upload it, thank you so much, i hope you can help me after i edit it again Dec 16, 2020 at 8:21
• Your diagrams look like a sensible solution to this problem. Where have you run into difficulty implementing the solution you've shown here? Dec 16, 2020 at 11:16
• guys can see my code, i edited it, please see it :( Dec 16, 2020 at 22:52
• I won't look at code on an external site, but you're welcome to include a minimal complete verifiable example of the problem in the question itself. Be sure to explain what the problem in this code is so we can help you fix it. Dec 16, 2020 at 23:06