Debug.Log extension

Hello

I want to extend function “Debug.Log()”.

But, I can’t know how to write.

using UnityEngine;

public static class Extensions
{
    bool isDebug = true;
    public static void Log(this Debug debug, object obj)
    {
        if (isDebug)
        {
        Debug.Log(obj);
        }
    }
}

The I want to do is “If it is debug, write log, but if it is not debug, write nothing”

Do you know how to write?

can’t?

Debug is a static class so an instance (this) never exists.

That being said - it’s not really clear what you’re even trying to do. Why does this need to be an extension method? What is ‘isDebug’? Why can’t you just create a wrapper class that does this logic and calls Debug.Log normally?

i did like this

#if !UNITY_EDITOR
#define DEBUG_LOG_OVERWRAP
#endif
using UnityEngine;

#if DEBUG_LOG_OVERWRAP
public static class Debug
{
    static public void Break ()
    {
        if (IsEnable ())
        {
            UnityEngine.Debug.Break ();
        }
    }

    static public void Log (object message)
    {
        if (IsEnable ()) {
            UnityEngine.Debug.Log (message);
        }
    }

    static public void Log (object message, Object context)
    {
        if (IsEnable ()) {
            UnityEngine.Debug.Log (message, context);
        }
    }

    static public void LogWarning (object message)
    {
        if (IsEnable ()) {
            UnityEngine.Debug.LogWarning (message);
        }
    }

    static public void LogWarning (object message, Object context)
    {
        if (IsEnable ()) {
            UnityEngine.Debug.LogWarning (message, context);
        }
    }

    static public void LogError (object message)
    {
        if (IsEnable ()) {
            UnityEngine.Debug.LogError (message);
        }
    }

    static public void LogError (object message, Object context)
    {
        if (IsEnable ()) {
            UnityEngine.Debug.LogError (message, context);
        }
    }

    static public void DrawLine (Vector3 start, Vector3 end, Color color, float duration = 0.0F, bool depthTest = true)
    {
        UnityEngine.Debug.DrawLine(start, end, color, duration, depthTest);
    }

    static bool IsEnable ()
    {
        return UnityEngine.Debug.isDebugBuild;
    }
}
#endif
4 Likes