Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by saldavonschwartz · Jun 26, 2015 at 05:40 AM · unity 5threadsloadlevelasync

LoadLevelAsync never executes on a background thread?

In spite of the what the Unity docs say, it seems to me LoadLevelAsync doesn't really work as advertised (or I'm screwing up of course -- which I'm trying to find out here).

I have a game made up of 2 scenes. A start scene where the player chooses a game type, and the main game scene where the game takes place. I'm trying to make it so that when the user chooses the game type, a transitional animation happens, after which the async loading of the level starts and in the meantime, I show the loading progress. Here's my code:

  public void onGameTypeChosen(int gameType) {
     StartCoroutine(startGame((GameType)gameType));
   }
   
   private IEnumerator startGame(GameType gameType) {
     GameManager._GameType = gameType;
     AsyncOperation loading = Application.LoadLevelAsync("Level1");
     animator.Play("ZoomIn", -1, 0f);
     progressIndicator.GetComponent<Animator>().Play("ProgressIndicatorFade", -1, 0f);
     while (!loading.isDone) {
       progressIndicator.endAngle = 360f * loading.progress;
       yield return null;
     }
   }

my expected result would be that the animator transitions to the 'zoom in' state, where the text that displayed the game type options fades into the camera, the another animator fades in the progress indicator and the thing keeps updating the loading progress in the UI until the level is fully loaded and the scene should just almost immediately show up.

What happens instead is that the zoom in animation happens, then the progress indicator fades in and it is already at 90%. After this, it takes about 5s on the editor and 30s on iOS to load the remaining 10% of the scene before it finally gets loaded. During this time (the infamous 10% left to load), the UI is clearly getting preempted and freezes at different intervals.

The first thing I did was print Thread.CurrentThread.IsBackground before and after the LoadLevelAsync call, as well as inside the while loop. It's always false.

I also tried replacing the yield return null with yield return loading, since it wouldn't be the first time that Unity doesn't document things, like the special meaning of returning an AsyncOperation from a coroutine. Still everything seems to be executing on the main thread anyway. The only difference is that if I yield return the op, the progress indicator doesn't even update.

Finally, I tried to explicitly dispatch LoadLevelAsync on a background thread by doing:

 Thread t = new Thread(() => Application.LoadLevelAsync("Level1"));
 t.IsBackground = true;
 t.Start();


But then I get the usual 'can only be called from the main thread."

So I have no idea what the deal is or how async loading is supposed to be used. At least for me, the code snippet in the docs does not work.

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

0 Replies

· Add your reply
  • Sort: 

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

21 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

Related Questions

Unity Threading Issue: Not able to delegate task 0 Answers

Google Daydream Thread with Unity 1 Answer

allowSceneActivation only works once in the editor when trying to delay loading a scene. 0 Answers

unity LoadSceneAsync works like LoadScene 0 Answers

Thread in Jar verify it in Unity 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