LoginSignup
6
1

More than 1 year has passed since last update.

【Unity】Debug.Log 置き換えテンプレート

Last updated at Posted at 2022-10-01

Debug.Log() でイイじゃんね。て感じもありますが、最初にこれさえやっておけば後から最低限の対応ができるテンプレート。

  • Unity コンソールのログをダブルクリックしたときにロガーの .cs ファイルが開いたりしない。ちゃんとログを出力したスクリプトが開く。
  • ログがごちゃついてきたから一括でプリフィックス付けたい。色変えたい。エラーだけファイルやサーバーに記録を残したい。が後から可能。
    • => Debug.Log($"<color=#CC7700><b>[{nameof(MyOrg.App)}]</b></color> " + message);
  • スタックトレースが無駄に長くならない。
  • 一旦手を離れたモジュールからのログは非表示にしたい。
  • リリースビルドではエラー以外は出力しない、とかできる。
using System.Diagnostics;
using System.Runtime.CompilerServices;
using UnityEngine;
using Debug = UnityEngine.Debug;

namespace MyOrg.App
{
    internal static class Logger
    {
        [Conditional("UNITY_EDITOR")]
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static void Log(object message, Object context = null)
            => Debug.Log(message, context);

        [Conditional("UNITY_EDITOR")]
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static void LogWarning(object message, Object context = null)
            => Debug.LogWarning(message, context);

        [Conditional("UNITY_EDITOR")]
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static void LogError(object message, Object context = null)
            => Debug.LogError(message, context);
    }
}

--

ロガーはロガーで色々と奥が深いみたいですが、本気のロガーに後から差し替えも可能でしょう。

以上です。お疲れ様でした。

6
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
1