Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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 nvand · Jul 29, 2013 at 04:29 PM · loadingfreezeloadlevelloadlevelasyncunitypro

LoadLevelAsync is acting like LoadLevel

I am having trouble getting LoadLevelAsync to work correctly. For some reason it seems to act just like LoadLevel. The loading time is almost the exact same and when the user clicks on the gui to pick which level to load next it freezes all movement while it attempts to load the next level. All I need is for the user to be able to continue walking around the scene while the next scene is loaded. Loading time isn't as important. Here is a small snippet of my code, and I just can not figure out what the problem is.

 void OnGUI()

{

     Rect[] guiRects = {Rect1, Rect2, Rect3, Rect4};
     string[] configNames = {"Level1", "Level2", "Level3", "Level4"};
 
     for(int i = 0; i < guiRects.Length; i++)
     {
        if(GUI.Button(guiRects[i], configNames[i]))
             StartCoroutine(this.LoadLevel(configNames[i]))
     }
 
 }
 
 
 IEnumerator LoadLevel(string name)
 {
     yield return new WaitForSeconds(1.5f);

     //nextLevel is one of the class fields
     nextLevel = Application.LoadLevelAsync(name);
     while (!nextLevel.isDone)
     { 
          yield return 0; 
     }        
 }
 
 
Comment
Add comment · Show 5
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 Bunny83 · Jul 29, 2013 at 04:51 PM 0
Share

Just in case: Do you actually have Untiy pro?

avatar image nvand · Jul 29, 2013 at 04:57 PM 0
Share

Yes I have Unity Pro

avatar image Bunny83 · Jul 29, 2013 at 05:13 PM 0
Share

When you put a Debug.Log in your while loop, how often does it get called?

avatar image nvand · Jul 29, 2013 at 05:17 PM 0
Share

It only got called once and that was right as the next scene loaded

avatar image svendkiloo · Jan 18, 2016 at 08:03 AM 0
Share

I believe you can yield directly on the LoadLevelAsync call (it returns an AsyncOperation).

5 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by logicalerror · Sep 17, 2014 at 01:33 PM

I discovered that LoadLevelAsync behaves like LoadLevel depending on which thread it's running.

Unity has this nasty undocumented behavior that in Start/Awake it's running on the main thread (which makes LoadLevelAsync behave like LoadLevel) and everywhere else it might not run on the main thread. Update seems to make LoadLevelAsync asynchronous for me .. on PC at least.

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 JanWosnitza · Oct 16, 2014 at 09:45 AM 0
Share

Had a similar problem, when starting a coroutine in the first OnEnable. yield return null; fixed it.

(yield return null; is the same as yield return new WaitForEndOfFrame(); but does not impact garbage-collection)

avatar image nat-soragge · Jul 17, 2015 at 11:18 PM 0
Share

You save a lot of my time! Thank you.

avatar image
0

Answer by Stevenwithaph · Jul 29, 2013 at 05:15 PM

 while (!nextLevel.isDone)
 { 
    yield return 0; 
 }  

This line here might be the problem, I think you're pausing the application in the while loop.

Looking at the docs(http://docs.unity3d.com/Documentation/ScriptReference/Application.LoadLevelAsync.html) you may want to try something like this

 AsyncOperation async = Application.LoadLevelAsync("MyBigLevel");
 yield return async;


And just to make sure, async operations are unity pro only, so make sure you have unity pro setup.

Comment
Add comment · Show 1 · 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 Stevenwithaph · Jul 29, 2013 at 10:34 PM 0
Share

I don't know why my answer is just showing that tiny piece, there was a lot more.

avatar image
0

Answer by DVFrance · Aug 06, 2014 at 01:54 PM

I've wrote this maybe it could help you : (in C#)

 void OnGUI()
     {
         if(async != null)
         {
             loadingComplete = async.progress;
         }
         else
         loadingComplete = 0;
         GUI.DrawTexture(new Rect(0, 0, Screen.width * (loadingComplete), 50), Texture2D);
     }
 
     IEnumerator waitAsecond (float waitTime) 
     {
         yield return new WaitForSeconds (waitTime);
         StartCoroutine(Load ());
     }
 
     IEnumerator Load () 
     {
         async = Application.LoadLevelAsync("###");
         async.allowSceneActivation = false;
         yield return async.isDone;
         yield return loadingComplete = 100;
         SwitchScene();
     }
 
         private void SwitchScene()
     {
         if (async != null)
             async.allowSceneActivation = true;
     }


I still have a freeze but it's the best I could do, so if someone found a better way I'm interested !!!

Comment
Add comment · 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
0

Answer by smoggach · Aug 06, 2014 at 02:05 PM

While you're waiting for your level to load your coroutine is just returning 0 constantly. The loading is not what is slowing your program it is this loop.

Instead of yield return 0, use yield return new WaitForEndOfFrame()

Comment
Add comment · 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
0

Answer by J_Troher · Dec 08, 2014 at 01:26 AM

I've tried all of these answers and none of them work for me. lol. Also Using Pro.

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

LoadSceneOnClicks.LoadAsynchronously(int)': not all code paths return a value 1 Answer

Why does Application.LoadLevelAsync cause a huge spike? 2 Answers

LoadLevel + LoadLeveAsync = freeze? 1 Answer

Load music only when everything is loaded 1 Answer

Title Screen loading animation not looped 1 Answer


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