Tutorial
Heading
Oct 11, 2023

Export your Unity scene to GLB

Reduce the weight and loading time of your Unity scene by transforming it into .glb format

The mission

Let's imagine ourselves in this situation: we already have a fantastic scene finished in Unity, with all its assets correctly positioned.

However, we have a lot of entities, and we aim to reduce the secene's weight and loading time. Not to worry, we have the solution: use the Unity Mesh Baker Tool to generate single .glb file with colliders and animations included.

Let's begin!

Resources

Create Mesh Baker configuration

Let’s start by creating the Mesh Baker object. In the hierarchy pannel, right-click and choose "Create Other → Mesh Baker → Mesh Baker".

This action will generate an empty object with a script attached:

On the "Inspector tab", we can configure how we want to combine our meshes into a single one:

  • Yellow: here, we can drag all the object from the "hierarchy tab" that we want to be combined.
  • Purple: this section displays the "output" of our scene. In this case, we want to change the output to "Bake Into Prefab".
  • Red: these are additional settings that we can configure and create a template to use.

  • Once we have the "output" to "Bake Into Prefab", create an "Empty Prefab" in the desired location, (usually on the "Models" folder).

  • We will observe that the prefab appears on the folder we specified.

NOTE: we have the option to create a template with the shared settings.

Bake meshes into colliders and visuals

Let's bake the objects currently in the scene. To accomplish this, we'll locate the center of gravity (COG) and add it into the "Objects to be Combined" list. It's important to exclude any objects you don't want to combine, such as colliders, as the tool selects the entire hierarchy, even if they are hidden.

Additionally, 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 place our prefab into the scene, ensuring all the transformations are set to 0,0.

To export the prefab as a .fbx file for later import into Blender, right-click on our prefab inside the hierarchy tab, and select "Export To FBX…".

The only field we need to consider is the export format, which must be set to binary, as Blender does not read ASCII files.

Import and materials configuration in Blender

As is obvious, we open Blender and import your .fbx file. Upon inspection, we observe that the object is parented to an empty. Let’s unparent it and delete the empty.

Parent to an empty
Without parent

To unparent the object, use the shortcut "Alt + P" and select "Clear and Keep Transformation". Then, proceed to delete the empty:

To customize the materials according to your preferences, we'll navigate to the Shader Editor window. Here, we can configure the following attributes:

  • Base Color
  • Metallic
  • Specular
  • Roughness
  • Emission
  • EmissionStrength
  • Alpha
  • Normal

Make animations

We'll test two types of animations: one using bones and another using transformations. To achieve this, we'll separate the mesh we want to animate into a new object using the shortcut "P".

In this scenario, we want to animate the glitch screens, so we'll parent it to a new armature (Single Bone).

When parenting the mesh to our bone, select "Automatic Weights" from the options.

Let’s also separate the Venus head and create a simple animation for it.

Don't forget to stash the animations on the NonLinear Animation Editor.

Export into a single .glb

Before exporting everything, let's ensure the hierarchy is set up correctly. All items should be parented to a mesh that has no animation, or to an empty, to avoid any issues.

Additionally, make sure all transformations are frozen, except for the meshes that are part of the animation.

Now, navigate to "File → Export → glb 2.0 (.glb/.gltf)".

Ensure to check "Include the Selected Objects" only if you want to export a selection and not everything.

Finally, let’s open our 3D model just to confirm that everything went well.

Conclusion

The process of creating a single .glb with colliders and animations seems like a real hassle.

There are many steps involved, and it's easy to make mistakes along the way. However, it's essential to reduce the weight of your scene and improve loading time, so it's worth tthe effort. With patience and attention to detail, the end result should hopefully justify the work invested.

Remember, practice and familiarity with the tools will make the process smoother over time.

Unity
Blender
Javier Vicén Lucia
3D Artist

Enthusiastic about videogames, 3D impression and photography

Tutorial
How to import Decentraland SkyboxEditor into Unity
Tutorial
Doing a MANA transaction in a Decentraland scene
Tutorial
Canvas (2D UI) Manager in Decentraland