DEV Community

GameDevToolLab
GameDevToolLab

Posted on

Fast File Loading Techniques in Unity

Temporarily Switch to 60 FPS During Loading and Load Multiple Files in Parallel

Even if your main gameplay runs at 30 FPS or another frame rate lower than 60 FPS, temporarily switching to 60 FPS during loading can improve loading times when reading multiple files.

Also, when loading multiple files, it is more efficient to start all load operations first and then wait for all of them to finish, rather than waiting for each one sequentially.

using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.AddressableAssets;

public class LoadSample : MonoBehaviour
{
    async void Start()
    {
        int prevFps = Application.targetFrameRate;
        int prevVSync = QualitySettings.vSyncCount;

        // Switch to 60 FPS only during loading
        QualitySettings.vSyncCount = 0;
        Application.targetFrameRate = 60;

        // Start multiple load operations at the same time
        var playerTask = Addressables.LoadAssetAsync<GameObject>("Player").Task;
        var enemyTask  = Addressables.LoadAssetAsync<GameObject>("Enemy").Task;
        var uiTask     = Addressables.LoadAssetAsync<GameObject>("BattleUI").Task;

        // Wait until all load operations are complete
        await Task.WhenAll(playerTask, enemyTask, uiTask);

        var playerPrefab = playerTask.Result;
        var enemyPrefab  = enemyTask.Result;
        var uiPrefab     = uiTask.Result;

        // Restore the FPS settings after loading is complete
        Application.targetFrameRate = prevFps;
        QualitySettings.vSyncCount = prevVSync;

        Instantiate(playerPrefab);
        Instantiate(enemyPrefab);
        Instantiate(uiPrefab);
    }
}
Enter fullscreen mode Exit fullscreen mode

There are two key points.

// Start everything first
var taskA = LoadA();
var taskB = LoadB();
var taskC = LoadC();

// Then wait for all of them at the end
await Task.WhenAll(taskA, taskB, taskC);
Enter fullscreen mode Exit fullscreen mode

If you load them sequentially, like this:

await LoadA();
await LoadB();
await LoadC();
Enter fullscreen mode Exit fullscreen mode

the waiting time accumulates.

On the other hand, if the load operations do not depend on each other, starting them at the same time and waiting for all of them together may reduce the total loading time.

However, loading many large assets at the same time can increase memory usage or cause spikes during asset expansion, so it is better to group them reasonably by screen or use case.

Run Network Requests and File Loading at the Same Time

When transitioning to a specific screen, you may need both of the following:

  • Fetch data from a network API
  • Load the prefab used to build the screen

If you load the prefab only after the network request finishes, the waiting time is added together.

var apiResult = await CallApi();
var prefab = await LoadPrefab();
Enter fullscreen mode Exit fullscreen mode

If the network request and the loading process do not depend on each other, it is more efficient to start them at the same time.

var apiTask = CallApi();
var prefabTask = LoadPrefab();

await Task.WhenAll(apiTask, prefabTask);

var apiResult = apiTask.Result;
var prefab = prefabTask.Result;
Enter fullscreen mode Exit fullscreen mode

Because the waiting time for the network request and the file loading overlaps, you can reduce the amount of time the user has to wait.

Top comments (0)