• 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 Spoukey · May 11, 2015 at 12:12 PM · c#unity 5asyncloadlevelasync

Got a problem with Async!

Hey. I made a custom loadingscreen for my game but i got a problem:

  • List item

the loadingscreen last for 0.5 second or something like that so i cant see the "Facts" that i put on it and i want to make a "Press any key" button to load the new level after it is loaded 100%.

 void Update () 

{ StartCoroutine(DisplayLoadingScreen(levelToLoad)); }

 IEnumerator DisplayLoadingScreen(string level)
 {
     progress.enabled = true;
     fact.enabled = true;
     progressbar.enabled = true;
     know.enabled = true;
     background.enabled = true;

     progressbar.transform.localScale = new Vector3 (loadProgress,progressbar.transform.localScale.y,progressbar.transform.localScale.z);

     progress.text = "Loading Progress : " + loadProgress + "%";

         AsyncOperation async = Application.LoadLevelAsync (level);
         while(!async.isDone )
     {
         loadProgress = (int)(async.progress * 100);
         progress.text = "Loading Progress : " + loadProgress + "%";
         progressbar.transform.localScale = new Vector3 (async.progress, progressbar.transform.localScale.y, progressbar.transform.localScale.z);
         yield return null;

     }
 }

}

Ok so what i want to ask is: I have to put an if sentence to async or something like that If(async==is.Done && input.GetKeyDown("x")) Application.LoadLevel(level)

or something like that but it wont work because AsyncOperation async = Application.LoadLevelAsync (level); is gona load the level automatically when is 100%.

How should i do it?

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

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by Bunny83 · May 11, 2015 at 12:30 PM

You have to prevent Unity from switching to the new loaded scene by setting allowSceneActivation to false. Something like that:

 AsyncOperation async = Application.LoadLevelAsync (level);
 async.allowSceneActivation = false;
 while(!async.isDone || !Input.GetKeyDown("x"))
 {
     loadProgress = (int)(async.progress * 100);
     progress.text = "Loading Progress : " + loadProgress + "%";
     progressbar.transform.localScale = new Vector3 (async.progress, progressbar.transform.localScale.y, progressbar.transform.localScale.z);
     yield return null;
 }
 async.allowSceneActivation = true;

You might want to replace this ugly condition:

 while(!async.isDone || !Input.GetKeyDown("x"))

with

 while( !(async.isDone && Input.GetKeyDown("x")) )

by applying simply boolean logic ^^

Comment

People who like this

0 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 Spoukey · May 11, 2015 at 12:43 PM 0
Share

Tnx for help but i manage to do it without allowSceneActivation. Now i dont know how to count how much %of the lvl load , it is always at 100%.

avatar image Bunny83 · May 11, 2015 at 03:59 PM 0
Share

@Spoukey:
How you "managed" it without it? Also how large are your levels? If they aren't that heavy it probably loads too fast...

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

problem with Task in update? 1 Answer

Loading Bar in Different Scenes 1 Answer

Task and async Update 0 Answers

Task await in the async update 0 Answers

Load level async freezing animation for several seconds 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