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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

I wonder if there's a way to turn off all debug.drawray/drawline calls without if checks?

Discussion in 'Scripting' started by Deleted User, Jan 15, 2017.

  1. Deleted User

    Deleted User

    Guest

    Debug.logger.logEnabled=false;

    I found that one, which is super great; now I can string Debug.Logs that are sort of silly/redundant/useless informing me of my game system's states everywhere and also turn the debug off without turning it off in the build settings.

    The reason that's a concern is that Debug.Log does a stack trace (which is good) but that takes time, so it's nice to be able to toggle it. Drawray doesn't do a stack trace, naturally.

    I wish there was a similar toggle for drawing rays and lines. I wonder if I could wrap it somehow?

    I could put Debug.Drawray in my own static method that way I wouldn't need to rewrite an if-check every time, that's what I'm gonna do now, but it does an extra if-check for every one. Then again, then I have a method call to a method doing nothing even if I turn debugging off in the build settings, so maybe that's a bad idea?

    You guys typically just leave your draw rays on all the time? I don't feel like making them and then removing them, I want to leave them in so I can see em' in the future.
     
  2. Deleted User

    Deleted User

    Guest

  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Use conditional compilation:

    Code (csharp):
    1. #if UNITY_EDITOR
    2. // do stuff only in editor; code does not exist in builds
    3. #endif
    --Eric
     
    Gametyme and Kiwasi like this.
  4. Deleted User

    Deleted User

    Guest

    Oh, thank you. If I turn off the debug in the build settings, wouldn't that be doing the same thing though?
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    No, that's only for Debug.Log; other stuff like DrawRay calls are still in the build. (So is Debug.Log for that matter, but I assume the Log function just returns without doing anything in that case, like DrawRay. However they are still technically taking time to run, just not very much.)

    --Eric
     
  6. Deleted User

    Deleted User

    Guest

    Interesting, and good to know.

    With that knowledge I'll do both things. I'm gonna make a class that just draws rays and such with my if statements to toggle the debug work on and off, then I'll put it and every call to it as conditionally not compiled.

    It wouldn't really be worth doing but I'm doing some stuff with Vector2's and I've got a couple little methods that just draw a ray given Vector2's instead of 3's.
     
  7. alfreema

    alfreema

    Joined:
    Dec 31, 2016
    Posts:
    6
    OK, I would like to toggle all Debug.Log statements on and off, so I searched and stumbled on this pretty recent thread. Any hints on where to change this: Debug.logger.logEnabled=false; would be super helpful!
     
  8. VengeanceDesign

    VengeanceDesign

    Joined:
    Sep 7, 2014
    Posts:
    84
    Why not create a static bool in one of your classes called Global_Enable_DebugLines. Have it be false by default. In a class like a game manager class have this in the Awake function:
    #if UNITY_EDITOR
    GameManager.Global_Enable_DebugLines = true;
    #endif

    Remember to make the variable false in the OnDestroy() method

    Then search your entire project and do a replace:
    Original: Debug.DrawRay
    Replace With: if (GameManager.Global_Enable_DebugLines) Debug.DrawRay

    You can also have on-off switches for a particular script instance, so that only certain instances can do debug drawing if debug draws are enabled at all. Then your check becomes
    Original: if (GameManager.Global_Enable_DebugLines) Debug.DrawRay
    Replace With: if (GameManager.Global_Enable_DebugLines && this.debugDrawEnabled) Debug.DrawRay

    Having a class to handle all debug draws is good program design but for a game I think it may be better to avoid the overhead of the extra method call. In your build you'd be wasting CPU time on method calls that do nothing. I'd personally prefer to just prevent the attempt of drawing the ray.
     
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
  10. Deleted User

    Deleted User

    Guest

    Just wanted to say I was revisiting this topic because my solution was clunky and yours is a brilliant answer.