I am also encountering a weird variant of this.
When I initialise my scene, I am using GetComponentsInChildren to loop through and find all UGUI child game objects containing a particular component, so I can cache them in a dictionary for faster access later. The search starts at each canvas root parent in the scene, and all child objects are active. The weirdness comes in with the root parent being enabled or not.
Now, if the canvas object is enabled, no problem - it all goes nice and fast. But if I set the canvas disabled in the scene (which is preferable, to avoid flickering) then GetComponentsInChildren runs very slow and adds up to 5 seconds to startup. No problem, you say, just SetActive it before you start getting the components: but when I tried that, the slowness doesn't go away even though effectively the objects are now in the exact same state as if I'd saved them as active in the scene. Weird.
Currently I am getting around it all by leaving the parents active but turning the camera off until I've done all the caching, to avoid the flickering. But I would much prefer to understand why this is happening like this, and why the mere presence of a disabled parent makes GetComponentsInChildren so considerably slow in the first place.
↧