{"id":104337,"date":"2019-11-22T18:36:00","date_gmt":"2019-11-22T18:36:00","guid":{"rendered":"http:\/\/www.gamasutra.com\/view\/news\/354540"},"modified":"2019-11-22T18:36:00","modified_gmt":"2019-11-22T18:36:00","slug":"blog-handling-unity-scene-hierarchy-and-performance","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2019\/11\/22\/blog-handling-unity-scene-hierarchy-and-performance\/","title":{"rendered":"Blog: Handling Unity scene hierarchy and performance"},"content":{"rendered":"<p><strong><i><small> The following blog post, unless otherwise noted, was written by a member of Gamasutra\u0092s community.<br \/>The thoughts and opinions expressed are those of the writer and not Gamasutra or its parent company. <\/small><\/i><\/strong> <\/p>\n<hr>\n<p data-css=\"tve-u-16e85c2bf35\"><em>Last week, I wrote a&nbsp;<\/em><a href=\"https:\/\/thegamedev.guru\/unity-performance\/scene-hierarchy-optimization\/\" target=\"_blank\" rel=\"noopener noreferrer\"><em>post&nbsp;<\/em><\/a><em>to show you&nbsp;<\/em><em>how your unity scene hierarchy is reducing the performance of your game<\/em><em>. That post arose awareness across many of you developers. And so, many of you asked great questions that I&#8217;ll answer in today&#8217;s entry.<\/em><\/p>\n<p data-css=\"tve-u-16e85c2bf35\"><em>[The original post can be found <a href=\"https:\/\/thegamedev.guru\/unity-performance\/scene-hierarchy-catch-that-performance-thief-part-2\/\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>]<\/em><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt data-css=\"tve-u-16e85c27d93\" data-id=\"3974\" height=\"250\" src=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2019\/11\/blog-handling-unity-scene-hierarchy-and-performance.jpg\" title=\"Unity-Scene-Hierarchy-Part2-Thumbnail\" width=\"469\"><\/p>\n<p>Yes&#8230; I confess.<\/p>\n<p>The examples I often show you in my blog posts are not real-life projects. This is one of the points you have been commenting on.<\/p>\n<p><em>Ruben, this is an artificial scenario.<\/em><\/p>\n<p><em>Ruben, this doesn&#8217;t happen in games.<\/em><\/p>\n<p>You see, I understand. It&#8217;s easy to doubt the information I provide you when the only cases you see are extreme.<s>&nbsp;I&#8217;d even rub some salt in&nbsp;the wound on the Reddit threads if the author wasn&#8217;t me (see&nbsp;<a href=\"https:\/\/www.reddit.com\/r\/gamedev\/comments\/dv71y1\/unity_your_scene_hierarchy_is_robbing_you\/\" target=\"_blank\" rel=\"noopener noreferrer\">how I got owned<\/a>).<\/s><\/p>\n<p>But here&#8217;s the thing: all the information I give you is based on the&nbsp;<strong>pain and gains of real-life projects<\/strong>&nbsp;I worked on. I do my research before I write here too.<\/p>\n<p>Doing research is great. But that takes time. A lot.<\/p>\n<p>So I won&#8217;t set up a new game for every weekly post that I publish. What I do instead is to create a small project to make a point&#8230;<\/p>\n<p>&#8230;A very real point that I experienced in production games.<\/p>\n<p>You&#8217;re never going to come across the exact dummy project in your games.&nbsp;<strong>But you&#8217;re likely to suffer from the issues these points reveal<\/strong>.<\/p>\n<p>And that&#8217;s what matters.<\/p>\n<p>So I took some of the feedback you provided for today&#8217;s post. I&#8217;ll elaborate on some of the problematic hierarchy patterns you&#8217;ll commonly see in production games. We will address unity scene hierarchy bottlenecks based on the <a href=\"https:\/\/thegamedev.guru\/unity-performance\/scene-hierarchy-optimization\/#tab-con-3\" target=\"_blank\" rel=\"noopener noreferrer\">tools<\/a> I gave you in the last article:<\/p>\n<ul>\n<li data-css=\"tve-u-16e850d7209\">The <em>FAP Hierarchy Tool<\/em><\/li>\n<li data-css=\"tve-u-16e850d7209\">The <em>DetachGameObject<\/em>&nbsp;simple performance boostingg&nbsp;component<\/li>\n<\/ul>\n<p><strong>Quick Navigation (opens in a new tab)<\/strong><\/p>\n<p><a href=\"https:\/\/thegamedev.guru\/unity-performance\/scene-hierarchy-catch-that-performance-thief-part-2\/#tab-con-3\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">The golden rules for an efficient scene hierarchy<\/a><\/p>\n<p>&nbsp; &nbsp;&nbsp;<a href=\"https:\/\/thegamedev.guru\/unity-performance\/scene-hierarchy-catch-that-performance-thief-part-2\/#tab-con-4\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">The Gamedev Guru&#8217;s Golden Rules of a Clean Unity Scene Hierarchy<\/a><\/p>\n<p><a href=\"https:\/\/thegamedev.guru\/unity-performance\/scene-hierarchy-catch-that-performance-thief-part-2\/#tab-con-5\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">Flattening a unity scene hierarchy: an artificial case-study<\/a><\/p>\n<p>&nbsp; &nbsp;&nbsp;<a href=\"https:\/\/thegamedev.guru\/unity-performance\/scene-hierarchy-catch-that-performance-thief-part-2\/#tab-con-6\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">The hierarchy structure<\/a><\/p>\n<p>&nbsp; &nbsp;&nbsp;<a href=\"https:\/\/thegamedev.guru\/unity-performance\/scene-hierarchy-catch-that-performance-thief-part-2\/#tab-con-7\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">Profiling the unoptimized scene<\/a><\/p>\n<p>&nbsp; &nbsp;&nbsp;<a href=\"https:\/\/thegamedev.guru\/unity-performance\/scene-hierarchy-catch-that-performance-thief-part-2\/#tab-con-2\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">Flattening our scene hierarchy<\/a><\/p>\n<p><a href=\"https:\/\/thegamedev.guru\/unity-performance\/scene-hierarchy-catch-that-performance-thief-part-2\/#tab-con-0\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">So what?<\/a><\/p>\n<h2 id=\"tab-con-3\">The golden rules for an efficient scene hierarchy<\/h2>\n<p>In the previous post we established a guideline for diagnosing and optimizing unity scene hierarchies.<\/p>\n<p data-css=\"tve-u-16e851023f5\">Let&#8217;s quickly recap&nbsp;<strong>The Gamedev Guru&#8217;s golden rules for an efficient hierarchy<\/strong>:<\/p>\n<p><a href=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2019\/11\/blog-handling-unity-scene-hierarchy-and-performance.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" alt height=\"270\" src=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2019\/11\/blog-handling-unity-scene-hierarchy-and-performance.png\" width=\"600\"><\/a><\/p>\n<p>These apply especially to hierarchy trees that have&nbsp;<strong>dynamic objects<\/strong>. And by dynamic I mean, game objects whose transforms are altered. It can be a position, a rotation, a scale or any of the attributes you find in a&nbsp;<em>RectTransform<\/em>.<\/p>\n<p>If an entire tree is static, i.e. no periodical transform changes, then don&#8217;t worry about that tree.<\/p>\n<p>You see,&nbsp;<strong>propagating&nbsp;<\/strong>these transform changes takes time. And it takes more time when you have more game objects in the same tree.<\/p>\n<p>But it&#8217;s not the total CPU time that concerns me the most. The issue that I see is that it is pretty hard for Unity to do these transform operations in&nbsp;<strong>parallel&nbsp;<\/strong>when they happen to be in the same hierarchy tree.<\/p>\n<p>So changes in complex trees take a higher CPU time for two main reasons:<\/p>\n<ul>\n<li>The absolute CPU time required to do the math increases<\/li>\n<li>These calculations cannot be spread across different threads<\/li>\n<\/ul>\n<h2 id=\"tab-con-5\">Flattening a unity scene hierarchy: an artificial case-study<\/h2>\n<p>I&#8217;m a pragmatic and practical professional developer. So let&#8217;s see all this theory in action.<\/p>\n<h3 id=\"tab-con-6\">The hierarchy structure<\/h3>\n<p>What I have here for you is a scene full of props, particle systems and characters. This is how the unity scene hierarchy looks like:<\/p>\n<p><a href=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2019\/11\/blog-handling-unity-scene-hierarchy-and-performance-1.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" alt=\"Unity Scene Hierarchy: Original Structure\" data-css=\"tve-u-16e858bbad7\" data-id=\"3967\" data-tcb-events=\"__TCB_EVENT_[{&quot;t&quot;:&quot;click&quot;,&quot;a&quot;:&quot;thrive_zoom&quot;,&quot;c&quot;:{&quot;id&quot;:&quot;3967&quot;,&quot;size&quot;:&quot;full&quot;}}]_TNEVE_BCT__\" height=\"500\" src=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2019\/11\/blog-handling-unity-scene-hierarchy-and-performance-1.png\" title=\"Unity-Scene-Hierarchy-Kinda-Deep\" width=\"211\"><\/a><\/p>\n<p><em>Unity Scene Hierarchy: Original Structure<\/em><\/p>\n<p data-css=\"tve-u-16e858db459\">That&#8217;s it. No magic.&nbsp;<strong>4-5 levels of depth<\/strong>, plus all the bones required for the characters. Nothing too crazy apart from the 300 simple characters, which probably accounts for all the missing pieces that a real game has.<\/p>\n<p>Have a look at&nbsp;<em>World<\/em>. It&#8217;s a single root game object containing&nbsp;<strong>way too many children<\/strong>. This fact violates the first golden rule of an efficient unity scene hierarchy.<\/p>\n<p>Is that bad? I don&#8217;t know. The way to find out is by&nbsp;<strong>measuring its relative cost.<\/strong><\/p>\n<p>I&#8217;m using free assets I found in the asset store. That means, I can&#8217;t upload this project to GitHub or I&#8217;ll risk ending up in jail. Yeah, thanks for that restrictive license, Unity.<\/p>\n<h3 id=\"tab-con-7\">Profiling the unoptimized scene<\/h3>\n<p data-css=\"tve-u-16e85cd0244\">And so I start the scene. Not much happening, just a slight amount of movement. I cannot say this is the funniest game I ever played.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"Sample project &quot;gameplay&quot;\" data-css=\"tve-u-16e85ccc124\" data-id=\"3975\" data-tcb-events=\"__TCB_EVENT_[{&quot;t&quot;:&quot;click&quot;,&quot;a&quot;:&quot;thrive_zoom&quot;,&quot;c&quot;:{&quot;id&quot;:&quot;3975&quot;,&quot;size&quot;:&quot;full&quot;}}]_TNEVE_BCT__\" height=\"269\" src=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2019\/11\/blog-handling-unity-scene-hierarchy-and-performance.gif\" title=\"Unity-Scene-Hierarchy-Gameplay\" width=\"534\"><\/p>\n<p><em>Sample project &#8220;gameplay&#8221;<\/em><\/p>\n<p data-css=\"tve-u-16e85cd8a98\">Well, I&#8217;m not an artist or designer. I&#8217;m excused for the looks but not for the performance. Point which brings me to using the profiler now.<\/p>\n<p data-css=\"tve-u-16e85249ba9\">I captured a 300-frame profile and here&#8217;s what I got:<\/p>\n<p><a href=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2019\/11\/blog-handling-unity-scene-hierarchy-and-performance-1.jpg\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" alt=\"Unity Scene Hierarchy: Pre-Optimization\" data-css=\"tve-u-16e8523d53b\" data-id=\"3965\" data-tcb-events=\"__TCB_EVENT_[{&quot;t&quot;:&quot;click&quot;,&quot;a&quot;:&quot;thrive_zoom&quot;,&quot;c&quot;:{&quot;id&quot;:&quot;3965&quot;,&quot;size&quot;:&quot;full&quot;}}]_TNEVE_BCT__\" height=\"239\" src=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2019\/11\/blog-handling-unity-scene-hierarchy-and-performance-1.jpg\" title=\"Unity-Scene-Hierarchy-Pre-Optimization\" width=\"600\"><\/a><\/p>\n<p><em>Unity Scene Hierarchy: Pre-Optimization<\/em><\/p>\n<p>Was that useful?<\/p>\n<p>Nah, don&#8217;t even bother to look at the image.<\/p>\n<p>We don&#8217;t know whether that&#8217;s good or bad, because we don&#8217;t have a reference point.<\/p>\n<p>But we can compare&#8230; We can compare against an equivalent, flattened hierarchy.<\/p>\n<p>Let&#8217;s try that out.<\/p>\n<h3 id=\"tab-con-2\">Flattening our scene hierarchy<\/h3>\n<p>Analyzing the previous hierarchy, we can notice something of interest.<\/p>\n<p data-css=\"tve-u-16e85b72326\">Most of the introduced game objects are there for organization purposes in this setup. That means, some&nbsp;<strong>added hierarchy levels are useful for developers to&nbsp;<\/strong><strong>structure content<\/strong>&nbsp;around them. We incorporate these objects to make development easier.<\/p>\n<p>Game objects such as&nbsp;<em>World<\/em>,&nbsp;<em>City<\/em>,&nbsp;<em>Props&nbsp;<\/em>don&#8217;t serve any other purpose than organizing. Our characters, particles, UI and props do not really depend on those to accomplish their goals.<\/p>\n<p>I see a potential gain here.<\/p>\n<p>But on the other side, we don&#8217;t want to break the only organizational tool we have. I don&#8217;t want to deal with a flat hierarchy during development. That sucks.<\/p>\n<p data-css=\"tve-u-16e85ce78d6\"><strong>We want to keep things structured on the editor and yet we want our game to be performant on run-time<\/strong>.<\/p>\n<p data-css=\"tve-u-16e85cedc82\">Ehmm&#8230; Is this possible?<\/p>\n<p>You bet it is. That we can do by&nbsp;making use of the script you downloaded in the&nbsp;<a href=\"https:\/\/thegamedev.guru\/unity-performance\/scene-hierarchy-optimization\/#tab-con-3\" target=\"_blank\" rel=\"noopener noreferrer\">first part of the blog series<\/a>:&nbsp;<em>DetachGameObject<\/em>. This script will let you maintain the original hierarchy when you develop your game but will unparent the gameobject of your choice to squeeze all its&nbsp;<em>performance juice<\/em>&nbsp;on run-time.<\/p>\n<p data-css=\"tve-u-16e85a081de\">So&nbsp;I&#8217;ll add right now our&nbsp;<em>DetachGameObject&nbsp;<\/em>component to the Character prefab, to all particle systems and to the dynamic canvas we have in place. I&#8217;ll ask our component to unparent the game object after a delay of 15 seconds so I can take two profiles: one before detaching and another after it.<\/p>\n<p data-css=\"tve-u-16e859f706f\">Below you find the&nbsp;<em>DetachGameObject&nbsp;<\/em>asset applied to an example particle effect.<\/p>\n<p><a href=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2019\/11\/blog-handling-unity-scene-hierarchy-and-performance-2.png\"><img loading=\"lazy\" decoding=\"async\" alt=\"DetachGameObject Performance Booster\" data-css=\"tve-u-16e859ee904\" data-id=\"3968\" data-tcb-events=\"__TCB_EVENT_[{&quot;t&quot;:&quot;click&quot;,&quot;a&quot;:&quot;thrive_zoom&quot;,&quot;c&quot;:{&quot;id&quot;:&quot;3968&quot;,&quot;size&quot;:&quot;full&quot;}}]_TNEVE_BCT__\" height=\"265\" src=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2019\/11\/blog-handling-unity-scene-hierarchy-and-performance-2.png\" title=\"Unity-Scene-Hierarchy-DetachGameObject\" width=\"600\"><\/a><\/p>\n<p><em>DetachGameObject Performance Booster<\/em><\/p>\n<p>Now that I have set up all&nbsp;<em>DetachGameObject&nbsp;<\/em>components, there&#8217;s only one thing remaining. That&#8217;s right, press the play button!<\/p>\n<p>So I run the game and after 15 seconds&#8230;<\/p>\n<p><a href=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2019\/11\/blog-handling-unity-scene-hierarchy-and-performance-3.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" alt=\"Scene Hierarchy: Flatter Optimized Version\" data-css=\"tve-u-16e85a63e36\" data-id=\"3969\" data-tcb-events=\"__TCB_EVENT_[{&quot;t&quot;:&quot;click&quot;,&quot;a&quot;:&quot;thrive_zoom&quot;,&quot;c&quot;:{&quot;id&quot;:&quot;3969&quot;,&quot;size&quot;:&quot;full&quot;}}]_TNEVE_BCT__\" height=\"480\" src=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2019\/11\/blog-handling-unity-scene-hierarchy-and-performance-3.png\" title=\"Unity-Scene-Hierarchy-Flat\" width=\"200\"><\/a><\/p>\n<p><em>Scene Hierarchy: Flatter Optimized Version<\/em><\/p>\n<p><strong>Boom<\/strong>.<\/p>\n<p>All my characters, particles and UI have been&nbsp;<strong>detached<\/strong>. Now the hierarchy is much&nbsp;<strong>flatter<\/strong>.<\/p>\n<p><em>So I wonder&#8230;<\/em><\/p>\n<p>How do both profiles compare? Let&#8217;s use the neat&nbsp;<a href=\"https:\/\/blogs.unity3d.com\/2019\/05\/13\/introducing-the-profile-analyzer\/\" target=\"_blank\" rel=\"noopener noreferrer\">profile analyzer<\/a>&nbsp;to get some fresh numbers.<\/p>\n<p><em>*drums*<\/em><\/p>\n<p><a href=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2019\/11\/blog-handling-unity-scene-hierarchy-and-performance-4.png\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" alt=\"Profile Comparison: Deeper vs. Boosted Flatter CPU Performance\" data-css=\"tve-u-16e85b3559e\" data-id=\"3970\" height=\"312\" src=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2019\/11\/blog-handling-unity-scene-hierarchy-and-performance-4.png\" title=\"Unity-Scene-Hierarchy-Profile-Analyzer\" width=\"600\"><\/a><\/p>\n<p><em>Profile Comparison: Deeper vs. Boosted Flatter CPU Performance<\/em><\/p>\n<p>I&#8217;ll translate for you what this chart means&#8230;<\/p>\n<p>This comparison says that there&#8217;re&nbsp;<strong>significant differences<\/strong>&nbsp;between the deeper and flatter hierarchies.<\/p>\n<p><strong>The flatter hierarchy improves performance significantly over the deeper one<\/strong>.<\/p>\n<p>Yes, you might not have 300 characters, but you will surely have many over 100 times more complexity in real gameplay elements, scripts, networking and such.<\/p>\n<h2 id=\"tab-con-0\">So what?<\/h2>\n<p>The conclusion is simple:&nbsp;<strong>there&#8217;s a performance penalty you&#8217;re paying if you don&#8217;t have a flat hierarchy<\/strong>. Can you afford it? That&#8217;s a question only you can answer. And you better answer that with data and metrics. My metrics in my previous games always reach the same conclusion: I don&#8217;t want to pay that expensive bill. I let instead&nbsp;<em>DetachGameObject<\/em>&nbsp;pay it for me.<\/p>\n<p>That&#8217;s my advice to you. Measure your game through the profiler and&nbsp;<a href=\"https:\/\/thegamedev.guru\/unity-performance\/scene-hierarchy-optimization\/\" target=\"_blank\" rel=\"noopener noreferrer\"><em>FAP Hierarchy Tool<\/em><\/a>. Both tools will immensely help you finding scene hierarchy bottlenecks in your Unity game.<\/p>\n<p>Always remember&#8230;<\/p>\n<p data-css=\"tve-u-16e85b5a072\"><strong>Flattening your hierarchy will improve your Unity CPU performance<\/strong>.<\/p>\n<p>What were your scores before and after flattening your hierarchy? Share below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The following blog post, unless otherwise noted, was written by a member of Gamasutra\u0092s community.The thoughts and opinions expressed are those of the writer and not Gamasutra or its parent company. Last week, I wrote a&nbsp;post&nbsp;to show you&nbsp;how your unity scene hierarchy is reducing the performance of your game. That post arose awareness across many [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":104338,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-104337","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-news"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/104337","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=104337"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/104337\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media\/104338"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=104337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=104337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=104337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}