Reducing build sizes in Unity3D

I was building a Unity3D application and during its development, I learned few things about Unity’s build system.

Format/Size of your Asset file does not matter

Unity has a completely automatic Asset pipeline. Whenever a source Asset like a .psd or a .fbx file is modified, Unity detects the change and automatically re-imports it. The imported data from the file is subsequently stored by Unity in an internal format. Source: Unity manual - Cache Server | Asset Pipeline

Unity doesn’t care about file format and size of your asset files. File format of your asset files is just a way in which your 3D modeling program gives Information to Unity

What I mean by this is that if you import a .blend file of size 12mb or export that same .blend file to .fbx which results in a 4mb file, Unity will treat them same because they both have the same information

When Unity builds your application it doesn’t bundle your asset files as it is, Unity uses it’s own internal format in which Unity keeps Asset data. Look at Asset Database API if you are interested in it

You may have seen that if Unity detects any changes in asset file, it re-imports it (cache server is used to speed up that re-import process)

cache_server

Find out how much each asset is contributing to build size

Just like any optimization task first, find out the culprit responsible for high build sizes.

To see how much each asset is contributing in your build have a look at Editor.log after building you application You can open Ediotr.log by going to Console -> Open Editor Log

editor_log

Here is log from my application showing contribution of assets in build size

You can see how each compression level affects mesh and results clearly shows the compression artifacts

Mesh Compression: Off

8.2 mb	 15.7% Assets/Models/diamond.fbx
7.3 mb	 13.9% Assets/Models/rocksalt.fbx
4.7 mb	 8.9% Assets/Models/ice.fbx
4.7 mb	 8.9% Assets/Models/graphite.fbx
...

result

off

Mesh Compression: Medium

3.5 mb	 14.2% Assets/Models/diamond.fbx
3.0 mb	 12.2% Assets/Models/rocksalt.fbx
2.0 mb	 8.0% Assets/Models/graphite.fbx
1.9 mb	 7.5% Assets/Models/ice.fbx
...

result

med

Mesh Compression: High

2.7 mb	 13.5% Assets/Models/diamond.fbx
2.3 mb	 11.4% Assets/Models/rocksalt.fbx
1.5 mb	 7.5% Assets/Models/graphite.fbx
1.4 mb	 6.9% Assets/Models/ice.fbx
...

result

high

Techniques to reduce build size

Compressing your assets

1. let Unity do the task

You can reduce the build size by selecting a mesh compression level in Unity mesh_compr

2. Compress it yourself

As we know the file size of you asset does not matter, what matters is mesh data(mostly triangle/vertices count) You can keep triangle/vertices count low in your models which will result in low size models when Unity imports it. which will result in lower build size

If you are working in Blender, you can keep models low ploy during modeling and then add modifiers to achieve the desired detail level

Targeted Builds

When you build for Android with default setting which is FAT(ARMv7+x86) it includes the Assets + ARMv7 Specific files + x86 specific files in an apk file, so a single application can run on x86 android devices and ARMv7 android devices.

You can reduce the apk size by building it separately for both architectures, it will reduce apk size significantly.

You can use stripping level to further reduce the build size, for me micro mscorelib option resulted in the minimum size, but Unity3D [manual says use it with care] (https://docs.unity3d.com/Manual/iphone-playerSizeOptimization.html)

build_target

Results

Resulted file sizes

  • Mesh Compression: Off & Combined(FAT) Build - Android APK size: 41.2 MB
  • Mesh compression: Medium - Android APK size: 32.5 MB
  • Targeted build - Android APK size(ARMv7): 30.4 MB
  • Unity3D Mesh Compression: Medium + Targeted builds
    • Android APK size(ARMv7): 19.3 MB
    • Android APK size(x86): 19.7 MB

From files size you can see that we can reduce build size by 50%

Unity3D Project and [Assets] (https://github.com/electron0zero/MolView–3d-data-files) is available on GitHub.


👋 Like what you have read, Subscribe to get updates 📩

Continue Reading