はじめに
前回、Debug.Logを抑制する方法を紹介しましたが、もっと簡単にできる方法があったので紹介します。
環境
- Unity 2021.2.1f1
抑制の仕方
やり方はDebug.Logをラップする関数を作り、ConditionalのAttributeをつけるだけです。

ConditionalAttribute クラス (System.Diagnostics)
指定した条件付きコンパイル シンボルが定義されていない場合、メソッド呼び出しまたは属性を無視するようコンパイラに指示します。
サンプルコード
下記のサンプルはUnity Editorのときにのみログが表示されます。
AppDebug.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class AppDebug
{
[System.Diagnostics.Conditional("UNITY_EDITOR")]
public static void Log(object message)
{
Debug.Log(message);
}
[System.Diagnostics.Conditional("UNITY_EDITOR")]
public static void Log(object message, Object context)
{
Debug.Log(message, context);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SampleSceneController : MonoBehaviour
{
[SerializeField] private Button button;
// Start is called before the first frame update
void Start()
{
button?.onClick.AddListener(OnClickedButton);
}
private void OnClickedButton()
{
AppDebug.Log("ボタンが押されたよ");
}
}
実行結果

試しにUNITY_EDITORを他のものに書き換えると。。。

Debug.Logが表示されなくなります。
さいごに
前回のよりも、こっちのほうが簡単かもです。
コメント