Inspecting a 2D array

If you search for topics about inspecting data type not serializable by default in Unity, the number one suggestion is to create a custom serialization with ISerializationCallbackReceiver.

My problem with that:

  • But I just want to Debug.Log() my 2D array sometimes!

  • Would it be wise to write a custom serialization just for that?

  • Or is a simple class that perform a custom ToString() on the 2D Array so that it can be logged properly, a much more appropriate solution?

Dear people, how would you do it?

I would use a loop.

for(int i = 0; i < myArray.length - 1; i++)
        {
            Debug.Log(myArray[i]);
            //or
            Debug.Log(myArray[i].ToString());
        }

//sorry for typos, code not tested, It’s free handed

Thx johne5, but I am not asking for a specific solution; there are many, I am sure.

I am asking the best way you can think of to print an int[X,Y] array nicely in Editor console. I think making it serializable is an overkill. I want to hear people’s opinions.

(One would want a single Debug.Log for example, because you don’t want to click through all those logs).

I think just about any solution is going to be overkill in some way or another.
Maybe I’d try making a class that wraps my 2d array, and then just provide a ToString() that does all the heavy lifting?

Here’s my thought. Make an extension method, and return a string formatted however you want it formatted.

It is a little bit overkill maybe, but being convenient to re-use later helps.

 using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;

public static class Extender {
    public static string Output(this int[,] array)
    {
        return "Array size: "+array.Length;
    }
}

public class ArrayExtensionTest : Editor {

    [MenuItem("Test/Array")]
    static void Main()
    {
        int[,] multiDimArray = new int[10,10];
        Debug.Log(multiDimArray.Output());
    }
}

(Sadly, it does not appear possible to have an extension supply a .ToString override, which would make this much more convenient. If it does, I can’t figure out the syntax.)

I ended up writing a simple utility class that takes an int[,] input and output a formatted string. I only need it for debugging anyway, leaving minimal footprint on my other code is my main concern. Thx everyone for the input.