- Home /
Unity Procedural Game Optimization
I am currently developing a 3d dungeon crawler which implements a procedural dungeon generation system which works perfectly fine. However the game is not very performant. I have tried a few optimization tricks I heard like:
Adding LOD Groups to each room prefab (this works perfectly fine), has impact on fps and batches
Tried addig static batching, I made every room prefab static and enabled dynamic and static batching in the player settings, however there is no change in the batches count or the saved batches count.
Tried addind occlusion culling but i cannot bake it before the game, as it's a procedural generated world.
The room prefabs are low poly rooms and are not even decorated yet, just the floor and walls and torches.
I should mention that i am using deffered lightning and I am activating the torch point lights as the players walks in the room. There aren't any baked lights everything is realtime. Every room prefab uses the same metrial and is not copied when the room is instantiated.
Some screenshots to help you understand better: [1]: https://i.stack.imgur.com/msoBy.png [2]: https://i.stack.imgur.com/vpuTT.png [3]: https://i.stack.imgur.com/WF6Y6.jpg [4]: https://i.stack.imgur.com/S7hQz.jpg
Answer by Riiich · Aug 31, 2021 at 08:10 AM
According to https://docs.unity3d.com/Manual/occlusion-culling-dynamic-gameobjects.html you can add occlusion culling to static objects during runtime (static means it's not moving, even if they're "dynamically generated")
"A dynamic GameObject can be an occludee at runtime, but it cannot be an occluder."
Have you tried setting the prefab to static and seeing if the occulsion system is working at runtime?
Otherwise I'd have a look at Occlusion Areas: https://docs.unity3d.com/Manual/class-OcclusionArea.html
That's what i came across in the first place. I have set all my room prefabs as static but i still need to go to the occlusion inspector tab and bake it. But i haven't got any meshes in the scene to bake. I will take a look at the occlusion area and come back with a response. Thanks
@Tudoraster what about making your own system? I found this Reddit post that might be useful: https://www.reddit.com/r/Unity3D/comments/2g3nw5/occlusion_culling_at_runtimerealtime_tips/
Answer by Llama_w_2Ls · Aug 31, 2021 at 02:19 PM
however there is no change in the batches count or the saved batches count.
If you set a game object to static at runtime, static batching only takes place if you call StaticBatchingUtility.Combine()
on your root object. See here. For example, if all your rooms are under a game object called 'dungeon', call StaticBatchingUtility.Combine on the 'dungeon' game object after all rooms are generated. This will enable static batching and you should see your saved batches count increase, and your performance drastically improve.
Another thing to mention, occlusion culling isn't really possible at runtime. My guess would be it would be slow and costly to calculate in the first place. However, since your dungeons are small interior rooms with small hallways, you could set up a simple occlusion culling system manually, to turn off all rooms if the door is closed, for example. So any rooms with closed doors are deactivated, and if you are inside a room with a closed door, all rooms are deactivated. This might be a lot harder to implement, since you'll have to set things up at runtime, but it seems like the best solution to increase your performance.
Hope that helps
@Llama_w_2Ls I implemented StaticBatchingUtility.Combine() right after the dungeon generation has finished but there is no change in the batches count or the saved by batching count. Maybe it has something to do with the fact that under my dungeon generation gameobject there are empty gameobjects which just hold the room gameobjects?
My hierarchy is structured like this: - Dungeon Generator (Where the generation script is and StaticBatchingUtility.Combine() is called) - MainBranch (just an empty gameobject which hold all the rooms prefabs that take part in the main branch from the spawn room to the exit room) - Room 1 - Room 2 - ....
Ok I found out that I needed to tick write/read on the mesh properties. The batches lowered to around 150 and the saved batches jumped to 44. And when i click on the generated room prefab and look at the mesh filter it says: Combined Mesh (root: Dungeon Generator) 88. Thanks @Llama_w_2Ls. If you have any other suggestions I would really appreciate it.
Answer by Tudoraster · Aug 31, 2021 at 10:21 AM
Ok i found out that the occlusion culling works at runtime if the room prefabs are set to static and it only shows how it work when you click visualize on the occlusion culling window my guess is that this is what is happening when the game is running under the hood. Thanks @Riiich, if you have any other optimization adivce, I would really appreciate it.
@Tudoraster No problem! Let me know if my answer was correct by marking it as the best answer :D
Your answer
Follow this Question
Related Questions
Optimization of Procedurally Generated Game 0 Answers
Best way to instantiate and render a grid of text 0 Answers
How to improve 2D rendering at a distance. 1 Answer
Vertex count and smoothing groups 2 Answers