Reputation:
When I first set a particular gameobject in the Canvas element to active, (SetActive(true)), it lags for a good second or so. Sequential activations are instantaneous. Note that this only happens in the standalone player. In the editor, it doesn't lag at all. My guess is obviously that the first time it has to load the asset. However, can I preload this particular asset in any way? I attempted to do so in the build settings under optimization, but that didn't affect anything. And no, I don't have any code in the Start() function or the OnEnable() function of the said gameobject that's being enabled.
Upvotes: 0
Views: 3695
Reputation: 808
The accepted answer is a workaround, not a solution. Keeping hundreds of UI elements loaded is an utter waste of memory.
You should instead divide up your canvases, as written in this wonderful article by Unity. The reason for the lag is Unity's updating all canvas contents, even disabled objects, when you enable an object for the first time. To prevent unused objects from updating, isolate them in a different canvases. You can even put those canvases as children of your original canvas if you wish.
Upvotes: 1
Reputation: 125275
That's expected.The lag depends on how many UI
components and GameObjects that are under the hierarchy of the Canvas
. There is so much memory allocation and draw calls when SetActive(true)
is used on a Canvas
or UI component. The fix is simple. Instead of using SetActive(true)
for UI, disable the component by modifying the enabled
property.
For example, to enable/disable the the Canvas
:
Canvas canvas;
canvas.enabled = true; //Enable
canvas.enabled = false; //Disable
Let's say that you only want to disable a Text
component under a Canvas
, use the enabled
property to disable that Text
component instead of SetActive(true)
. The-same thing applies to other UI components.
Upvotes: 1