【Unity】デバッグログ無効化と小ネタ

DEVELOP, Unity

ログ出力は思っている以上に重い。なのでリリースビルドだったりでは無効化します。恐らくこれが最も有効な手段で、別名のClassを作らずに済む。

【Unity】リリース時にDebug.Logを出力しないようにする – noracle

//#define DISABLE_LOG
using UnityEngine;
#if DISABLE_LOG
public static class Debug
{
	//DUMMYは定義しなくて良い。
	[System.Diagnostics.Conditional("DUMMY")] public static void Assert(bool condition, string message, Object context) {}
	[System.Diagnostics.Conditional("DUMMY")] public static void Assert(bool condition, object message, Object context) {}
	[System.Diagnostics.Conditional("DUMMY")] public static void Assert(bool condition, string message) {}
	[System.Diagnostics.Conditional("DUMMY")] public static void Assert(bool condition, object message) {}
	[System.Diagnostics.Conditional("DUMMY")] public static void Assert(bool condition, Object context) {}
	[System.Diagnostics.Conditional("DUMMY")] public static void Assert(bool condition) {}
	[System.Diagnostics.Conditional("DUMMY")] public static void AssertFormat(bool condition, string format, params object[] args) {}
	[System.Diagnostics.Conditional("DUMMY")] public static void AssertFormat(bool condition, Object context, string format, params object[] args) {}
	[System.Diagnostics.Conditional("DUMMY")] public static void Break() {}
	[System.Diagnostics.Conditional("DUMMY")] public static void ClearDeveloperConsole() {}
	[System.Diagnostics.Conditional("DUMMY")] public static void DebugBreak() {}
	[System.Diagnostics.Conditional("DUMMY")] public static void DrawLine(Vector3 start, Vector3 end, Color color, float duration, bool depthTest) {}
	[System.Diagnostics.Conditional("DUMMY")] public static void DrawLine(Vector3 start, Vector3 end, Color color, float duration) {}
	[System.Diagnostics.Conditional("DUMMY")] public static void DrawLine(Vector3 start, Vector3 end) {}
	[System.Diagnostics.Conditional("DUMMY")] public static void DrawLine(Vector3 start, Vector3 end, Color color) {}
	[System.Diagnostics.Conditional("DUMMY")] public static void DrawRay(Vector3 start, Vector3 dir, Color color, float duration) {}
	[System.Diagnostics.Conditional("DUMMY")] public static void DrawRay(Vector3 start, Vector3 dir, Color color, float duration, bool depthTest) {}
	[System.Diagnostics.Conditional("DUMMY")] public static void DrawRay(Vector3 start, Vector3 dir) {}
	[System.Diagnostics.Conditional("DUMMY")] public static void DrawRay(Vector3 start, Vector3 dir, Color color) {}
	[System.Diagnostics.Conditional("DUMMY")] public static void Log(object message) {}
	[System.Diagnostics.Conditional("DUMMY")] public static void Log(object message, Object context) {}
	[System.Diagnostics.Conditional("DUMMY")] public static void LogAssertion(object message, Object context) {}
	[System.Diagnostics.Conditional("DUMMY")] public static void LogAssertion(object message) {}
	[System.Diagnostics.Conditional("DUMMY")] public static void LogAssertionFormat(Object context, string format, params object[] args) {}
	[System.Diagnostics.Conditional("DUMMY")] public static void LogAssertionFormat(string format, params object[] args) {}
	[System.Diagnostics.Conditional("DUMMY")] public static void LogError(object message, Object context) {}
	[System.Diagnostics.Conditional("DUMMY")] public static void LogError(object message) {}
	[System.Diagnostics.Conditional("DUMMY")] public static void LogErrorFormat(string format, params object[] args) {}
	[System.Diagnostics.Conditional("DUMMY")] public static void LogErrorFormat(Object context, string format, params object[] args) {}
	[System.Diagnostics.Conditional("DUMMY")] public static void LogException(System.Exception exception, Object context) {}
	[System.Diagnostics.Conditional("DUMMY")] public static void LogException(System.Exception exception) {}
	[System.Diagnostics.Conditional("DUMMY")] public static void LogFormat(Object context, string format, params object[] args) {}
	[System.Diagnostics.Conditional("DUMMY")] public static void LogFormat(string format, params object[] args) {}
	[System.Diagnostics.Conditional("DUMMY")] public static void LogWarning(object message) {}
	[System.Diagnostics.Conditional("DUMMY")] public static void LogWarning(object message, Object context) {}
	[System.Diagnostics.Conditional("DUMMY")] public static void LogWarningFormat(string format, params object[] args) {}
	[System.Diagnostics.Conditional("DUMMY")] public static void LogWarningFormat(Object context, string format, params object[] args) {}
    // 拡張
    [System.Diagnostics.Conditional("DUMMY")] public static void Log(object _message, string color) {}
}
#else
public static class Debug
{
    public static void Assert(bool condition, string message, Object context)
    {
        if(!condition)
        {
            UnityEngine.Debug.Assert(condition, message, context);
        }
    }
	public static void Assert(bool condition, object message, Object context)
    {
        if(!condition)
        {
            UnityEngine.Debug.Assert(condition, message, context);
        }
    }
	public static void Assert(bool condition, string message)
    {
        if(!condition)
        {
            UnityEngine.Debug.Assert(condition, message);
        }
    }
	public static void Assert(bool condition, object message)
    {
        if(!condition)
        {
            UnityEngine.Debug.Assert(condition, message);
        }
    }
	public static void Assert(bool condition, Object context)
    {
        if(!condition)
        {
            UnityEngine.Debug.Assert(condition);
        }
    }
	public static void Assert(bool condition)
    {
        if(!condition)
        {
            UnityEngine.Debug.Assert(condition);
        }
    }
	public static void AssertFormat(bool condition, string format, params object[] args)
    {
        if(!condition)
        {
            UnityEngine.Debug.AssertFormat(condition, format, args);
        }
    }
	public static void AssertFormat(bool condition, Object context, string format, params object[] args) 
	{
        if(!condition)
        {
    	    UnityEngine.Debug.AssertFormat(condition, context, format, args);
    	}
	}
	
