- Home /
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.
Answer by rh_galaxy · May 24 at 06:24 PM
[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();
Thanks for that.
But I removed the Resources folder altogether to see what happens but it's still the same issue. Not sure how to pinpoint what is causing the initial delay and why the delay doesn't occur when the scene is loaded the second time.
What do you mean by removing the resource folder? Don't you have any resources that loads with the scene? Isn't scene 3 the heaviest user of resources (textures/prefabs)?
What my code does (in real build/not in unity editor) is forcing all the textures and prefabs (In my case a one gigabyte file) into the system cache before I run LoadSceneAsync() that then accesses this file to put all things in the gfx-card. But it will only be effective once after the computer reboot, not successive runs of the program, so if it does this with every start of the program it might be that some resources are kept in the gfx-card even after you switch from scene 3 and back the first time...
Answer by UnityDeveloper21 · May 24 at 11:41 PM
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.
But how can the game function when you delete the textures and prefabs?
Anyway the profiler is very helpful, and you should be able to spot the difference between when the delay happens and when not.
You can save the logs with this code in your GameManager if you have one (something that is DontDestroyOnLoad that is running during scene transitions).
#define LOGPROFILERDATA //at top of file or in settings
#if LOGPROFILERDATA
int logProfilerFrameCnt = 0;
int logProfilerFileCnt = 0;
#endif
void Awake()
{
#if LOGPROFILERDATA
Profiler.logFile = "log" + logProfilerFileCnt.ToString();
Profiler.enableBinaryLog = true;
Profiler.enabled = true;
#endif
//...
}
void Update()
{
#if LOGPROFILERDATA
//unity profiler log files can only be viewed 300 frames at a time! :(
logProfilerFrameCnt++;
if(logProfilerFrameCnt>300)
{
logProfilerFileCnt++;
logProfilerFrameCnt = 0;
Profiler.logFile = "log" + logProfilerFileCnt.ToString();
}
#endif
//...
}
The game does not function when it gets to that screen but the UI loads but it still takes around 6 seconds the first time... And there are no resources to load. Hope that makes sense.
Your answer
Follow this Question
Related Questions
Scene Loading Issue. 0 Answers
how to save scene when switch another scene? 2 Answers
Attempting to Create an Essential SceneElements Checker 0 Answers
Load scenes to Dual Monitors 2 Answers