I’ve been wondering… I’m using gameobject hierarchy (parenting gameobjects to other gameobjects) to organize the structure of my scene, the thing is getting quite complex and I wonder what impact that may have on performance.
When an object is parented to another, it will inherit the coordinates from its parent, so when you change its transform (children), I assume Unity have to internally convert the coordinates in order to actually make the stuff happen… perhaps that’s not a problem when you have 1 or 2 parent objects, but what if the hierarchy is really long, with like 15 or more, and lots of ‘siblings’ there updating their coordinates each frame?
Also, gameobjects aren’t the fastest and most optimized class on earth, I wonder the impact of having lots of empty gameobjects (still they have transforms and stuff) only to function as ‘folders’ for organizing objects in a scene…
Obviously, there is a cost to GameObjects and transform hierarchies. GameObjects themselves are somewhat complex in that every single one is made up of a multitude of individual objects and pieces of data. So, for every one, you are paying a cost in memory, in loading-time, and in general management overhead.
Transform hierarchies, as you say, incur a cost in computing world space matrices that is effected by the depth of the hierarchy. This computation, however, happens lazily and the result is cached.
But overall, it really depends on what you are doing. If you have a largely static setup of GameObjects but then nested deep within the hierarchy a set of rapidly moving objects, for example, you will take a bigger hit than if those rapidly moving objects were at toplevel. But whether that really matters much depends.
My advice would be to not worry too much and use GameObjects in a way that makes most sense to you. Then see if you run into bottlenecks and if so, address those.
One subtle conflict can be between organizing things for clarity and ease of use and for running optimally. For example, complex hierarchies can help work with complex scenes. In that case, you can procedurally do cleanups when building the game using scene postprocessors (EDIT: or by marking certain objects as EditorOnly).
Also, some time ago we added the option to automatically collapse GameObject hierarchies for imported models. This can save a lot and if you’re interested in any nodes at all, it’s usually a very specific subset of transforms that you want (e.g. for mounting a weapon to the hand and stuff like that).
Yeah, and it has made quite a difference but on that note, any news on bug 600149? It is causing some issues here and it’d be good to know whether we need to find a better long term workaround rather than wait for a fix.
@AlkisFortuneFish
The bug has been fixed by a developer and is currently in the verification stage. It is earmarked for 5.0 but I’ll check tomorrow whether this is/can be/will be routed to a 4.5 patch release.
@Rene-Damm
Thank you very much for your detailed explanation and tips.
I was about to create a ‘folder’ thing that parent its children to its own parent and destroy itself at runtime… I’m going to look at how tagging objects as “EditorOnly” works though…
EDIT: Tagging objects as “EditorOnly” won’t work in this case because their children won’t show up also… I think the best solution is to create real ‘folder’ objects that simply deletes themselves and parent their children to its own parent. Perhaps Unity could implement a ‘folder’ object that does that in future versions?
Ah right, yes, we kill off the entire hierarchy. An easy and better solution is to devise your own tag (or other means of identifying the objects you want filtered) and then write a scene postprocessor that simply does the reparenting/unparenting and deletions the way you want them. It won’t do quite the same as your solution as you the objects will still be there in play mode, but the upside is that your player builds are free of any design-time artifacts.
Can you explain a little about the scene postprocessor you mention? I’m not sure what you mean by this, and I can’t find any other mention of ‘scene postprocessor’. Is this possible as part of the build pipeline, or just a script that runs after the game has loaded the scene?
I second that, even though this is a little off-topic now! I can’t find much on Scene Postprocessors and how they work.
When testing them, I seemed to get inconsistent results and the postprocessor didn’t get called in the editor. I ended up using a DidReloadScipts callback, but learning about postprocessors would help a lot!