When debugging scripts, being able to write out to a log is critical. The Unity Console is great, but sometimes you want to write to the screen or a file. The Debug Logger lets you do this in the simplest way possible.
With the Debug Logger, you can write to the console, write to the screen, write to a text file, or write to them all at once. You can also write to specific line positions on the screen in order to keep your view clean.
Note: The logger also uses a super fast object caching pool that you can use with other objects!
The package you download from the Unity Asset Store contains all of the scripts you need to use the Debug Logger. Feel free to use the code as-is or modify it.
To set up the Debug Logger, follow these simple steps:
1. Create a new unity project or open an existing one
The scripts you’ve just downloaded will work with any Unity project.
2. Add this package to your project
You can actually move the individual script files into other folders and it won’t hurt anything, but keeping them where they are will help keep your project organized.
3. If you want to write to the screen, add the “Log” script as a component to your camera
Using the logger can’t be simpler.
1. Include the Log object in your scripts
As with any classes you want to use, you have to tell your scripts they exist. To do this, you’ll need to add the “using” directive to the top of your script files that call the Log class’s functions.
2. Call the logger’s functions
Log.ScreenWrite("I am writing to the screen"); Log.ConsoleWrite("I am writing to the Unity console"); Log.FileWrite("I am writing to a file"); Log.Write("I am writing to the screen, console, and file");
In the “Scripts” folder, you’ll find a “LogDemo.cs” file. This is a simple example of logging at work. Simply add this script to your camera as a component.
In this example, the “Write” calls are tied to buttons so you can see them logging information in real time.
In addition to the simple approach shown above, the Debug Logger also supports some additional properties. These properites can be set in the camera’s inspector or through code by accessing the “Log” object.
One note: Updating the inspector properties while running won’t actually update the Log object.
Sets the file path and file name used when logging to a file. Note that if you never call “Log.FileWrite”, a file won’t be created and no resources are wasted.
Using “.\\Log.txt”, for example, would create a “Log.txt” file at the root of your project.
Adds the current “Time.time” value to your log entry.
Determines if we write to the file when the “Write” function is called.
Determines if we write to the screen when the “Write” function is called.
Determines if we write to the console when the “Write” function is called.
Height (in pixels) of each line we write to the screen.
Font size used when writing to the screen.
Font color used when writing to the screen.
When logging to a file, determines if we save the change to the file immediately or if we wait for the end of the frame.
Log.ScreenWrite(string rText, int rLine)
Writes the text to specify to the specific line on the screen. This isn’t a pixel cooridnate, but a line. The Y position is basically: rLine * Log.LineHeight. The contents is then cleared at the end of the frame so we can write again the next frame.
Writes to both the console and screen, but not to the file.
If you’re building a web/HTML5 project, you can’t access the file system. So, you’ll need to open the Log.cs file and comment out the very first line by changing it to this:
// #define USE_FILE_IO