Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Debug.Log() before other computations?

Discussion in 'Scripting' started by bw92, Feb 18, 2020.

  1. bw92

    bw92

    Joined:
    Apr 17, 2019
    Posts:
    20
    Hey all,

    I'm trying to use Debug.Log() as a way of communicating whether the webcam is currently playing or paused. I have code that looks like this:
    Code (CSharp):
    1. public void PauseOnClick()
    2.     {
    3.         if (webcamTexture.isPlaying)
    4.         {
    5.             webcamTexture.Pause();
    6.             UnityEngine.Debug.Log("Paused");
    7.         }
    8.         else if (!webcamTexture.isPlaying)
    9.         {
    10.             UnityEngine.Debug.Log("Debugging");
    11.  
    12.             // Perform some computations
    13.  
    14.             UnityEngine.Debug.Log("Paused");
    15.         }
    Essentially it says playing when cam is playing, pause when cam has been paused, and when the pause button is pressed again, do some computations with the paused image.

    I would like to display "Debugging" when Unity starts to perform the computations and "Paused" when it ends again to let the user know when the program is performing computations.

    At the moment, it is displaying Debugging after the computations have been performed and is immediately overwritten by Paused.

    This means the entire code section is being run before Debug.Log() takes effect. Is it possible to perform Debug.Log() first, before all other computations?
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    First, notice that the log isn't overwritten - it's still available if you open the Console window, along with all other Debugging.Log() Messages you wrote. The console window is very important, and it's the first window I add to my Unity Setup whenever I start a Project, before even adding any assets. The line that gets overwritten is the Status line at the very bottom, yes.

    Also, note that the reason it appears that the Debugging message is shown after the computations are done is because they are all done in the same frame, and it is likely that Unity only gets around to outputting/drawing the message when it returns from PauseOnClick, which means after all computations are done, and in that case it will quickly draw the two Messages Debugging and Paused quickly one after the other - and they both are available in full glory in the console.
     
    bw92 likes this.
  3. bw92

    bw92

    Joined:
    Apr 17, 2019
    Posts:
    20
    Yes, I think I framed my question poorly. What I meant was the status at the very bottom. I am trying to change that status while computations are being performed, then changed it back once they are done.

    Do you know if this is possible?
     
  4. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    You essentially need to wait x amounts of frames between setting your debugging message and setting it back. This is usually what Coroutines are used for. Plenty of resources out there that explain them way better than I ever could.
     
    bw92 likes this.
  5. bw92

    bw92

    Joined:
    Apr 17, 2019
    Posts:
    20
    Ahh, I'm fairly new so I was unfamiliar with them. Thanks for the heads up I'll check them out now :D.
     
  6. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Unity isn't well mutithreaded (if at all). Almost everything that happens, happens on the main thread. Unity supports a sort of cooperative multi-tasking via coroutines (as ThermalFusion Points out). When you have a computationally expensive task, all operation in unity halts (including screen updates and log output) until your computations are done. To change this, break your computations out into a coroutine, and break it down into segments that can 'yield' back processor time to Unity. That way, you can spread the computations over multiple frames, and the screen gets updated. As the very last line in your computations, Log 'Computations done' so you know they are done.
     
    bw92 likes this.