Skip to content

A wrapper script for Unity debug calls to use conditional attributes in order to avoid debug code being compiled into release builds.

License

Notifications You must be signed in to change notification settings

JoebRogers/UnityDebug

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Maintenance License Ask Me Anything ! Twitter Follow Made With Unity

UnityDebug is a small script to wrap Unity's Debug logging calls to enforce strict calling intent on which debugging calls make it into compiled builds.

How does it work?

The script contains a static class InternalDebug, wrapping all UnityEngine.Debug calls with conditional attributes. Any calls made from this class will only be preserved when the project is built with the "Development Build" option checked in the "Build Settings" window, (or alternatively, the Development flag set in BuildOptions).

Why would I need this?

It's a common misconception for newer developers working with Unity to expect debug calls to be stripped out of game builds. This is not the case. It's all too easy to leave debug calls lying around within your scripts, that will be using up potentially precious performance under the hood (that Debug.LogWarning you left in Update is really going to come back to bite you).

Even for experienced developers, there's definite distinction between the debug calls you utilise during every day development and those you want to ship with final builds in order to aid in debugging user-submitted bugs. It can quickly become a cumbersome annoyance to find+comment/uncomment these calls, or even strip and replace every time you need to build.

By utilising a script like this, it'll not only save you work by automatically stripping out internal debug calls, but it's also explicit about the intent of particular logs. Being able to immediately discern the intent of particular calls, especially in a team environment is a big aid in streamlining the shipping process.

Things to know

There are two variants of this script you can use, one of which is the raw .cs file, which can be found in the src folder. The other, is a .dll file of the compiled script, which can be found in bin. The difference here, is that when using the raw script version, double-clicking on the logged message in the console window won't jump you to the line referred to, but rather the debug script. Using the compiled .dll file circumvents this issue.

Also, unlike the traditional Unity methods, there are also overloaded versions of all Log methods which take in a conditional argument (defaults to true). Allowing you to pass in your conditions, rather than wrapping your method calls - in turn, doing things this way also means that the condition itself is stripped from the build.

How do I use this?

It's as simple as dropping either the script file or .dll into your project window and replacing any calls to Debug. (or UnityEngine.Debug.) with InternalDebug. (or Utilities.InternalDebug. if you don't include the namespace). The arguments have been left as-is, with any additions being made as overloads, so nothing should need to change about your current project set up at all.

Enjoy!