• 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 t$$anonymous$$s, 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, w$$anonymous$$ch 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.

T$$anonymous$$s 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));

     w$$anonymous$$le (async.progress < 0.9f) {
         loadProgress = (async.progress) / loadcount * 0.9f;
         progressBar.value = loadProgress;
         yield return null;
     }
     if(prefabRequest != null){
         w$$anonymous$$le (!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

· Add your reply
  • 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. W$$anonymous$$le 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 w$$anonymous$$le 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% w$$anonymous$$ch 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

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

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

SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex); is working badly 1 Answer

,I want to save the execution result. 0 Answers

How to select scene based on device screen size? 1 Answer

Where do Application.LoadLevel() and Resources.Load() load from? 1 Answer

How can I make a loading screen with LoadSceneAsync 3 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