• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by UnityDeveloper21 · May 24, 2022 at 05:28 PM · scene-loadingscene-switching

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by rh_galaxy · May 24, 2022 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 w$$anonymous$$le 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 t$$anonymous$$nk 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 { }
     w$$anonymous$$le (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();
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image UnityDeveloper21 · May 24, 2022 at 10:13 PM 0
Share

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.

avatar image rh_galaxy UnityDeveloper21 · May 24, 2022 at 10:51 PM 0
Share

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...

avatar image
0

Answer by UnityDeveloper21 · May 24, 2022 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 t$$anonymous$$nking it's somet$$anonymous$$ng else but not sure how to pinpoint.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rh_galaxy · May 25, 2022 at 12:36 AM 0
Share

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
         //...
     }
avatar image UnityDeveloper21 · May 25, 2022 at 12:21 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

140 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Attempting to Create an Essential SceneElements Checker 0 Answers

Scene Loading Issue. 0 Answers

how to save scene when switch another scene? 2 Answers

Load scenes to Dual Monitors 2 Answers

AsyncOperation activating immediately even with async.allowSceneActivation = false; 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges