After i thought i was optimizing my game it turns out it resulted in lower fps which i was not expecting. My game at the moment is a map made up of thousands of game objects which are blocks. below is a script i made thinking it would optimize the game.
for (int i = 0; i < enabledBlocks.Count; i++) {
Destroy(enabledBlocks[i]);
}
enabledBlocks.Clear ();
for (int x = (int)playerPosition.position.x - 23; x < (int)playerPosition.position.x + 25; x++) {
for (int y = (int)playerPosition.position.y - 10; y < (int)playerPosition.position.y + 12; y++) {
if( (x >= 0 && y >= 0) && (x < mapWidth && y < mapHeight) ){
if(backgroundBlocks[x,y] != null){
GameObject tempObject = (GameObject)Instantiate(backgroundBlocks[x,y],new Vector2(x,y),Quaternion.identity);
enabledBlocks.Add(tempObject);
}
if(blocks[x,y] != null){
GameObject tempObject = (GameObject)Instantiate(blocks[x,y],new Vector2(x,y),Quaternion.identity);
enabledBlocks.Add(tempObject);
}
}
}
}
I can see what could of caused this to slow down the game which is the fact that it has to keep creating and deleting the objects every frame, but there’s 32 768 blocks on the map that have to get rendered without this script and only about 1052 blocks get rendered using this script which is 3.2% of the total amount. What is this script doing to cause the game to run much slower then rendering the total amount of blocks, the game objects have these components: sprite renderer, box collider 2d.
rendering is usually quite clever without any assistance, you might want to look into the “culling” techniques used to avoid rendering things that can’t be seen to understand that more fully.
constantly creating and destroying objects in the scene is going to be a huge resource hog, it’s the entire reason object pooling exists as a concept.
Out of curiosity, did you use the profiler to identify where your game was suffering before attempting any optimisation?
I didn’t use the profiler, My game was running fine at about 70 fps or 45-60 when i had this script enabled. I wanted to optimize as much things as possible so it run well with most computers, i decided to start with the rendering.
What do you mean by “rendering is usually quite clever without any assistance” does unity automatically only render whats on screen?
Lol, I’d say you are making the garbage collector work overtime with the constant creation and destruction of objects. To make things worse, you are performing that many distance checks too.
You might have an easier time breaking your environment into chunks and streaming them in and out depending on where the player is on the current chunk.
I believe that’s the “clipping” part in a rendering pipeline, “culling” being things behind other things or facing away… seriously, rendering pipelines are a whole subject in and of themselves and I’ve only paid them passing attention.
Although it comes to mind that we might be using “render” with different degrees of specificity. Unity will still “handle” objects that aren’t on the screen (collisions, scripts, etc.) but they wont be rendered (specifically meaning they won’t get displayed).
Occlusion culling is the process of determining whether or not to render something based on if it’s blocked by something else closer to the camera (occluded). You have to set this up yourself. See here - Unity - Manual: Occlusion culling
So then disabling the sprite renderer component whenever the object is off screen would not effect performance but disabling box colliders when not near the player would improve performance?
You’ll get more mileage out of disabling as many layer interactions as possible from the collision matrix in the Physics settings of your project. We had big performance issues that were completely solved by turning as many layers off as possible in there.
But again - profile your game. If Physics.Simulate() is taking up a bunch of time then go address that.
there was another live training where they were showing how to optimise things using the profiler too, I just remember they shifted some stuff from the main physics processing into the shuriken system and saw huge gains… can’t find it in the list now
If you intend on using that script, you should declare your ‘tempObject’ outside of that function, make it class-level. You’ll also want to cache the position value of ‘playerPosition.position’ in a similar way.
Destroying the array at the start seems very inefficient, you’d be better off managing the items in the array rather than just deleting and rebuilding.
Your use of ‘new Vector2(x,y)’ is not great either, to fix that you’d need to rethink the approach to that loop.
In short, there’s quite a lot wrong in that script that would probably cause performance problems.
Without more details on what exactly you’re trying to do, it’s hard to say how you should be fixing your problem, but I wouldn’t expect that script to be helping much as it is.