Skip to content

Instantly share code, notes, and snippets.

@kimsama
Created November 21, 2012 04:38
Show Gist options
  • Save kimsama/4123043 to your computer and use it in GitHub Desktop.
Save kimsama/4123043 to your computer and use it in GitHub Desktop.
It overrides UnityEngine.Debug to mute debug messages completely on a platform-specific basis. [Edit] Added isDebugBuild property.
//#if UNITY_EDITOR
//#define DEBUG
//#endif
using UnityEngine;
using System.Collections;
using System;
using System.IO;
using System.Text.RegularExpressions;
using UnityEngineInternal;
///
/// It overrides UnityEngine.Debug to mute debug messages completely on a platform-specific basis.
///
/// Putting this inside of 'Plugins' foloder is ok.
///
/// Important:
/// Other preprocessor directives than 'UNITY_EDITOR' does not correctly work.
///
/// Note:
/// [Conditional] attribute indicates to compilers that a method call or attribute should be
/// ignored unless a specified conditional compilation symbol is defined.
///
/// See Also:
/// http://msdn.microsoft.com/en-us/library/system.diagnostics.conditionalattribute.aspx
///
/// 2012.11. @kimsama
///
public static class Debug
{
public static bool isDebugBuild
{
get { return UnityEngine.Debug.isDebugBuild; }
}
[System.Diagnostics.Conditional("UNITY_EDITOR")]
public static void Log (object message)
{
UnityEngine.Debug.Log (message);
}
[System.Diagnostics.Conditional("UNITY_EDITOR")]
public static void Log (object message, UnityEngine.Object context)
{
UnityEngine.Debug.Log (message, context);
}
[System.Diagnostics.Conditional("UNITY_EDITOR")]
public static void LogError (object message)
{
UnityEngine.Debug.LogError (message);
}
[System.Diagnostics.Conditional("UNITY_EDITOR")]
public static void LogError (object message, UnityEngine.Object context)
{
UnityEngine.Debug.LogError (message, context);
}
[System.Diagnostics.Conditional("UNITY_EDITOR")]
public static void LogWarning (object message)
{
UnityEngine.Debug.LogWarning (message.ToString ());
}
[System.Diagnostics.Conditional("UNITY_EDITOR")]
public static void LogWarning (object message, UnityEngine.Object context)
{
UnityEngine.Debug.LogWarning (message.ToString (), context);
}
[System.Diagnostics.Conditional("UNITY_EDITOR")]
public static void DrawLine(Vector3 start, Vector3 end, Color color = default(Color), float duration = 0.0f, bool depthTest = true)
{
UnityEngine.Debug.DrawLine(start, end, color, duration, depthTest);
}
[System.Diagnostics.Conditional("UNITY_EDITOR")]
public static void DrawRay(Vector3 start, Vector3 dir, Color color = default(Color), float duration = 0.0f, bool depthTest = true)
{
UnityEngine.Debug.DrawRay(start, dir, color, duration, depthTest);
}
[System.Diagnostics.Conditional("UNITY_EDITOR")]
public static void Assert(bool condition)
{
if (!condition) throw new Exception();
}
}
@JamesMcMahon
Copy link

/// Important:
/// Other preprocessor directives than 'UNITY_EDITOR' does not correctly work.

I found that custom preprocessor directives do work in builds, but not in the editor.

After messing with it for a little, I got it to work using:

[Conditional("CUSTOM_CONDITION"), Conditional("UNITY_EDITOR")]

@zhangzhibin
Copy link

It works.
And, Unity 5.3 provides an easier solution: Debug.logger.logEnabled=false to disable it.

@Vamoss
Copy link

Vamoss commented Nov 23, 2016

Hi, thanks for sharing, it is missing two methods:

public static void LogFormat(string message, params object[] args) {
	UnityEngine.Debug.LogFormat(message, args);
}

public static void LogErrorFormat(string message, params object[] args) {
	UnityEngine.Debug.LogErrorFormat(message, args);
}

Cheers!

@shanecelis
Copy link

Re: Debug.logger.logEnabled=false. That will disable logging but it'll still leave all the call sites and any dynamic string construction will have allocations. This class is more performant because it'll elide the call sites: No method call. No argument construction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment