tried to make kind of a generic 2D array visualisation editor window.
drag the gameObject that has the script with the 2D array in the “Script:” slot and write the Type of the 2D array in the “Type:” slot. if you want to see the content of the 2D array during runtime, press the refresh button.
it should also expose all the Fields of the Type with toggle’s to visualise the value of the field or not.
Have fun and Please let me know if it works/doesn’t work for you ![]()
(put this script in the Editor folder)
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Reflection;
using System.Linq;
public class MultiDimensionalArrayWindow : EditorWindow
{
string arrayType;
GameObject obj;
int size = 50;
Font font;
bool index = true;
bool val = true;
Vector2 scroll;
GUIStyle boxStyle;
bool isDrawn = true;
Type type;
Array array;
Dictionary<FieldInfo, bool> dic = new Dictionary<FieldInfo, bool>();
[MenuItem ("Window/MultiDimensionalArrayWindow")]
public static void Init ()
{
MultiDimensionalArrayWindow window = EditorWindow.GetWindow<MultiDimensionalArrayWindow>("Multi-Dimesional Array", typeof(SceneView));
window.Show();
}
void OnGUI()
{
EditorGUILayout.BeginHorizontal(GUILayout.MinWidth(position.width));
EditorGUILayout.BeginHorizontal("Box");
EditorGUILayout.LabelField("Script: ", GUILayout.MaxWidth(100));
obj = (GameObject)EditorGUILayout.ObjectField(obj, typeof(GameObject), true, GUILayout.MaxWidth(100));
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal("Box");
EditorGUILayout.LabelField("Type: ", GUILayout.MaxWidth(100));
arrayType = EditorGUILayout.TextField(arrayType, GUILayout.MaxWidth(100));
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal("Box");
EditorGUILayout.LabelField("Box Size", GUILayout.MaxWidth(100));
size = EditorGUILayout.IntField(size, GUILayout.MaxWidth(30));
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal("Box");
if(GUILayout.Button("Refresh"))
{
array = null;
type = null;
isDrawn = false;
}
EditorGUILayout.EndHorizontal();
if(arrayType == "string" || arrayType == "String")
{
arrayType = "String";
EditorGUILayout.BeginHorizontal("Box");
EditorGUILayout.LabelField("Font: ", GUILayout.MaxWidth(100));
font = (Font)EditorGUILayout.ObjectField(font, typeof(Font), true, GUILayout.MaxWidth(100));
EditorGUILayout.EndHorizontal();
}
EditorGUILayout.EndHorizontal();
if(obj != null && !string.IsNullOrEmpty(arrayType) && !isDrawn && array == null)
{
object o = (object)obj.GetComponent(obj.name);
type = o.GetType();
foreach (FieldInfo field in type.GetFields())
{
if(field.FieldType.IsArray && field.FieldType.GetElementType().Name == arrayType)
{
array = (Array)field.GetValue(o);
}
}
dic.Clear();
}
if(array == null)
return;
if(array.Rank==2)
{
type = array.GetType();
foreach (FieldInfo field in type.GetElementType().GetFields())
{
if(!dic.ContainsKey(field))
dic.Add(field, false);
}
MethodInfo method = typeof(MultiDimensionalArrayWindow).GetMethod("DrawArray");
MethodInfo genericMethod = method.MakeGenericMethod(type);
genericMethod.Invoke(this, new object[]{array});
}
}
public void DrawArray<T>(Array array)
{
EditorGUILayout.BeginHorizontal("Box", GUILayout.Width(position.x));
index = EditorGUILayout.ToggleLeft("Show Index", index, GUILayout.MaxWidth(100));
if(type.GetElementType().IsValueType || type.GetElementType().Name == "String")
val = EditorGUILayout.ToggleLeft("Show value", val, GUILayout.MaxWidth(100));
for(int i=0; i< dic.Count; i++)
{
dic[dic.Keys.ElementAt(i)] = EditorGUILayout.ToggleLeft(dic.Keys.ElementAt(i).Name, dic.Values.ElementAt(i), GUILayout.MaxWidth(100));
}
EditorGUILayout.EndHorizontal();
boxStyle = new GUIStyle(GUI.skin.box);
boxStyle.normal.textColor = Color.white;
if(font != null)
boxStyle.font = font;
scroll = EditorGUILayout.BeginScrollView(scroll);
EditorGUILayout.BeginHorizontal();
for(int i=0; i<array.GetLength(0); i++)
{
EditorGUILayout.BeginVertical();
for(int j=array.GetLength(1)-1; j>=0; j--)
{
string s ="";
if(index)
s += i+":"+j+"\n";
if(val)
s += array.GetValue(i,j);
for(int k=0; k< dic.Count; k++)
{
if(dic[dic.Keys.ElementAt(k)])
{
FieldInfo f = dic.Keys.ElementAt(k);
s += f.GetValue(array.GetValue(i,j));
}
}
GUILayout.Box(s, boxStyle, GUILayout.MinWidth(size),GUILayout.MinHeight(size));
}
EditorGUILayout.EndVertical();
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.EndScrollView();
isDrawn = true;
}
}
Edit: made minor changes because it threw an exception when switching between scenes.