You've optimized all of the low-hanging fruits of your game. Except that you didn't. You missed a sneaky, non-so-obvious spot: optimizing your Unity Scene Hierarchy.
[The original entry with its formatting can be found at Unity Optimization: Your Scene Hierarchy is Robbing you Performance]
What's the crack with the hierarchy now?
I want to show you something.
Launch Unity and open your game project. Then, run your game on your target device and attach the Unity Profiler to it. Record a few frames during gameplay.
In the Unity Profiler, look for the following unpleasant profiler markers:
You didn't find any?
Keep looking. I'm sure you'll find at least one.
They'll spawn when you're about to give up. As soon as you mouse over the close button of the Profiler, they'll show up.
Found them already?
If so, it's your lucky day.
I discovered those in my previous project as well and I learned how to land a final blow to them.
I know the evil behind them...
Do you want to know the performance secrets of Unity Scene Hierarchies?
Quick Navigation (opens in a new tab)
I still remember the first Unity project I worked on, just slightly over a decade ago.
I was not sure what the difference between game objects and components were.
But that didn't matter. I created games anyway.
Deep Unity Scene Hierarchy
Was it uncommon to see a messy unity scene hierarchy like this?
Actually, yes. Because it used to be worse than that.
Interestingly enough, you can get away with this kind of hierarchies most of the time.
But you'll suffer. You'll pay the price later on.
Luckily, before I started developing games professionally, I learned to appreciate more structured hierarchies.
A great scene hierarchy structure can be your best friend. Structure makes development and collaboration much easier.
It doesn't have to be the best, whatever that means. It's enough for it to be good.
But bear with me here. This is important.
Having a cluttered unity scene hierarchy is dangerous and will get you into trouble. This is so because it's highly unlikely you'll directly notice its side effects.
There'll be a creepy ghost chasing you, pulling you back, slowing you down. Sometimes you think you hear something, but when you look back, there's nothing. So you get used to walking slower.
I learned to diagnose these side effects over the years. And the main problem is that the side effects of a messy unity scene hierarchy are not obvious to spot.
You'll see the profiler and wonder what some markers mean. Their names are often cryptic... what secrets do they hide?
The only thing you can be sure of is this:
Mysterious markers tell you something's worth investigating
When you and I talk about structuring your scenes better, we usually discuss about increasing clarity and reducing maintenance cost.
But performance? Just like my cat *gitignores *all the toys she has around my flat, everybody forgets about performance when it comes to the Unity Scene Hierarchy.
Let's analyze the previously shown unity scene hierarchy. It contains 2,000 rotating spheres nested on top of each other.
What does the profiler say about it? Look, don't be scared.
That's pretty bad for a simple prefab and a rotation script applied to a built-in sphere.
As you can see, *Physics.SyncColliderTransform *and *UpdateRendererBoundingVolumes *are taking a huge portion of your frame budget.
But let's not stop analyzing there, because I'm quite curious about this case.
Let's dig further. I hope you saved some 4G data this month for this impressive image.
Here lies the key of the article.
Unoptimized hierarchies causes two BIG problems in Unity.
- Deep hierarchies do not allow the job system to work efficiently in parallel.
- Nested hierarchies cause bottlenecks in several subsystems within Unity.
Remember my words...
Because this applies to all types of dynamic objects and especially user interfaces.
In case you're wondering, I own a Threadripper with 16 physical cores and 32 threads. Do you know how many am I using effectively in this project? Just one.
There're so many ways to say the same: the performance of your game will suffer. And by trying to fix this mess by following your gut feeling based on those mysterious names, you'll start to age blazingly faster.
What would a Level 2 Developer do with 2,000 objects?
A Level 2 Unity Developer knows that parenting transforms comes at a price.
The Level 2 Developer also knows this price is paid in milliseconds of CPU time. Or, similarly put, it is paid in hours of overtime spent by the developer chasing this up.
Ask yourself: do you have enough CPU budget to pay for this expensive hierarchy?
From which area of your game are you subtracting those milliseconds from?
Will your game bombard the player with less and less flashy particles? Or will you cut the number of city invaders your player will have to defend from?
Hopefully, you'll do nothing of these two things., but rather...
"Create efficient Scene Hierarchies. Destroy those who aren't so."
Rubén Torres Bonet
The reason complicated parenting steals resources from your game is simple:
Each transform modification affects its children and potentially its parent and siblings
Unity recalculates a lot of thingies for you when you change an object's position, rotation or scale.
This process is transparently done for you, so you can focus on developing your game. That is great, but the price to pay for it becomes astronomical if you're caught off-guard.
Keep it simple, adhere to...
- No more than 50 total child elements per game object
- No more than 4 levels of depth in any game object
Isn't that simple?
A Level 2 Developer will keep their unity scene hierarchies as flat as possible.
Look below. Same amount of objects, this time structured under a flat hierarchy.
Savings are huge, I see.
But doing this is easier said than done, right? Detaching and destroying relation(ships) between objects is a laborious, error-prone and time-intensive task.
What would a Level 3 developer do?
A good Unity developer gets things done.
A great Unity developer gets things done faster, more accurately.
But how is that?
By using tools.
Because the only cheap hierarchy is the empty one, there's one key answer you need to answer at all points from your game...
How complex are your hierarchies?
You can answer this by methodically analyzing your hierarchies and giving it a score.
Below 35: Smells of Trouble. RUN!
Around 50: You might be OK (For now)
Above 70: Profit! (But don't relax)
So what are the math formulas I need to calculate the score?
Luckily, none. I've done this for you.
I prototyped a tool to automatically analyze and detect bottlenecks in your hierarchy based on The Gamedev Guru's Golden Rules of a Clean Hierarchy.
I call this tool...
The Fabulous Advanced Profiling Hierarchy Tool
Run your game. Get to a point of interest. Open the tool and let it do the work for you.
Let the tool *whisper *in your ear the magical melody of an integer that we will call... score.
Once you know what your score is, you know what to do... Or not?
Because I'm not giving you only that tool, but also a bonus script to boost your hierarchy problem-solving techniques.
I'm giving you the extraordinary power of the simple *DetachGameObject.cs *Demigod script.
You're not likely to encounter many 27-line scripts that will help you improving performance so much in that small time frame.
What this does is simple: it'll move your heavy and nested game objects to the root of the scene hierarchy in run-time. That way, you'll profit from having structured hierarchies during development and gain an insane performance advantage during run-time.
Chances are, you can do this for the great part of your hierarchy that is heavily nested for structure's sake.
Claim your free bonuses***** before they become a 404 error* (opens in a new tab)
And after that? Comment below and share your score.