2
\$\begingroup\$

Nested hierarchies cause a performance overhead when they include moving objects.

Every time a GameObject moves, altering its transform, we need to let every game system that might care about this know. Rendering, physics, and every parent and child of the GameObject needs to be informed of this movement in order to match. As the scope of a game increases, and the number of game objects skyrockets, just the overhead of sending out all these messages can become a performance problem.

However, nested hierarchies are useful in organising a scene and keeping logical groups of gameobjects separate.

What options exist to save performance whilst keeping the hierarchy? Is there any pre-existing solution to flatten a hierarchy when building only?

\$\endgroup\$
2
\$\begingroup\$

First of all, hierarchies structured for organizational purposes should have minimal impact. I believe dirtyness (nescessity to recalculate position, rotation or scale) messages travel up the hierarchy for every child that is dirtied (changed) in a normal or fixed frame/tick, but dirtyness only travels back down to other siblings if a/the parents get dirty, which shouldn't be the case for organizational objects acting like folders.

This means that you should not worry about this until 1) your game actually needs optimization, and 2) other more relevant bottlenecks have already been optimized. Otherwise you're engaging in premature optimization.


As for flattenning the hierarchy, one easy solution is to simply have a script that unparents every child of the object on Awake() and then destroyes the object itself. And then plug that script in every organizational object. This will probably incur a small performance hit on the first frame after a scene is loaded, but it's negligible, can be considered part of loading that scene, and still elimitanes the continuous performance hit that the hierarchy would cause (even though that in itself should be negligible, as I explained).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.