Tsuの雑記¯\_(ツ)_/¯

主に製作メモ・備忘録として使用。製作したアプリのリンクもあります。

【Unity】Debug.Log() で複数の object を同時に表示する【C#】

Console 画面に変数を表示できるDebug.Log()メソッドですが,基本的に表示できるのは1つの変数です。

本稿では,複数の変数を並べて1行で表示する方法を御紹介いたします。

(Unity 2018.3.0f2)

docs.unity3d.com

1つの文字列として扱う

docs.microsoft.com

これが,最も簡単な方法だと思います。

using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    void Start()
    {
        var a = 0;
        var b = false;
        Debug.Log(a + ", " + b);
    }
}

複数の変数に+記号で文字列を加えれば,それ全体が1つの文字列として扱われます。

(半角コンマ,である必要はありません。)

C# 6 以降であれば,文字列補間も使えます。

docs.microsoft.com

補足する文字が多い場合などは,+記号を多用するよりも文字列補間を使った方が良いでしょう。

Debug.Log($"{nameof(a)} = {a}, {nameof(b)} = {b}");

クラスや構造体としてまとめる

docs.microsoft.com

docs.microsoft.com

同時に出力したい変数の関係性によっては,有効な方法です。

using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    class Class
    {
        public int A { get; }
        public bool B { get; }

        public Class()
        {
            A = 0;
            B = false;
        }

        public Class(int a, bool b)
        {
            A = a;
            B = b;
        }

        public override string ToString() => $"({A}, {B})";
    }

    struct Struct
    {
        public int A { get; }
        public bool B { get; }

        public Struct(int a, bool b)
        {
            A = a;
            B = b;
        }

        public override string ToString() => $"({A}, {B})";
    }

    void Start()
    {
        Debug.Log(new Class());
        Debug.Log(new Struct());
    }
}

変数A,Bが基本的に組み合わせて使用される場合,こうしてクラスや構造体にまとめると良いでしょう。

分かりやすい例は Unity 独自構造体のVector3などで,x,y,zそれぞれの数値が同時にコンソール画面へ表示されます。

匿名型にまとめる

docs.microsoft.com

型を指定せず,プロパティの塊として扱います。

using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    void Start()
    {
        var a = 0;
        var b = false;
        Debug.Log(new { a, b });
    }
}

クラスや構造体を定義するより,簡単に実装できますね。

Tuple にまとめる

docs.microsoft.com

前述の匿名型と,使いかたは似ています。

using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    void Start()
    {
        var a = 0;
        var b = false;
        Debug.Log(System.Tuple.Create(a, b));
    }
}

初期化にはnewキーワードではなく,Createメソッドを使う点に注意。

ValueTuple にまとめる

docs.microsoft.com

C# 7 以降の機能ですが,匿名型やTupleよりもお勧めしたい方法です。

using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    void Start()
    {
        var a = 0;
        var b = false;
        Debug.Log((a, b));
    }
}

簡単ですよね,これだけで複数の変数を並べて表示できます。

まとめ

using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    class Class
    {
        public int A { get; }
        public bool B { get; }

        public Class()
        {
            A = 0;
            B = false;
        }

        public Class(int a, bool b)
        {
            A = a;
            B = b;
        }

        public override string ToString() => $"({A}, {B})";
    }

    struct Struct
    {
        public int A { get; }
        public bool B { get; }

        public Struct(int a, bool b)
        {
            A = a;
            B = b;
        }

        public override string ToString() => $"({A}, {B})";
    }

    void Start()
    {
        var a = 0;
        var b = false;
        // 文字列
        Debug.Log($"{nameof(a)} = {a}, {nameof(b)} = {b}");
        // クラス
        Debug.Log(new Class());
        // 構造体
        Debug.Log(new Struct());
        // 匿名型
        Debug.Log(new { a, b });
        // Tuple
        Debug.Log(System.Tuple.Create(a, b));
        // ValueTuple
        Debug.Log((a, b));
    }
}

UnityEditor の Console 画面に出力した結果の画像
Debug.Log() それぞれの結果

以上,Debug.Log() で複数の object をまとめて表示する方法でした。