	public static void Break()
	{
	    UnityEngine.Debug.Break();
	}
	
	public static void ClearDeveloperConsole()
	{
	    UnityEngine.Debug.ClearDeveloperConsole();
	}
	
	public static void DebugBreak()
	{
	    UnityEngine.Debug.DebugBreak();
	}
	
	public static void DrawLine(Vector3 start, Vector3 end, Color color, float duration, bool depthTest)
	{
	    UnityEngine.Debug.DrawLine(start, end, color, duration, depthTest);
	}
	
	public static void DrawLine(Vector3 start, Vector3 end, Color color, float duration)
	{
	    UnityEngine.Debug.DrawLine(start, end, color, duration);
	}
	
	public static void DrawLine(Vector3 start, Vector3 end)
	{
	    UnityEngine.Debug.DrawLine(start, end);
	}
	
	public static void DrawLine(Vector3 start, Vector3 end, Color color)
	{
	    UnityEngine.Debug.DrawLine(start, end, color);
	}
	
	public static void DrawRay(Vector3 start, Vector3 dir, Color color, float duration)
	{
	    UnityEngine.Debug.DrawRay(start, dir, color, duration);
	}
	
	public static void DrawRay(Vector3 start, Vector3 dir, Color color, float duration, bool depthTest)
	{
	    UnityEngine.Debug.DrawRay(start, dir, color, duration, depthTest);
	}
	
	public static void DrawRay(Vector3 start, Vector3 dir)
	{
	    UnityEngine.Debug.DrawRay(start, dir);
	}
	public static void DrawRay(Vector3 start, Vector3 dir, Color color)
	{
	    UnityEngine.Debug.DrawRay(start, dir, color);
	}
	public static void Log(object message)
	{
	    UnityEngine.Debug.Log(message);
	}
	
	public static void Log(object message, Object context)
	{
	    UnityEngine.Debug.Log(message, context);
	}
	
	public static void LogAssertion(object message, Object context)
	{
	    UnityEngine.Debug.LogAssertion(message, context);
	}
	
	public static void LogAssertion(object message)
	{
	    UnityEngine.Debug.LogAssertion(message);
	}
	
	public static void LogAssertionFormat(Object context, string format, params object[] args)
	{
	    UnityEngine.Debug.LogAssertionFormat(context, format, args);
	}
	
	public static void LogAssertionFormat(string format, params object[] args)
	{
	    UnityEngine.Debug.LogAssertionFormat(format, args);
	}
	
	public static void LogError(object message, Object context)
	{
	    UnityEngine.Debug.LogError(message, context);
	}
	
	public static void LogError(object message)
	{
	    UnityEngine.Debug.LogError(message);
	}
	
	public static void LogErrorFormat(string format, params object[] args)
	{
	    UnityEngine.Debug.LogErrorFormat(format, args);
	}
	
	public static void LogErrorFormat(Object context, string format, params object[] args)
	{
	    UnityEngine.Debug.LogErrorFormat(context, format, args);
	}
	
	public static void LogException(System.Exception exception, Object context)
	{
	    UnityEngine.Debug.LogException(exception, context);
	}
	
	public static void LogException(System.Exception exception)
	{
	    UnityEngine.Debug.LogException(exception);
	}
	
	public static void LogFormat(Object context, string format, params object[] args)
	{
	    UnityEngine.Debug.LogFormat(context, format, args);
	}
	
	public static void LogFormat(string format, params object[] args)
	{
	    UnityEngine.Debug.LogFormat(format, args);
	}
	
	public static void LogWarning(object message)
	{
	    UnityEngine.Debug.LogWarning(message);
	}
	
	public static void LogWarning(object message, Object context)
	{
	    UnityEngine.Debug.LogWarning(message, context);
	}
	
	public static void LogWarningFormat(string format, params object[] args)
	{
	    UnityEngine.Debug.LogWarningFormat(format, args);
	}
	
	public static void LogWarningFormat(Object context, string format, params object[] args)
	{
	    UnityEngine.Debug.LogWarningFormat(context, format, args);
	}
    // 拡張
    static public void Log(object _message, string color)
    {
        UnityEngine.Debug.Log("<color=" + color + ">" + _message + "</color>");
    }
}
#endif

DISABLE_LOG定義を有効にするとデバッグログが一通り消える。

小ネタ、Assertに事前conditionチェック

public static void Assert(bool condition, string message, Object context)
{
    if(!condition)
    {
        UnityEngine.Debug.Assert(condition, message, context);
    }
}

素直に関数を呼ばず、わざわざconditionを見る様にしています。ifの中でブレイクポイントを張っておけば、アサートの条件にヒットした際に簡単にコールスタックが追えます。

Posted by kazupon