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
7
Question by siaran · Mar 27, 2015 at 11:56 PM · unity5loadlevelasync

Loadlevel.async stops at ~90%?

Hi all, tried to use the loadlevelasync function, as I understand those are useable with the free version of unity 5? If they're not, well, I guess that would explain it...

If they are, well, here is my relevant code:

 AsyncOperation loadLevel;

 IEnumerator LoadNextLevel(){
     loadLevel = Application.LoadLevelAsync(scene);
     loadLevel.allowSceneActivation = false;
     yield return loadLevel;
     Debug.Log("loading complete");

 }
 void Update(){

     if(loadLevel != null){
         loadBar.fillAmount = loadLevel.progress;
         if(loadLevel.isDone){
             beginButton.interactable = true;
         }else{
             beginButton.interactable = false;
             //backButton.interactable = false;
         }
     }
 }

void Start () {

     StartCoroutine(LoadNextLevel());
 }

I don't get any errors or anything, the loadbar fills to about 90% and then just...stops, and I never see my 'loading complete' debug statement. So...am I doing something wrong or do I have to get the pro version in order for this to work?

Comment
Add comment · Show 2
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 Buka · Apr 28, 2015 at 05:31 PM 0
Share

I also want to answer to this question since i am working on a game where i have to create a lot of colliders and other stuff on scene in start function. So basically when i call function to load scene my bar gets to 90% same as yours and stops there so i have to wait another 20 - 30 seconds for everything to load but bar not moving up, it is stuck at 90% and screen freeze until level is loaded. Don't know what to do

avatar image Buka · Apr 29, 2015 at 12:42 PM 0
Share

I also want to answer to this question since i am working on a game where i have to create a lot of colliders and other stuff on scene in start function. So basically when i call function to load scene my bar gets to 90% same as yours and stops there so i have to wait another 20 - 30 seconds for everything to load but bar not moving up, it is stuck at 90% and screen freeze until level is loaded. Don't know what to do

4 Replies

· Add your reply
  • Sort: 
avatar image
12
Best Answer

Answer by ThermalFusion · Apr 29, 2015 at 12:17 PM

AsyncOperation.isDone will never be true if AsyncOperation.allowSceneActivation is false. Progress will stop at around 0.9f. You can work around this to do whatever needs to be done before the final load takes place by waiting until progress has reached 0.9f, and end with allowing the scene activation again after you've done what you need to prior to that. Example:

 using UnityEngine;
 using System.Collections;
 
 public class LoadLevel : MonoBehaviour {
     public string level;
     private string progress = "";    // Name of scene to load.
     private bool isLoading = false;
     private bool doneLoading = false;
     private bool allowLoading = false;
     private void OnGUI() {
         GUILayout.BeginVertical("box");
         if (!isLoading) {
             if (GUILayout.Button ("Begin Load")) {
                 isLoading = true;
                 StartCoroutine(LoadRoutine());
             }
         } else {
             if (doneLoading) {
                 if (GUILayout.Button ("Actually Load")) {
                     allowLoading = true;
                     StartCoroutine(LoadRoutine());
                 }
             }
             GUILayout.Label(progress);
         }
 
         GUILayout.EndVertical();
     }
     private IEnumerator LoadRoutine() {
         AsyncOperation op = Application.LoadLevelAsync(level);
         op.allowSceneActivation = false;
         while (op.progress < 0.9f) {
             // Report progress etc.
             progress = "Progress: " + op.progress.ToString();
             yield return null;
         }
         // Show the UI button to actually start loaded level
         doneLoading = true;
         while(!allowLoading) {
             // Wait for allow button to be pushed.
             progress = "Progress: " + op.progress.ToString();
             yield return null;
         }
         // Allow the activation of the scene again.
         op.allowSceneActivation = true;
     }
 }

One important thing to note is that anything that happens in Start or Awake on any of the MonoBehaviours in the new scene will not be performed asynchronously. It will only load the things that are in the scene before any code in it is actually run. Another example of an Awake/Start script in the loaded level that takes a long time to process shows that this is the case:

 using UnityEngine;
 using System.Collections;
 
 public class WorkAtStart : MonoBehaviour {
     public int calculations = 10000;
     public int iterations = 10000;
     public bool inAwake = true;
     public bool inStart = true;
     void Start () {
         if (inStart == true) {
             Calculate("Start");
         }
     }
     void Awake() {
         if (inAwake == true) {
             Calculate("Awake");
         }
     }
 
     string debugText = "Calculating...";
     private void Calculate(string funcName) {
         System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
         stopwatch.Start();
         Vector3 calc = Vector3.zero;
         // Do some random calculations:
         for (int i = 0; i < iterations; i++) {
             calc = Vector3.zero;
             for (int c = 0; c < calculations; c++) {
                 calc = calc + Random.onUnitSphere;
             }
         }
         stopwatch.Stop();
         Debug.Log (stopwatch.Elapsed);
         debugText += "\n" + funcName + ": " + stopwatch.Elapsed.ToString();
     }
 
     private void OnGUI() {
         GUILayout.BeginVertical("box");
         GUILayout.Label(debugText);
         GUILayout.EndVertical();
     }
 }
 

So if you need to do asynchronous things in Start/Awake you will need to fire up some Coroutines or do some threading inside the new scene.

Comment
Add comment · Show 4 · 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 JoeStrout · Apr 29, 2015 at 01:30 PM 1
Share

Sounds right to me! Nice answer, @ThermalFusion.

avatar image siaran · Apr 29, 2015 at 02:18 PM 0
Share

Well, I don't do anything really heavy in start() or awake(), I just wanted a loading bar, and to activate a button to go to the next level when it was done. One the one hand it makes sense that the load operation is never done without actually loading the scene, on the other hand, bit confusing.

Ah well, I'll just show the load bar in a separate loading screen scene ins$$anonymous$$d of during my mission briefing (I wanted to have a load bar while showing a mission briefing, then you could click 'start mission' when it was fully loaded and you had read everything and set some parameters).

avatar image ThermalFusion · Apr 30, 2015 at 12:05 AM 0
Share

I agree it is confusing, isDone should really turn to true even if allowSceneActivation is false, likewise progress should go to 1.

Regarding briefing during the loading, with a button when it's done, that's pretty much what my example does.

avatar image siaran · Apr 30, 2015 at 12:11 AM 0
Share

Yes, but your example loads to 90%, then allows the button to be pressed after that. I think that's something that could really confuse a player - it's 90% done loading but I can press "Start" now? (Yes, you could just cheat and add 10% to your visible load progress but I don't like doing things like that, it makes code confusing and weird).

avatar image
1

Answer by doug__ · Oct 25, 2015 at 05:59 AM

It's worth noting, for anyone else who finds this issue, that you cannot finish the LoadLevelAsync() operator if you don't yield from a MonoBehaviour context.

 public IEnumerator Open(string sceneId) {
   AsyncOperation op = Application.CanStreamedLevelBeLoaded(sceneId) ? Application.LoadLevelAsync(sceneId) : null;
   if (op != null) {
     op.allowSceneActivation = true;  // Do not defer loading
     while (!op.isDone) {
       yield return new WaitForSeconds(0.1f);
     }
   }
 }

This above function will never complete beyond 0.9 loaded if it is not yielded into a monobehaviour context, like this:

 public void LoadScene(MonoBehaviour context, string sceneId) {
   context.StartCoroutine(Open(sceneId));
 }

This is a bug in Unity; without the MonoBehaviour to track, the AsyncOperation will never complete; waiting for 0.9 does NOT fix this issue and under no circumstances should you ever use the hack:

 while (op.progress < 0.9) { ... }

As a workaround; this code above is incorrect; the new level will not have loaded when the function returns; specifically components and objects in the scene will not have spawned.

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 lekkii · Sep 02, 2020 at 02:43 AM 0
Share

Thank you, this is what I needed!

avatar image
0

Answer by JoeStrout · Apr 28, 2015 at 05:45 PM

If you "yield return loadLevel", then that's it for that method; nothing beyond that point will ever get executed. That's why you don't see your debug log. Use "yield return null" if you want to continue execution (asynchronously).

As for the progress, I too see the progress reports stop around 90%, but then isDone becomes true shortly after that. So it doesn't seem like a big deal to me. But note that it may be considerably smoother in a built app than when running within the editor, so be sure to test it there.

Comment
Add comment · Show 4 · 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 Buka · Apr 28, 2015 at 06:22 PM 0
Share

Thanks for answer, i am doing it with return null and yeah i get progress working and shown, but in my case i have to instantiate around 10000 game objects so that is intensive process and require time so my load is around 20 sec and i want people to still see loading progress, not to have feel of game freeze. I get instant load to 90% but then i have to wait over 15 seconds for level to load

avatar image JoeStrout · Apr 28, 2015 at 07:39 PM 0
Share

Even when running as a built app (not within the Unity editor)?

avatar image Buka · Apr 28, 2015 at 08:45 PM 0
Share

On build app it works normally (part with progress bar up to 90%) but it's the same when stuck at 90%, i still have wait for my colliders to be created on the scene. If it wasn't for those creation of so many objects, then it would be fine i guess when i build app. Collider creation is happening on start function (tried with awake, still the same) and i tried to add indicator for loading but not working when i stuck at 90%, basically screen freeze and all i can do is to wait for it to load

avatar image siaran · Apr 29, 2015 at 10:41 AM 0
Share

Changing the yield statement to return null does show my debug statement, but my programm still just freezes on 90% loaded. It does not complete shortly after - it doesn't complete (it hadn't completed after 5 $$anonymous$$utes and I figured it wasn't going to happen after that). Running a built app rather than in the editor didn't seem to make a difference.

avatar image
0

Answer by Noxury · Aug 01, 2016 at 04:59 PM

For lazy guys adding 11 to the progress value should do it.

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

27 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

Related Questions

Loading Progress using UNET 2 Answers

Reloading scene asynchronously causes stutter as the scene seems to restart a few times in a row. 1 Answer

Build game in Window phone 8 platform. 0 Answers

Scene items are not showing in hierarchy nor in scene window in unity 5 0 Answers

Close unity activity and start another activity - android 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