Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 FargleBargle · May 11 at 06:55 PM · scene-loadingmultithreadingasynchronous

Can Jobs be used to load large scenes asynchronously without freezing?

I've struggled with async scene loading for years. My scenes are typically very large, and take 10+ seconds to load. This is because they use 2k x 2k terrains, dense Vegetation Studio landscaping, and realistic ocean water, plus lots of other objects.

My initial plan was to move the player to a persistent enclosed space or sub-scene - a hyperloop car, or a space warp bubble for instance - and project a scene transition video while destroying the old scene and using SceneManager.LoadSceneAsync to load the new one. Unfortunately async isn't really async, as has been reported elsewhere, and the player and video freeze for quite a long time once the new scene load reaches 90%.

So I have several questions:

  1. Can the new Jobs System be used to load a new scene in another thread, so it's fully ready to go, and then dump it all back into the main thread without as much lag?

  2. If not, are there any other thread safe ways to do this outside the main thread?

  3. If it can't be done at all using multi-threading, are there other ways to minimize the lag? Load the parts the player will see first for instance, and build up the rest over multiple frames to minimize impact?

I'm looking for solutions that are tested, and relatively simple to implement, either through code or Asset Store products. Most of the suggestions I've seen elsewhere are pretty vague, or show the person offering them just read them from the manual without actually using them in challenging scenes. They're also generally several years old, some going back to Unity 5, so I'm hoping there have been new developments in Unity 2021/2022. Thanks.

Comment
Add comment
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
0

Answer by rh_galaxy · May 11 at 09:40 PM

I'm not familiar with Jobs. But threading is good with the limitation that you are not allowed operations that involves Unity state.
The freezes will possibly be shorter, but will not be gone. I made a VR game where steady framerate is important, so I ended up fading screen to black.

Here is a piece of code for one extra thread I have in my game (modified/untested).

 using System.Threading;
 public class GameLevel : MonoBehaviour
 {
     Thread thread;
     ManualResetEvent event = new ManualResetEvent(false);
     bool next = false;
     void LoadThread()
     {
         //do something
         //...
         //pause until main thread detect it
         next = true;
         event.WaitOne();
         //generate final mesh for example
         //...
         loaded = true;
     }
     void LoadBegin()
     {
         ThreadStart ts = new ThreadStart(LoadThread);
         thread = new Thread(ts);
         thread.Priority = System.Threading.ThreadPriority.Lowest;
         thread.Start();
     }
     public bool LoadDone()
     {
         if (next)
         {
             //do things that must be with Unity
             //...
             next = false;
             event.Set();
         }
         return !thread.IsAlive;
     }
 }
 
 //in main thread
 AsyncOperation asyncLoad;
 bool loadBeginDone = false;
 bool loadDone = false;
 IEnumerator LoadAsyncScene()
 {
     asyncLoad = SceneManager.LoadSceneAsync(scenName, LoadSceneMode.Single);
     asyncLoad.allowSceneActivation = false;
     while (!asyncLoad.isDone)
     {
         //scene has loaded as much as possible
         if (asyncLoad.progress >= 0.9f)
         {
             if (!loadBeginDone)
             {
                 GameLevel.LoadBegin();
                 loadBeginDone = true;
             }
             else if (GameLevel.LoadDone())
             {
                 loadBeginDone = false;
                 asyncLoad.allowSceneActivation = true;
                 //all done, next
                 state++;
             }
         }
         yield return null;
     }
     loadDone = asyncLoad.isDone;
 }
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 FargleBargle · May 12 at 12:29 PM 0
Share

@rh_galaxy Thanks for the quick reply, and the code example. The reason I asked specifically about Jobs, was that it supposedly keeps the code thread-safe by default, making issues related to that less likely. While your solution doesn't use it, it does look fairly clean and straightforward, so should give me something to test at least.

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

184 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

AsyncOperation activating immediately even with async.allowSceneActivation = false; 0 Answers

Two async operations at the same time not working ? why ? 0 Answers

How to call NetworkClient.Ready() ? 1 Answer

LoadLevelAsync behaviour within a scene 1 Answer

LoadLevelAdditiveAsync progress is always 0 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