Remove UnityEngine.Debug logs from your code instead of hiding them

3 minute read

Premise

  • Unity 2019.4.9f1 (LTS)

reference

-How to completely erase logs with Unity (Qiita)
-[Unity] Do not output Debug.Log at the time of release (Rakuru Note)
-ConditionalAttribute class (MS Docs)

Thank you very much.

How it works

–The wrapper class is enabled by #if! DEBUGLOG only if the DEBUGLOG symbol is undefined.
–In the class, define a method that does nothing with the same name, covering all the methods of ʻUnityEngine.Debug. --The defined method will not be compiled by [System.Diagnostics.Conditional (“DEBUGLOG “)], ignoring the call itself. --The Conditional attribute tells the compiler to ignore the call when the symbol is undefined. --If the class is valid, Debug will be referred to this class instead of the original ʻUnityEngine.Debug.
–When the DEBUGLOG symbol is defined, the class definition itself becomes invalid and ʻUnityEngine.Debug` is used.

How to use

–In general, when outputting a log, define DEBUGLOG at the beginning of the source that defines the Debug class below, and comment it out when removing it.
–Alternatively, enter DEBUGLOG in Project Settings> Player> Scripting Define Symbols when outputting the log, and delete it when removing it.
–If you replace the symbol DEBUGLOG below with DEBUG, it will be output during development and removed during release without having to control the symbol in that way.

result

python


#if !DEBUGLOG
public class Debug {
	[System.Diagnostics.Conditional ("DEBUGLOG")] public static void Assert (bool condition, string message, Object context) { }
	[System.Diagnostics.Conditional ("DEBUGLOG")] public static void Assert (bool condition, object message, Object context) { }
	[System.Diagnostics.Conditional ("DEBUGLOG")] public static void Assert (bool condition, string message) { }
	[System.Diagnostics.Conditional ("DEBUGLOG")] public static void Assert (bool condition, object message) { }
	[System.Diagnostics.Conditional ("DEBUGLOG")] public static void Assert (bool condition, Object context) { }
	[System.Diagnostics.Conditional ("DEBUGLOG")] public static void Assert (bool condition) { }
	[System.Diagnostics.Conditional ("DEBUGLOG")] public static void Assert (bool condition, string format, params object [] args) { }
	[System.Diagnostics.Conditional ("DEBUGLOG")] public static void AssertFormat (bool condition, string format, params object [] args) { }
	[System.Diagnostics.Conditional ("DEBUGLOG")] public static void AssertFormat (bool condition, Object context, string format, params object [] args) { }
	[System.Diagnostics.Conditional ("DEBUGLOG")] public static void Break () { }
	[System.Diagnostics.Conditional ("DEBUGLOG")] public static void ClearDeveloperConsole () { }
	[System.Diagnostics.Conditional ("DEBUGLOG")] public static void DebugBreak () { }
	[System.Diagnostics.Conditional ("DEBUGLOG")] public static void DrawLine (Vector3 start, Vector3 end, Color color, float duration, bool depthTest) { }
	[System.Diagnostics.Conditional ("DEBUGLOG")] public static void DrawLine (Vector3 start, Vector3 end, Color color, float duration) { }
	[System.Diagnostics.Conditional ("DEBUGLOG")] public static void DrawLine (Vector3 start, Vector3 end) { }
	[System.Diagnostics.Conditional ("DEBUGLOG")] public static void DrawLine (Vector3 start, Vector3 end, Color color) { }
	[System.Diagnostics.Conditional ("DEBUGLOG")] public static void DrawRay (Vector3 start, Vector3 dir, Color color, float duration) { }
	[System.Diagnostics.Conditional ("DEBUGLOG")] public static void DrawRay (Vector3 start, Vector3 dir, Color color, float duration, bool depthTest) { }
	[System.Diagnostics.Conditional ("DEBUGLOG")] public static void DrawRay (Vector3 start, Vector3 dir) { }
	[System.Diagnostics.Conditional ("DEBUGLOG")] public static void DrawRay (Vector3 start, Vector3 dir, Color color) { }
	[System.Diagnostics.Conditional ("DEBUGLOG")] public static void Log (object message) { }
	[System.Diagnostics.Conditional ("DEBUGLOG")] public static void Log (object message, Object context) { }
	[System.Diagnostics.Conditional ("DEBUGLOG")] public static void LogAssertion (object message, Object context) { }
	[System.Diagnostics.Conditional ("DEBUGLOG")] public static void LogAssertion (object message) { }
	[System.Diagnostics.Conditional ("DEBUGLOG")] public static void LogAssertionFormat (Object context, string format, params object [] args) { }
	[System.Diagnostics.Conditional ("DEBUGLOG")] public static void LogAssertionFormat (string format, params object [] args) { }
	[System.Diagnostics.Conditional ("DEBUGLOG")] public static void LogError (object message, Object context) { }
	[System.Diagnostics.Conditional ("DEBUGLOG")] public static void LogError (object message) { }
	[System.Diagnostics.Conditional ("DEBUGLOG")] public static void LogErrorFormat (string format, params object [] args) { }
	[System.Diagnostics.Conditional ("DEBUGLOG")] public static void LogErrorFormat (Object context, string format, params object [] args) { }
	[System.Diagnostics.Conditional ("DEBUGLOG")] public static void LogException (System.Exception exception, Object context) { }
	[System.Diagnostics.Conditional ("DEBUGLOG")] public static void LogException (System.Exception exception) { }
	[System.Diagnostics.Conditional ("DEBUGLOG")] public static void LogFormat (Object context, string format, params object [] args) { }
	[System.Diagnostics.Conditional ("DEBUGLOG")] public static void LogFormat (string format, params object [] args) { }
	[System.Diagnostics.Conditional ("DEBUGLOG")] public static void LogWarning (object message) { }
	[System.Diagnostics.Conditional ("DEBUGLOG")] public static void LogWarning (object message, Object context) { }
	[System.Diagnostics.Conditional ("DEBUGLOG")] public static void LogWarningFormat (string format, params object [] args) { }
	[System.Diagnostics.Conditional ("DEBUGLOG")] public static void LogWarningFormat (Object context, string format, params object [] args) { }
}
#endif