- Home /
Table with rows and columns editable
Hi.. I am trying to create 3*3 table where the data in the table is editable in editor window. Is there a specific way to create such. please help me
@$$anonymous$$aspianR , Am trying to accomplish this in Editor window so when I see in game window the table should have data in rows and columns
Answer by KaspianR · Oct 11, 2019 at 12:32 PM
Okay @Marynancy! Sorry for the delay, some things popped up. I almost understood that you meant the editor window so I removed the initial comment but thanks for the verification. I wrote a small script for you that I hope will work:
string[,] CreateTable(string[,] table)
{
for (int y = 0; y < table.GetLength(0); y++)
{
float width = Screen.width - 16;
GUILayout.BeginHorizontal();
for (int x = 0; x < table.GetLength(1); x++)
{
table[y, x] = GUILayout.TextField(table[y, x], GUILayout.Width(width / table.GetLength(1)));
}
GUILayout.EndHorizontal();
}
return table;
}
And then just call it using:
[YourTable] = CreateTable([YourTable]);
[YourTable] should be a two dimensional string-array and then this function should be flexible and size correctly! Hope it works our for you and sorry for the delay!
It's kinda pointless to return the same array you pass in as you modify the inco$$anonymous$$g array anyways. Also using multidimensional arrays might not be that useful in the context of Unity since Untiy can not serialize multi dim arrays and they also tend to be slower than a manually flattended array or an array of a class that contains a nested array. Those two solutions could be serialized by unity (as long as the nested class is marked as serializable and the actual contained type is serializable by Unity).
@Bunny83 I get your point although I don’t really agree. The serializable would totally be a problem for most uses but in this case I wrote a way to serialize it so in this specific case I think it should be fine. $$anonymous$$odifying the code to make work with an object wouldn’t be all that hard though and if $$anonymous$$arynancy would prefer that he’s welcome to do so. About the sending back the same variable but modified I see your point and I agree. For most uses a reference or similar would be better but in this case I didn’t since it looks more like the other EditorGUI methods this way. A pretty dull reason I know but I just like it more this way! :D