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 /
avatar image
1
Question by lorenzofman2 · Oct 03, 2018 at 01:56 AM · looploopingprocess

Update console in the same frame

Hi, I have two loops, one inside other. The process I am doing takes around 9 seconds with 8192 x 8192 iterations. I would like to print a percentage in the outer loop but it seems that console just update on the next frame after the loop.

Pseudo-code:

    for(int i = 0; i < 8192;i++){
         for(int j = 0; j < 8192;j++){
              // Do some crazy stuff here
         }
         Debug.Log(100f * i / 8192f + "%"); /* Doesn't print until next frame*/
    }

I understand why it would be a bad practice to print every time the console updates,in most cases unnecessary and process consuming but since I need it how can I force the console to update? Or I will need to write to a file in the HD to ensure the system stop processing.

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
Best Answer

Answer by gvergidis · Oct 03, 2018 at 08:26 AM

Hello.

No, it will not print in next frame. It will print when operation finishes. A frame is a loop of ALL commands of ALL scripts running every frame. C# is a serial programming language. What you need is a parallel programming here. When using loading screen, you can not rely on serial programming. That is why there are Coroutines. Try changing your code to this :

 private IEnumerator CrazyStuffRoutine()
 {
      for(int i = 0; i < 8192;i++){
          for(int j = 0; j < 8192;j++){
               // Do some crazy stuff here
               Debug.Log(100f * i / 8192f + "%"); /* Doesn't print until next frame*/
               
               yield return null;
          }
     }
 }

Yield return null means wait a frame, and continue on the next one. So expect this code to run at your fps speed. Ofcourse you can yield return null every 10 steps or 100. This piece of code is not serial and will not halt your total process until it finishes. To call coroutine just use :

 StartCoroutine(CrazyStuffRoutine());

I hope this helps.

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 gvergidis · Oct 03, 2018 at 11:08 AM 0
Share

Be adviced that using Log so many times will consume a great amount of memory. I would suggest to only show progress every 10 loops or 100. Also, wrap your log command inside an #if C# preprocessor directives like so : #if DEVELOP$$anonymous$$ENT_BUILD Debug.Log("Something to log :P"); #endif

This will only be included to your build if you select development build. For more informations about directives, take a look here : Preprocessor Directives

avatar image lorenzofman2 · Oct 05, 2018 at 03:01 PM 1
Share

Hi, Thanks for the answer! I liked how complete you wrote it.

I have thought about Coroutines but I didn't want to have to wait until the next frame. Using it every 10/100 steps will do it.

Best regards!

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

92 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

Related Questions

Looping animation while key down 1 Answer

Help! Unsaved work stuck in frozen unity, believed to be from infinite loop. What can i do? 2 Answers

problem with loop / unity crash 2 Answers

Change Color of Material in Loop 1 Answer

NEED HELP! Wave spawner just wont work!,NEED HELP! Wave spawner not working :( 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