[Unity] 빌드 시 Debug.Log()에 대한 성능 최적화

Date:     Updated:

카테고리:

태그:

이 글은 유니티에서 빌드 시 Debug.Log()에 대한 성능 최적화를 정리한 글입니다

🧵 성능 최적화

  • Debug.Log()는 실제 빌드에서도 작동되어 성능을 잡아먹는다
  • 그래서 성능 최적화를 하기 위해 빌드시에 Debug.Log()가 작동되지 않게 만들어야 한다
  • 실제로 글쓴이는 아래 코드와 같이 개인용 Logger 클래스를 통해 Debug.Log()를 호출하게 만들었다

🧶 사용법

  • Project SettingsPlayer로 가보면 Script Compilation이라는 항목을 찾을 수 있다
  • 빨간 표시를 해둔 곳에 USE_DEBUG를 넣어두면 유니티에서 Log가 보여지게 된다
  • 반대로 Log를 표시 안하고 싶으면 USE_DEBUG를 제거하면 된다

Logger 클래스

using UnityEngine;

public static class Logger
{
#if USE_DEBUG
    public static void Log(object message)
    {
        Debug.Log(message);
    }

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

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

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

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

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

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

    public static void LogWarningFormat(Object context, string message, params object[] args)
    {
        Debug.LogWarningFormat(context, message, args);
    }

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

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

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

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

    public static void LogException(System.Exception exception)
    {
        Debug.LogException(exception);
    }

    public static void LogException(System.Exception exception, Object context)
    {
        Debug.LogException(exception, context);
    }
    
    public static void Assert(bool condition)
    {
        Debug.Assert(condition);
    }

    public static void Assert(bool condition, Object context)
    {
        Debug.Assert(condition, context);
    }

    public static void AssertFormat(bool condition, string message, params object[] args)
    {
        Debug.AssertFormat(condition, message, args);
    }

    public static void AssertFormat(bool condition, Object context, string message, params object[] args)
    {
        Debug.AssertFormat(condition, context, message, args);
    }
#else
    public static void Log(object message) {}
    public static void Log(object message, Object context) {}
    public static void LogFormat(string message, params object[] args) {}
    public static void LogFormat(Object context, string message, params object[] args) {}
    public static void LogWarning(object message) {}
    public static void LogWarning(object message, Object context) {}
    public static void LogWarningFormat(string message, params object[] args) {}
    public static void LogWarningFormat(Object context, string message, params object[] args) {}
    public static void LogError(object message) {}
    public static void LogError(object message, Object context) {}
    public static void LogErrorFormat(string message, params object[] args) {}
    public static void LogErrorFormat(Object context, string message, params object[] args) {}
    public static void LogException(System.Exception exception) {}
    public static void LogException(System.Exception exception, Object context) {}
    public static void Assert(bool condition) {}
    public static void Assert(bool condition, Object context) {}
    public static void AssertFormat(bool condition, string message, params object[] args) {}
    public static void AssertFormat(bool condition, Object context, string message, params object[] args) {}
#endif
}


💻 열심히 공부해서 작성 중이니 오류나 틀린 부분이 있을 경우 
  언제든지 댓글 혹은 메일로 알려주시면 감사하겠습니다! 😸

맨 위로 이동하기

Unity 카테고리 내 다른 글 보러가기

댓글 남기기