- Home /
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?
Look up multidimensional arrays on this site. It's a known problem, and there exist many workable solutions.
Answer by Wilaxxx · Mar 25, 2014 at 03:33 PM
to see 2d arrays in the inspector try this:
var array2d : MyArray[];
class MyArray{
var myArray : float[];
}
$$anonymous$$eep in $$anonymous$$d, 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 $$anonymous$$ultiDimensionalInt
{
public int[] intArray;
}
Then you can build an array of it in your main class like this:
public $$anonymous$$ultiDimensionalInt[] myIntArray;
Something important missing from this example is how to access the multiple dimensions. Here is an example of how to do so
myIntArray[row].intArray[column]
Thanks a lot @kalibcrone . I made class last day and I was struggling a lot to access members. I was just going to reject this approach and then I cam across your answer. You said right, this missing part is really important and I really thank you for this.
Regards,
And the source of Atabeks answer: http://answers.unity3d.com/questions/53038/visializing-a-multidimensional-array.html
Answer by sumeetkhobare · Jul 13, 2015 at 10:37 AM
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: https://youtu.be/uoHc-Lz9Lsc
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.
Answer by Bunny83 · Mar 25, 2012 at 03:37 AM
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.
For this purpose i've developed my "own" ingame inspector which is part of my debug class. It can list all objects variables (public and private). $$anonymous$$y class also supports DrawLine and even DrawQuad at runtime. I also integrated a console window for Debug.Log messages. At the moment it's not in a state i can publish it. $$anonymous$$aybe if i can find the time to polish it i'll release it ;)
It's build very modular so you can add very easy your own modules (actually almost like the UnityEditor, but at runtime ;)). I've made this mainly for debugging support on mobile platforms. The GUI is of course optimised for touch input ;)
Well, i think i shouldn't talk too much about it, maybe it will never be done...
Answer by Goshand · Sep 19, 2015 at 08:54 PM
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:
Answer by Sarchy · Nov 28, 2016 at 02:33 PM
Simple example, allows editing in Inspector non-debug mode:
[System.Serializable]
public struct NPCImgs
{
[SerializeField] public Sprite[] Img;
}
public NPCImgs[] CharSprites;