Is there any way to view 2d arrays in the inspector?

I am scripting in javascript and don’t seem to be able to view the 2d arrays that I create in the inspector. Is there a way around to fix this?

1 Like

Look up multidimensional arrays on this site. It's a known problem, and there exist many workable solutions.

7 Answers

7

to see 2d arrays in the inspector try this:

 var array2d : MyArray[];

class MyArray{
 	var myArray : float[];
 }

Thank you, Wilaxxx. This will come in very handy.

Keep in mind, if you make a tree structure using this, Unity will only serialize 7 levels deep before just giving up. So this works for simple 2 depth matrices or something, but deep trees won't work.

Thank you, Wilaxxx. In JS all work great but when i try it in C# it won't show in inspector :( Any suggestion how to fix this, or it impossible?

Hehe just found it by my self. Here is c# example all You need to do is add [System.Serializable] before class declaration. [System.Serializable] public class MultiDimensionalInt { public int[] intArray; } Then you can build an array of it in your main class like this: public MultiDimensionalInt[] myIntArray;

And the source of Atabeks answer: http://answers.unity3d.com/questions/53038/visializing-a-multidimensional-array.html

Hey,

I stumbled in this same issue a month ago and after solving it, I have made a solution to this, now. I have made a grid view for a 2D array.

You can find it here: Unity3D - 2D Array In Inspector - YouTube

Its in c# though.

Thank you! A lot of time looking for something like that!

I wish you put the answer here as well. It will be more reliable for future as links may become broken over time. By the way, thanks for the solution.

Thanks for the video. I actually gave it a shot and found this thing, dunno if you already saw it, but I was able to make my 3D array visualization by following your video and using the SerializedProperty.getArraySize property to be able to use whatever size the original array has. i.e. > SerializedProperty data = property.FindPropertyRelative("rows"); for (int x = 0; x < data.arraySize; x++) { // do stuff for each member in the array EditorGUI.PropertyField(newPosition, data.GetArrayElementAtIndex(x), GUIContent.none); } Hope it helps somebody.

Simple example, allows editing in Inspector non-debug mode:

[System.Serializable]
public struct NPCImgs
{
    [SerializeField] public Sprite[] Img;
}

public NPCImgs[] CharSprites;

No, multidimansional arrays are not serialized by Unity and therefore can’t viewed in the inspector.

edit
The only way is to use an array of a serializable class / struct which contains another array. See this question for more details. I know there are a lot better examples around, but can’t find them at the moment.

Or you always can use a custom inspector. I find it annoying most of the time but if it helps ...

@Berenger: Sure, but it won't serialize the array, so what's the point of showing it in the editor ;) Just to view the data?

exactly. So you can see if it works without using print messages, which I fing extremely cumbersome when using containers with more than 1 dimension. Also, you could edit it.

Hey guys,

If you want to use a 2d List of something like tiles for a Tile map, I wrote a fun solution that makes a wrapper around a 1d List and allows you to work with them as if they it were a 2d List with a minor change in notation for accessing elements.

Using my class, you can create a 2d map of tiles like this:

public Map map = new Map();

Add lists of tiles (rows) to the map like this:

map.Add(row);

Then get and set elements on the map like this:

Tile aTile = map[1,0];

map[2,3] = aTile;

Here’s the Map class:

Hello there,
I just came across this question today at work and solved it,

I made a GitHub for this: GitHub - Eldoir/Array2DEditor: Use 2-dimensional arrays in Unity's Inspector.

Hope you’ll like it :slight_smile:

If you want a matrix for Coordinates you can do it also like this :


public Vector2[] vectorName;


In inspector you can set the vector size and also can set x and y for each element inside vector;