[Unity] Loading GLTF Models at Runtime for Android
Published: 2023-07-19 10:24:59

The **goal** of this project is to load `.gltf` files at runtime in Unity for Android, with specific focus on the Oculus Quest. This can be challenging due to differences in rendering pipelines and limited native GLTF support in Unity.

## The Problem

While Unity provides support for common 3D formats like `.fbx`, `.obj`, and `.dae`, it lacks native support for GLTF files.

## The Contenders: UnityGLTF, GLTFUtility, and UniGLTF

On reviewing the available options, three libraries emerged as the front runners: [UnityGLTF](https://github.com/KhronosGroup/UnityGLTF), [GLTFUtility](https://github.com/Siccity/GLTFUtility), and [UniGLTF](https://github.com/ousttrue/UniGLTF)

## Selection

Taking into account all three options, I opted for **GLTFUtility** for this project. The native URP support is critical for the seamless loading of GLTF models on the Oculus Quest platform without any shader issues.

**UnityGLTF**, despite its strong feature set, was less suitable due to its lack of URP support, requiring additional workarounds or library extensions.

> The key differences lie in their complexity and features. While UnityGLTF boasts more features, it seems like an extremely messy project, and having to compile a DLL for it to work makes it inaccessible to most of the people who actually need it.
GLTFUtility is built with simplicity as the main focus, uses no external libraries, and is easy to navigate for potential contributors. It's plug and play. 

- [Thor Brigsted](https://forum.unity.com/threads/gltfutility-a-simple-gltf-plugin.654319/), creator of GLTFUtility

**UniGLTF** was a close competitor and offered much of what **GLTFUtility** provided, including URP support. However, I leaned towards **GLTFUtility**, as it is also designed with a focus on simplicity and performance which could prove beneficial when loading models at runtime on a device like the Oculus Quest 2.

## Implementation

Getting GLTFUtility up and running was quite straightforward. After adding it to my Unity project, I set up a basic script to load a `.gltf` file into my scene.

The GLTFUtility package also provides an asynchronous loading option which is useful for loading complex 3D models without freezing the main thread. 

```csharp
using Siccity.GLTFUtility;
using System.Collections;
using System.IO;
using UnityEngine;

public class GLTFLoader : MonoBehaviour
{
    private string relativeFilePath = "YourFile.gltf";

    private void Start()
    {
        string absoluteFilePath = Path.Combine(Application.streamingAssetsPath, relativeFilePath);
        StartCoroutine(LoadGLTF(absoluteFilePath));
    }

    private IEnumerator LoadGLTF(string filePath)
    {
        GameObject loadedObject = null;

        Importer.ImportGLTFAsync(filePath, new ImportSettings(), (result, animations) =>
        {
            loadedObject = result;
        });

        while (loadedObject == null) yield return null;

        loadedObject.transform.SetParent(transform);
        loadedObject.transform.localPosition = Vector3.zero;
    }
}
```

Note: For this project, place your `.gltf` file in the `StreamingAssets` folder, which is located at `YourUnityProject/Assets/StreamingAssets`. Ensure the `relativeFilePath` variable in the script matches your `.gltf` file's name.

The source code for this project can be found on [GitHub](https://github.com/CR-Daniel/GLTFUtility-example-use).