BLOG

Notes from the KAYAC hyper-casual team

On the Size of Nested Prefabs

Takashi Hirayama

Many cubes generated by nesting prefabs

Hello. I'm Hirayama from the Technology Department.

The conclusion of this article is that nested prefabs seem to have all their nesting expanded at build time. I'd also like to add that using ParticleSystem makes the data big.

Note that this is about 2018.4.6 and 2018.4.11; I haven't checked whether the same holds for other versions.

What's this about?

One of our products is a stage-based game in which each stage is made as a prefab.

The workflow is designed so that lots of prefabs for the components of a stage are prepared in advance, and the game designers place them in the Unity Editor to make the stage prefabs.

I think it's a pretty standard design for Unity, but when we actually built the game, these stage prefabs turned out to be strangely big. Big enough that the load time was a concern.

Wondering what on earth was going on, we looked into it and found what I stated as the conclusion at the start.

Prefabs as assets, and prefabs after the build

As assets, prefabs don't get bigger when nested.

For example, even if lots of bricks are placed in a stage, the stage's .prefab file doesn't get very big. Of course, things like position information are added according to the number of bricks, but that's all. And even if you add components to the brick prefab, the size of the stage prefab doesn't change.

When you think about it, that's natural. The stage prefab only references the brick prefab; it doesn't hold a copy of its contents. It's obviously better if changing a child doesn't require touching the parent (although at present, changing a child triggers a reimport of all the parent prefabs. There seems to be some reason for it).

In reality, however, after a build the size seems to grow in proportion to the actual number of GameObjects. If so, the likely explanation is that "all the children are expanded (copied) at build time".

I ran an experiment to see whether that's really the case.

The experiment

I've put the experiment project on GitHub. What it does is simple.

First, place one cube in a prefab named 0. There's no particular point to it, but I also added a component called Rotation that rotates it.

Next, make a prefab called 1, and place two 0s in it, offset from each other.

Then make a prefab called 2, and place two 1s in it, offset from each other. Two 2s in 3, two 3s in 4, two 4s in 5, and so on; I made them up to 13. For example, 7's structure looks like this.

The hierarchy of prefab 7

0 has 1 cube, 1 has 2 cubes, 2 has 4 cubes, and so the total number of cubes doubles each time. The total number of cubes in prefab k is 2^k, so 13 has 8192.

Then I measure the load time and Instantiate time for these, and do the same in a build. For the build, I also look at the size after building.

Results

Measurements in the editor

PrefabLoadInstantiate.prefab file size
00.9941.534kb
12.660.3427kb
22.010.5567kb
33.123.877kb
43.881.847kb
517.32.257kb
611.24.667kb
721.25.897kb
833.09.577kb
973.824.07kb
1016245.07kb
113111187kb
126072687kb
1312605777kb

Load is the time taken (in seconds) by Resources.Load. I measured it from the difference in System.DateTime.Now.

Up to about 7, measurement error makes it hard to tell much, but from 8 on it doubles each step.

And the .prefab files are all the same size. Only 0 is different because it alone contains a MeshRenderer, and its number of GameObjects differs from the others.

Measurements in a build

PrefabLoadInstantiateSize reported in Editor.log for the build
049.42.590.5kb
115.00.1831.1kb
26.890.1182.3kb
39.030.2554.9kb
43.490.5069.8kb
511.80.77919.7kb
614.91.7439.4kb
713.43.2179.0kb
830.34.18158kb
949.914.6316kb
1010922.2633kb
1118453.61.2mb
123801022.5mb
138152534.9mb

It's the same in the build: from around 7 up, both load time and Instantiate time double each step, and the size in the build log doubles too.

The build is IL2CPP on a Mac. On actual phones you'd expect it to be several times slower, so with as many as 8000 GameObjects, wouldn't it take 3 or 4 seconds? That's the concern.

It feels obvious, but...

Expanding the whole reference structure at build time so that prefabs and scenes initialize quickly is an understandable implementation. And once it's pointed out, it seems obvious that the load and the size depend on the number of GameObjects that end up in the result.

But when prefab nesting gets deep, as in this example, the size can grow exponentially, and it's hard to notice.

Old versions of Unity didn't allow nesting, so you didn't end up with all that many GameObjects. That's because adding GameObjects takes effort. Unless you build an automatic placement tool in the Editor, the effort limits the number.

But once prefabs can be nested, as in the experiment above, you can produce a tremendous number of GameObjects without much effort. Even with only 2 per level, 13 levels swell it to 8192. The number of GameObjects placed by hand is fewer than 50. You could say that "it's heavy because too many GameObjects are placed" is now more likely to happen than before.

This particular case

That said, on closer investigation, whether things were nested had little to do with the case we ran into in the product. The problem was ParticleSystem.

In terms of the earlier "stage" and "brick" example, each brick contained a "brick-shattering effect" prefab, made with ParticleSystem. But the data for this ParticleSystem is unbelievably big. Placing just one adds nearly 5000 lines to the .prefab file. Presumably that's because its enormous number of parameters all get written out. More than 50 of them were placed in one stage, and so the stage's data became very big.

Countermeasures

It's the obvious story of "lots of GameObjects make it slow", but the problem is that they multiply before you notice, and saying "let's be careful about the number of GameObjects when building things" doesn't seem like a realistic measure either. A good thing about Unity development is that non-programmers can place things freely, and having non-programmers worry about implementation and performance is inefficient.

So at present we haven't put any countermeasure in place, but one idea I'm considering is to run a script after the prefab has been made, store the information on the placed prefabs in json or something, and Instantiate dynamically from it at initialization.

When non-programmers make prefabs, the main thing they'll probably be tinkering with is "where" to place "what". If it's "just placing prefabs inside a prefab and adjusting their position and angle", then recording only "the prefab type, position and angle" in json or something is enough. Then you Instantiate from that information at runtime. At the very least, the post-build size and load time would improve.

Of course, being able to play it as is during development is important, so for the game designers the work should stay "place prefabs without worrying". I think it would be good if it could be done in a way game designers don't see, such as converting to json in a pre-build script.

Unfortunately the Instantiate time won't go down; if anything it'll go up, but I'd hope the increase stays "small enough not to be a problem". When there are lots of them, you could also spread them across frames with a coroutine to reduce spikes.

Depending on what's placed, it should also be possible to "not Instantiate until just before it's used". For example, a "brick-breaking effect" can be delayed until just before the breaking, and used ones can be kept and reused. For such things, you may not need to prepare as many as were placed in the original prefab, and overall the processing might even be reduced.

In this case, however, the problem is ParticleSystem, and if the prefab is big, Instantiate will be heavy too. Doing that dynamically means spikes during the game. So a reasonable implementation seems to be to create "roughly as many as should be enough" at initialization, and if by some chance they run out, add more dynamically even at the cost of a spike.

Summary

Personally, I wish Unity would prioritize size over speed here. Even if Instantiate became a bit slower, I'd like prefab data to be built with its references preserved.

You naturally can't know the size of a prefab after the build until you build it, and nobody bothers checking asset sizes in the build log unless something goes seriously wrong.

Also, the number of ParticleSystem parameters has gotten out of hand, and I can't help feeling, "surely it has hit its limit by now?"