- Home /
random generated map mobile game frame rate lag
So I'm making a mobile 2D survival game where the map is created randomly when you start a new game. The map is created first as two 2d arrays, one for the tiles and one for the values(for example in the tiles array if the value of tilesArray[30,20] is 1 then the tile will be a grass tile, if it is 2 then it will be a mud tile) after making the arrays, the engine loads the tiles. At the start I tried loading the whole map but it took forever to load and it was insanely laggy. after that I changed it so it only loads the tiles surrounding the player but that dropped the fps because the engine kept instantiating and destroying blocks with every step the player takes in the map. I also tried to make it so there are tiles already loaded and ready but far from the camera and when the player changes the position I change the tiles positions with the player instead of creating new tiles but that didn't fix the lag. what is the best way to load the map?
Ps. Sorry for the long question.
Answer by Xarbrough · Feb 23, 2018 at 08:18 PM
Can you post the code? How many tiles are we talking about? I've made a tile-based strategy game for Android and loaded 10,000 tiles at startup without any problems (in a few seconds). I used GameObjects with SpriteRenderers attached and created them in Awake as new objects. Instantiating Prefabs was slightly slower for me, as was loading a scene with already placed prefabs. I assume that Unity's deserialization of a big scene hierarchy is just a little slower than using new GameObject().AddComponent<SpriteRenderer>()
.
The benefit of the SpriteRenderer is the built-in batching (for render performance), also they are relatively fast to create. However, I assume you have some specific bottleneck in your implementation. Besides posting the code, have you done any profiling with the Unity Profiler so far? It can tell you what exactly takes long. But note, that it doesn't profile anything happening in or before Awake (at least the last time I tried), since the Profiler itself is only initialized in Awake (may have changed since, but if you're not getting results try moving your map loading to Start or later).
Basically, there is no best way of loading a map. You need to check all of your requirements (or list all of them here, if you want to hear opinions), but most importantly you have to profile which part of your code takes too long and then you can optimize it. Often it's just a plain mistake or something obvious to fix to speed it up.
Thank you for the advices. I changed it so it loads using chuncks so it doesn't lag that much. creating new gameObjects ins$$anonymous$$d of instantiating prefabs did make it a little better but I'm going to test it more
Your answer
Follow this Question
Related Questions
Unity - dragging a RigidBody2D Gameobject by mouse/touch without any lag 0 Answers
Making 2d joystick correct to controls (mobile) 0 Answers
Problem with Vector3 Distance and Mouse Input, 1 Answer
simple 2D controller for touch screens (arrows left/right/up/down) 1 Answer
How would I Limit the position of the Z axis Transform? 0 Answers