- Home /
Public 2D array, accessed through the editor ??
I am trying to declare a public multidimensional array of GameObjects(Using JavaScript not c#) that I can access through the editor. However it would appear that only single dimension arrays are usable through the property inspector.
can anyone please help me out with this i know it is easily possible with c# but i want to know how it can be achieved using javascript.
Thanks in Advance
anyone looking for the c# code for the same problem http://forum.unity3d.com/threads/54707-2D-Arrays-in-C
You can't use multidimensional arrays in the inspector by default. I could show you how to do something which can be used like a multidimensional array in code, but which can be accessed properly in the inspector, but I only know how to do that in C#, and you've forbidden me from doing that. Sorry!
well i have kept that as a last option if i dont get any solution,because my entire project is in java scripts and i dont want to mix it with c# scripts...i know it sounds a bit silly but i always try to complete the entire project using just one language, except for the plugins i always use c# for them....
@syclamoth : thanks for your help i know it is very easily possible with c#,but i am still trying to do it with javascript....if i don't get any solution then i will have to go by your way(c# rules)....
Answer by Cherno · May 20, 2014 at 02:43 PM
What I have done is declare a custom class that has an array as a variable. Declare a new array of this class and you have your multi-dimensional array that can be viewed in the editor without any fuzz. Here's an example in UnityScript, but it should be easily convertible to C#.
class Array2D {
var subArray : GameObject[] = new GameObject [12];
}
var my2DArray: Array2D [] = new Array2D [12];
function PrintArrayContent() {
if(my2DArray[0].subArray[0] != null) {
Print(my2DArray[0].subArray[0].name);
}
}
Answer by Minchuilla · Aug 02, 2014 at 03:36 PM
You could also write your own Custom Editor/Inspector code or use this code as a reference:
In know its in c# but just use this its not that hard to convert and just look at the docs for a custom editor.
using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor(typeof(BackgroundMaster))]
public class BackgroundEditor : Editor {
bool isShowing;
bool[] b;
bool a;
void Start()
{
BackgroundMaster myBGMaster = (BackgroundMaster)target;
b = new bool[2/*myBGMaster.backgroundItems.GetLength(0)*/];
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
BackgroundMaster myBGMaster = (BackgroundMaster)target;
isShowing = EditorGUILayout.Foldout(isShowing, "Background Items");
if(isShowing)
{
//Debug.Log(b.Length + (b[0]?"T":"F") + (b[1]?"T":"F"));
if(b != null && b.Length > 0)
{
for(int i = 0; i < b.Length; i++)
{
string s = "";
if(i == 0)
s = " Foreground";
else if(i == 1)
s = " Midground";
else
s = " Background";
b[i] = EditorGUILayout.Foldout(b[i], s);
if(b[i])
{
for(int j = 0; j < myBGMaster.backgroundItems.GetLength(1); j++)
{
if(myBGMaster.backgroundItems[i, j])
EditorGUILayout.LabelField(" ", myBGMaster.backgroundItems[i, j].name);
else
EditorGUILayout.LabelField(" N/A");
}
}
}
}
else
{
EditorGUILayout.LabelField(" N/A");
EditorGUILayout.LabelField((" [" + myBGMaster.backgroundItems.GetLength(0)) + ", " + myBGMaster.backgroundItems.GetLength(1) + "]");
}
}
}
}
Or you could have a look at this Unity tutorial
Hope this helps later viewers,
Minchuilla
Answer by sumeetkhobare · Jul 13, 2015 at 10:39 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.