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 jhooks098 · Mar 25, 2015 at 11:52 PM · scene-loadinginstantiationasynchronous

LoadLevelAsync behaviour within a scene

I'm looking to achieve the same behaviour as LoadLevelAsync, but with in-scene operations. My levels are loaded dynamically from serialized data at the beginning of the scene.

I'm currently using a co-routine to initialize the level while a loading screen displays, but I am unable to show a loading animation as the co-routine seems to just about pause the scene anyway. As I understand this has to do with main thread vs multi-thread but my googling to get the same behaviour hasn't given me what I need.

tl;dr Is there anyway to achieve the same behaviour of LoadLevelAsync when you plan on populating the scene after it begins?

Comment
Add comment · Show 1
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 Huacanacha · Mar 26, 2015 at 10:56 PM 0
Share

Application.LoadLevelAdditiveAsync ?

1 Reply

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

Answer by Ramlock · Mar 26, 2015 at 07:36 AM

Maybe Threading is what you need, instead of Coroutines: https://msdn.microsoft.com/en-us/library/aa645740%28v=vs.71%29.aspx

I didn't understand, though, why you can't just use LoadLevelAsync instead. You said you plan on populating the scene after it begins, but while you do that all you show is a loading screen, so why do they need to be on the same scene? My suggestion is that you simple create a "Loading Scene" that displays your animation and starts the LoadLevelAsync, then when the level is loaded it'll change to the proper scene.

Hope that helps :D

Comment
Add comment · Show 6 · 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 jhooks098 · Mar 26, 2015 at 12:51 PM 0
Share

I can't use LoadLevelAsync because my scene is empty. It's only once the scene is loaded that I do some procedural loading. I've done some research on threading before but it appears I can't use the Unity API in another thread, so I can't instantiate and set parameters which is pretty much all I need to do. I'm curious how LoadLevelAsync does this because it seems I just need to emulate its behavior, if only I had the source or something.

avatar image Ramlock · Mar 26, 2015 at 01:44 PM 0
Share

I see. You said you are using a Coroutine, but are you using yield often enough? Cause as far as I know, they actually operate on the same thread, so you must prevent it from taking too long on a single step, otherwise you'll have a very low frame rate.

If yielding as often as you can is still not enough, try doing it with a timer (experiment on how long you need it to sleep between steps), and hopefully it'll work.

avatar image jhooks098 · Mar 26, 2015 at 05:34 PM 0
Share

Hmm, so what your saying is break up my loading sequence with numerous yields?

$$anonymous$$y current coroutine as it stands looks something like this

 IEnumerator $$anonymous$$yCoroutine()
 {
     while(!InitializeLevel())
        yield return "";
 }
 bool InitializeLevel()
 {
     // Generate Level
     return true;
 }

With that in $$anonymous$$d would something like breaking up the initialize level function with a step iterator be what your talking about?

 IEnumerator $$anonymous$$yCoroutine()
 {
     int i = 0;
     while(!InitializeLevel(i))
     {
         i++;
         yield return "";
     }
 }

 bool InitializeLevel(int i)
 {
 switch(i)
 {
 case 0:
 //   Load some stuff
 return false;
 case 1:
 //   Load more stuff
 return false;
 case 2:
 //   Load last stuff
 return true;
 }
 } 
 }

Doing this might help ya?

avatar image Ramlock · Mar 26, 2015 at 10:03 PM 0
Share

Yes yes, something like that. From what I see from your original script, it really wasn't behaving like a coroutine at all. Ins$$anonymous$$d, it was loading everything before ever attempting to pause.

But I still don't feel like the switch will give enough pauses for the game not to freeze. Coroutines work with frames, so they should have a pause every frame. In other words, the work they should do each time they're called is $$anonymous$$imal.

First of all, it makes no sense to keep the entire code outside the coroutine method, by doing so you're basically destroying everything the coroutine is there for.

Second, I don't know how you're generating your stuff, but if it's procedural as you said, I suppose it works with multiple loops. If that's the case, try having a yield return null; at every step of it.

I'll be waiting to see how it goes. :D

avatar image jhooks098 · Mar 26, 2015 at 10:37 PM 0
Share

ya i'm just getting the hang of coroutines and I understand how I can utilize them now after your explanation. I also see why my while() statement and separating everything into another function makes no sense (i did this emulating a coroutine example).

I've set it up now so my initialize game operations have numerous yield statements in between. I can't do quite as many as i would like as there is a lot of work being done in some of my operations but I've got it running marginally smoother.

Thanks for your help!

Show more comments

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

22 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

Related Questions

LoadLevelAdditiveAsync progress is always 0 2 Answers

Odd behaviour when preloading multiple scenes 1 Answer

Load menu via SceneManager 0 Answers

SceneManager.LoadScene used within promise (or async callback) does nothing 0 Answers

Get a reference to the newly loaded scene after a LoadSceneAsync() 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