• 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
Question by Noxury · Jun 02, 2018 at 09:04 PM · scene-loadingmenuresources.loadloading screen

How to async load new Scene and Resources simultaneously?

Hey, I need some help on how to properly load resources at scene transitioning without noticing any lag. I searched the web for this, but it seems there are only answers of asyncronous loading just the desired scene.

.

My game is constructed by having a menu scene (=current scene) and a game scene (=target scene).

.

From the menu I can select a particular level. Each level is a prefab, which should get loaded into the game scene.

.

The prefab-loading should be part of the scene transitioning procedure as loading afterwards makes no sense. The application should load the prefab after the target scene is loaded (async.progress >= 0.9f) and after both are loaded the scene should activated.

This is what I have, but it will loop forever because allowSceneActivation = true is never reached.

 private IEnumerator LoadScene(string level, string prefab = ""){
     float loadcount = prefab == "" ? 1f : 2f;

     AsyncOperation async = SceneManager.LoadSceneAsync(level);
     async.allowSceneActivation = false;

     ResourceRequest prefabRequest = Resources.LoadAsync(prefab );
     DontDestroyOnLoad (Instantiate (prefabRequest.asset as GameObject));

     while (async.progress < 0.9f) {
         loadProgress = (async.progress) / loadcount * 0.9f;
         progressBar.value = loadProgress;
         yield return null;
     }
     if(prefabRequest != null){
         while (!prefabRequest.isDone) {
             loadProgress = (async.progress + prefabRequest.progress) / loadcount * 0.9f;
             progressBar.value = loadProgress;
             yield return null;
         }
      }
      async.allowSceneActivation = true;
 }
Comment

People who like this

0 Show 0
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

3 Replies

  • Sort: 
avatar image

Answer by Bshsf_9527 · Sep 22, 2020 at 12:31 PM

这一句“ async.allowSceneActivation = false; ” 会导致下一句:“ ResourceRequest prefabRequest = Resources.LoadAsync(prefab );” 永远加载不出来,这是Unity 内部的逻辑限制的。 https://docs.unity3d.com/ScriptReference/AsyncOperation-allowSceneActivation.html 里面说: When allowSceneActivation is set to true isDone can complete. While isDone is false, the AsyncOperation queue is stalled 很不幸,这个 queue 估计就只有一个。

Comment

People who like this

0 Show 0 · 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

Answer by rh_galaxy · Sep 22, 2020 at 01:26 PM

I have never used Resources.LoadAsync() so I wouldn't know about that, can it be loading while you have a LoadSceneAsync() in progress?

I could post how I do it but it is very similar to how you do. But I'm sorry to say it, there will be lagging, because it isn't fully async. Also you need to allow the scene to start after 90% which you have noticed. That will run all the Start() methods of your objects and if they take longer than a frame the game will appear to freeze. A trick I use is to set Time.timeScale = 0, and continue the loading of my level in Update() for however many loops I need, and when done I set Time.timeScale = 1.

Comment

People who like this

0 Show 0 · 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

Answer by laurentlavigne · Nov 01, 2021 at 06:11 PM

in unity you can only have one async load operation at a time i'm not joking and you cannot start another load request until the scene is activated i'm not even making that up

once you fix your code to follow these 2 rules you can change how much cpu load eats up by changing priority

Comment

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

87 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

Related Questions

Is there a perferred method for handling scene loading? 1 Answer

Passing information between scenes? 2 Answers

How to load scenes Asynchronously? 2 Answers

Particular loading screen works fine in Unity Editor, but gets stuck in the finished/built .exe version? 1 Answer

How do I code just a Loading scene with text and pictures in unity3d c# 2 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