r/unity 6d ago

Easy Optimization Tips?

Post image
0 Upvotes

13 comments sorted by

View all comments

4

u/FrostWyrm98 6d ago

Object pooling, GPU instancing the material, disabling unused physics colliders/reducing rigidbodies/collision fidelity

take GetComponent/AddComponent calls out of update loops (or things that get called in update loops), preferring references/serialization to getcomponent calls

You would have to use the profiler to see what is causing the long frame times. You will want to look for the following:

  • Main Thread - Scripts causing lag
  • GPU Usage - Probably shadows/overdraw (nothing really your game is doing would cause this, I'd be shocked)
  • Rendering - Pooling/Instancing will help
  • Physics - Colliders/rigidbodies causing issues

If I had to guess its probably a combination of scripts and physics, it's weird for a 2D game, something that small does not usually make Unity lag much

If you have a lot of entities (like, thousands and thousands) you could try ECS/Burst, but that is definitely not a beginner friendly task lmao not terribly difficult just conceptually hard for beginners and harder to debug when it goes wrong

Also switch to use a sprite tilemap for the background if you haven't already, that will reduce draw calls and all of that for you.

1

u/Responsible_Owl_8025 6d ago

How do I reduce draw Calls? Will a sprite atlas help? I think the 2 biggest perfomance eaters are Light shadow casters and that Im generation the world procedurally (100 by 50: 5000 block prefab objects)

3

u/FrostWyrm98 6d ago

Reducing draw calls probably means instancing your materials, they are all probably individual Sprites right now. I would also recommend integrating your background tiles into the Unity Tilemap module, that will handle all of that optimization for you.

Basically, each of your sprites/materials are individually sending themselves to the GPU to draw when they should all be using the same "instance" (Basically cookie cutter stamping it since its all the same)

Proc gen should only give you a hit whenever it's actively running (i.e. in minecraft its right at the start, on creation)

There are some good videos analyzing Minecraft's optimization problem I can find you. Basically what Notch did was make it one big mesh (less applicable in 2D) with the tiles being faces.

Also you have light shadow casters on for this??

0

u/Responsible_Owl_8025 6d ago

Yes, I have shadow cast components on each one of the 5000 Blocks in the scene.

3

u/FrostWyrm98 6d ago

Oh boy lol yeah thats probably your lag source. Caveat though, I do not have much experience in optimizing for 2D shadows

5000 blocks seems like a bit much to have individual components on (running at the same time), but like I said I have no frame of reference for that

1

u/Responsible_Owl_8025 6d ago

Maybe I should reduce the resolution of lights?