Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Jan 10, 2020 at 04:05 AM by Henderikus-Parekowhai.
avatar image
0
Question by Henderikus-Parekowhai · Jan 10, 2020 at 02:17 AM · noobmultithreadingfreezing

How to stop game freezing when doing large calculation? multi threading?

So i have a map generator that take a few seconds to generate and display. However, during these seconds the game completely freezes. Is there a way that i can do the calculation on a different thread and have the game not freeze when generating? Any help is greatly appreciated! :)

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 ShadyProductions · Jan 10, 2020 at 09:55 AM 0
Share

Seems normal to me that map generation can take a while, and usually this is only done during the start of the game level and a loading screen is displayed in most games.

1 Reply

  • Sort: 
avatar image
2

Answer by RendrWyre · Jan 10, 2020 at 02:47 AM

To multi-thread you can use Unity's Job system.

(https://docs.unity3d.com/ScriptReference/Unity.Jobs.IJob.html)


https://docs.unity3d.com/Manual/JobSystemSchedulingJobs.html


and if you're using jobs look into the burst compiler.


https://docs.unity3d.com/Packages/com.unity.burst@0.2/manual/index.html

Comment
Add comment · 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 Henderikus-Parekowhai · Jan 10, 2020 at 02:57 AM 0
Share

I have tried doing a simple test with unity Jobs and it still freezes while calculating. am i doing something wrong?

 public void Iterate(){
     NativeArray<int> RESULT = new NativeArray<int>(1, Allocator.TempJob);
     
             IterateJob iterateJob = new IterateJob {
                 RESULT = RESULT
                 //iterations = _iterations
             };
     
             JobHandle iterateJobHandle = iterateJob.Schedule();
             iterateJobHandle.Complete();
     
             Debug.Log(RESULT[0]);
     
             RESULT.Dispose();
 }
     public struct IterateJob : IJob
     {
         public NativeArray<int> RESULT;
     
         public void Execute()
         {
             RESULT[0] = 12412;
             for (int i = 0; i < 50000000; i++)
             {
                 RESULT[0] = (int)$$anonymous$$athf.Sqrt(RESULT[0]);
             }
         }
     }

avatar image Bunny83 Henderikus-Parekowhai · Jan 10, 2020 at 10:39 AM 2
Share

You call JobHandle.Complete inside your starting code which runs on the main thread. Read the manual. The Complete method will ensure the job has completed now. So if it hasn't been started yet it would be executed from inside Complete synchonosly on this thread and would block it.


If the job has already been started on a seperate thread you get a similar end result since the Complete method will wait / block until the job has finished since that's the point of the Complete method.


Running something on a seperate thread means it's executed asynchonously. So you won't get your results immediately. Whenever you wait inside your starting code for the completeion, it would be no difference than not using a seperate thread at all since you block the main thread until the job is done.


You seem to have used the example given here one-to-one, however you haven't read the comment above the Complete line which tells you that you usually shouldn't do this. Well the Unity documentation is known for its bad examples ^^.


Loading task are usually started in Start. To keep the game running before the job is completed you would check each frame if the job has completed and then do finalizing steps on the main thread. You could use the Update method to check for JobHandle.IsCompleted, however a coroutine would make more sense for one time loading tasks

 IEnumerator Load()
 {
     // prepare your job
     // [ ... ]
     
     // schedule your job
     JobHandle iterateJobHandle = iterateJob.Schedule();
     
     // check each frame until finished
     while (!iterateJobHandle.IsCompleted)
         yield return null;
     
     // job has finished, use your data
     // [ ... ]
 }

Follow this Question

Answers Answers and Comments

122 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

Related Questions

Multithreading freezes editor 1 Answer

How would I create a prefab that moves forward? 2 Answers

Ball physics for game - normal physics fine? 1 Answer

Vector3 randomize spawnpoints 1 Answer

Setting GameObject in a script. 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