- Home /
Strange Hierarchy Lag
I have a Unity Scene with a field of 10.000 models of magnet boards, and I got it running smooth in the Scene Editor. Except... when I select it, then I get horrible lag when moving around. I was wondering what's different and why this is happening.
Some more Info:
- Profiler shows CPU Usage is a little bit below 30 fps when moving around in scene view normally, rendering taking up ~90% of energy.
- Profiler shows CPU usage slower than 15 fps when moving around in scene view with object selected, rendering taking up ~95% of energy.
- No colliders, No other components, no editor scripts, just meshes in LOD groups in empty parents.
- No lag when working in the hierarchy, even on the object itself.
- No lag when a single model selected.
- The object is from root to child all static.
- Every magnet board has a low poly version that I switch between with a LOD group (though how this could make things slower I can't imagine).
- The Hierarchy item contains, in total plus itself included, 31.111 objects (the 1's are parents of 10 to keep it clean.) I'm not transforming any of the empty parents.
It seems to me that when a model in the hierarchy is selected, it is "rendered" twice, or at least the profiler has me believe so. Why is that happening and how to fix it?
P.S. I know creating a single model out of 10.000 magnet boards will reduce rendering time and I'll do so once the model is locked, but this still seems like an interesting problem to me and I couldn't figure it out with Google.
I post this as a comment ins$$anonymous$$d of an aswer, because I'm not sure if this is the main cause of the lag, but I'm pretty sure this is atleast part of it.
$$anonymous$$y guess would be that Unity is trying to render the gizmo outlines of selected objects (this outline is drawn for the object you select AND all child objects. Drawing gizmo's also takes up cpu time, as there is (over simplified) a piece of code that is executed by the cpu that loops all selected objects and then adds their outlines to a buffer for the gpu to render. It is done in a way similarly of how you could implement drawing OnGUI or gizmos yourself. Normally this has little to no impact because the cpu doesn't have a lot to do in edit mode anyway, but having to loop 31111 objects (and actually drawing the outlines) is a lot to do for the cpu.
Outside of playmode, the scene only updates (and with it, redraws gizmos) if there are any changes to any objects or when moving the scene camera. Sinse when you move the scene camera it has to update multiple times a second as opposed to just once when you change something in the inspector. The lag will be more noticeable when moving the scene camera.
You could try clicking 'gizmos' in the top right of your scene view and uncheck 'selection outline'. $$anonymous$$y guess is that this should make at least a no$$anonymous$$ble difference.
Troien, you got it in one! Turning off selection outline fixed my problem immediately, I would have never thought of that! Post it as an answer and I'll mark it so others can find it!
Answer by troien · Mar 05, 2019 at 08:38 PM
As mentioned in the comments.
When you select something in the hierarchy, Unity tries to render the gizmo outlines of selected objects. This outline is drawn for the object you select AND all child objects. Drawing gizmo's also takes up cpu time, as there is (over simplified) a piece of code that is executed by the cpu that loops all selected objects and then adds their outlines to a buffer for the gpu to render. It is done in a way similarly of how you could implement drawing OnGUI or gizmos yourself. Normally this has little to no impact because the cpu doesn't have a lot to do in edit mode anyway, but having to loop 31111 objects (and actually drawing the outlines) is a lot to do for the cpu.
Outside of playmode, the scene only updates (and with it, redraws gizmos) if there are any changes to any objects or when moving the scene camera. Sinse when you move the scene camera it has to update multiple times a second as opposed to just once when you change something in the inspector. The lag will be more noticeable when moving the scene camera.
You could try clicking 'gizmos' in the top right of your scene view and uncheck 'selection outline'. This should make a noteable difference.
For other people who have similar problems, but for who this didn't work. I don't know of any other thing that Unity does in the background with selections that could give a noteable difference (doesn't mean there are none). But if you select a lot of objects that have a MonoBehaviour with OnDrawGizmosSelected implemented (Some builtin Unity ones might have that aswell) you get the same problem, in that case disabling the gizmos of that specific MonoBehaviour might help aswell.
For me the "Selection outline" was a game changer. For 1-2 thousands of prefab instances the difference was astonishing. With outline I had about 20-30 fps in the editor while it easily reached the monitor's refresh rate (75 Hz) after I turned it off.