Scene takes long to load only first time

I have a simple game with 3 scenes. Each scene gets loaded after a click. The first 2 scenes load nicely but clicking from scene 2 to scene 3 “always” loads slowly the first time taking around 6 seconds.

But if I click and go back to scene 2 then come back to scene 3 it loads right away.

If I close the app and open it the same issue happens on scene 3 the first time .

My question really is on how to debug it?

I can’t tell what’s taking long. I looked at the profiler but not even sure where to begin.

Would be helpful for some debugging tips. Thanks.

[For PC] I had an issue with loading time (had 1000 MB textures, but later reduced it to 100 MB), and discovered that if I read the resource file in a thread while the user spends time in the menu, it reduced the load time significantly. Of course you need to have the memory for the OS to keep the file cached until Unity will access it…

It is a very unusual solution, but does the job. I don’t think it is easy to fix otherwise.

using System.IO;
using System.Threading;

//to avoid a 6 second freeze when the file is not in cache
byte[] preLoadBytes = new byte[1024 * 1024]; //1MB buffer
string preLoadDataPath;
Thread preLoadThread;
void PreLoadAssetsToCache()
{
    bool allRead = false;
    int iChunkSize = preLoadBytes.Length;
    int iChunkNr = 0;
    FileStream fs = null;
    try
    {
        fs = File.OpenRead(preLoadDataPath + "/sharedassets1.assets.resS");
    } catch { }
    while (fs != null && !allRead)
    {
        int fr = fs.Read(preLoadBytes, 0 + iChunkNr * iChunkSize, iChunkSize);
        if (fr != iChunkSize) allRead = true;
        iChunkNr++;
        Thread.Sleep(5);
    }
}

//code to run first in your program, once
preLoadDataPath = UnityEngine.Application.dataPath;
ThreadStart ts = new ThreadStart(PreLoadAssetsToCache);
preLoadThread = new Thread(ts);
preLoadThread.Priority = System.Threading.ThreadPriority.Lowest;
preLoadThread.Start();

Yes. I have resources but I actually deleted the folder to see if that would fix the problem but it didn’t. So I’m thinking it’s something else but not sure how to pinpoint.