Reduce the weight and loading time of your Unity scene by transforming it into .glb format
Let's put ourselves in this situation: we have a great scene already finished in Unity, with all its assets correctly positioned.
But we have a lot of entities, and we want to reduce the weight of the scene and its loading time. No problem, we have the solution: get through Unity Mesh Baker Tool a single .glb file with colliders and animations included.
Let’s start creating the Mesh Baker object, so in Hierarchy, right click and select Create Other → Mesh Baker → Mesh Baker.
This will create an Empty with a Script:
On the Inspector Tab, we can configure the way that we want to combine our meshes into a single one:
Yellow. Here we can drag from the Hierarchy Tab all the objects that we want to be combined.
Purple. Here is the Output of our Scene, we want in this case to Change the Output to → Bake Into Prefab.
Red. These are additional settings that we can configure and create a template to use it.
Once we have the Output on Bake Into Prefab, createan Empty Prefab in the route you want, (usually on the Models Folder).
We will see that the prefabwill appear on the folder we have specified.
NOTE: We can create a Template with the Shared Settings.
Bake Meshes Into Colliders and Visuals
Let's bake the objects currently in the scene. To do this, we will take the center of gravity (COG) and insert it into the Objects to be Combined list. Please remember to exclude any objects you don't wish to combine, such as colliders, as it selects the entire Hierarchy, even if they are hidden.
We can create another Mesh Baker to bake only the colliders, and then merge it in Blender with the visual objects.
Export Combined meshes into a .fbx
Once we Bake it, two files will be generated in the folder, a .mesh file and a Unity Prefab.
Let's put our Prefab into the scene, with all the transformations at 0,0.
We are going to export the Prefab in a .fbx to be able to import it later in Blender, right click on our Prefab inside the Hierarchy Tab, and select Export To FBX…
The only field we have to take into account is the Export Format, has to be Binary, because Blender don’t read ASCII files.
Import and materials configuration Blender
As it is obvious, we open Blender and import your .fbx. We will see that the object is Parent to an Empty, let’s Unparent it and delete the Empty.
Unparent the object with Alt + P and select Clear and Keep Transformation, then delete the Empty:
To be able to change the materials to our liking, we will go to the Shader Editor window, here we can configure the following attributes:
We will test by doing two types of animations, one by bones and another by transformations. For this, we will separate into a new mesh with P, the mesh we want to animate.
In this case, we want to animate the Glitch screens, so we parent it to a new Armature (Single Bone).
When parent the mesh to our bone, select with Automatic Weights.
Let’s separate the Venus head too, make a simple animation.
Remember to Stash the animations on theNonLinear Animation.
Export into a single .glb
Before we export everything, let's make sure the Hierarchy is set up correctly. All items should be parented to a mesh that has no animation, or to an Empty, in order to avoid any issues.
Make sure all transformations are frozen, except for the meshes that are part of the animation.
Now we go toFile → Export → glb 2.0 (.glb/.gltf).
Make sure to Include the Selected Objects, only if we want to export a selection and not everything.
Finally, let’s open our 3D model, just to see that everythinggone well.
The process of creating a single .glb with colliders and animations seems like a real hassle.
It seems like there are so many steps involved, and it's easy to make mistakes. However, I know that it's important to reduce the weight of my scene and improve the loading time, so I'm willing to stick with it. Hopefully, the end result will be worth the effort.
Javier Vicén Lucia
Enthusiastic about videogames, 3D impression and photography