- Home /
C# Rows and Columns GUILayout.Window Issues
I'm trying to put this group of GUILayout.Boxes in a GUILayout.Window . When I run the scene they don't appear anywhere within the GUILayout.Window. But If I put them in a different function like Void OnGUI they appear in a group like they're suppose to. Any idea why this is happening? Sorry if the bottom of my code is messed up. Whenever I try to post it the bottom gets completely messed up.
using UnityEngine;
using System.Collections;
public class RowsAndColumns : MonoBehaviour {
public const int rowSizeX= 8;
public const int columnSizeY= 5;
public int[,] rowColumnArray;
public Vector2 offSet = new Vector2 (100, 100);
public Rect windowRect0;
public int boxWidthHeight= 40;
public int boxSpacing= 2;
void OnGUI () {
windowRect0 = GUILayout.Window(0, windowRect0, DoMyWindow, "My Window");
}
void DoMyWindow(int windowID){
for (int i= 0; i < rowSizeX; i++)
{
for( int j = 0; j < columnSizeY; j ++ )
{
if( rowColumnArray[i,j] != null )
{
GUILayout.BeginArea(new Rect(offSet.x+j*(boxWidthHeight+boxSpacing), offSet.y+i*(boxWidthHeight+boxSpacing), boxWidthHeight, boxWidthHeight));
GUILayout.Box("A Box");
GUILayout.EndArea();
}
}
}
}
}
Answer by Bunny83 · Oct 24, 2013 at 02:54 AM
Well the problem is that your window has a size of 0,0. That's because it has "no content". By no content i mean the Window itself is a layout group which doesn't contain a single GUILayout element. You start seperate Layout Areas, but an Area is an independent layouting area so it doesn't count as element which should be layouted by the window. That's why the size stays 0
You can do it like this:
void DoMyWindow(int windowID)
{
// this could be easily done with a GUISkin:
GUIStyle box = new GUIStyle("box");
box.margin.right = boxSpacing;
box.margin.bottom = boxSpacing;
box.fixedHeight = boxWidthHeight;
box.fixedWidth = boxWidthHeight;
GUILayout.Space(offSet.y);
GUILayout.BeginVertical();
for (int i= 0; i < rowSizeX; i++)
{
GUILayout.BeginHorizontal();
GUILayout.Space(offSet.x);
for( int j = 0; j < columnSizeY; j ++ )
{
if( rowColumnArray[i,j] != null )
{
GUILayout.Box("A Box", box);
}
else
{
GUILayoutUtility.GetRect(boxWidthHeight, boxWidthHeight);
}
}
GUILayout.EndHorizontal();
}
GUILayout.EndVertical();
}
Since you have a more or less fix layout, it might be easier to simply use the normal GUI stuff. In your code you don't make use of the layouting anywhere since each layout-areas only contain one element...
So this might be simpler in your case:
//...
public Rect windowRect0 = new Rect(0,0,500,500);
void OnGUI ()
{
windowRect0 = GUI.Window(0, windowRect0, DoMyWindow, "My Window");
}
void DoMyWindow(int windowID)
{
for (int i = 0; i < rowSizeX; i++)
{
for( int j = 0; j < columnSizeY; j ++ )
{
if( rowColumnArray[i,j] != null )
{
GUI.Box(new Rect(offSet.x+j*(boxWidthHeight+boxSpacing), offSet.y+i*(boxWidthHeight+boxSpacing), boxWidthHeight, boxWidthHeight), "A Box");
}
}
}
}
In this case you have to set the window size manually.
Also make sure you have unique window IDs across your whole project.
I always use an "ID manager" like this:
public static class WinManager
{
private static int m_NextWinID = 0;
public static int GetWinID()
{
return m_NextWinID++;
}
}
Whenever you need a window, declare an ID variable for it an initialize it like this:
int m_ChatWindowID = WinManager.GetWinID();
This ensures that each window has it's own ID.
Why does the GUILayout.Box become so huge if you don't use the GUISkin like you put in your code. Is something else in the GUILayout.Window manipulating the values ins$$anonymous$$d of boxSpacing and boxWidthHeight?
If you use a GUIStyle that has one of the stretchXXX options set, The element will stretch the element as far as the parent element allows. Since the Window is only limited by the screen it would enlarge the Window as much as possible. Again, in your original code you don't actually use any layout feature and your layout seems to be a fix layout, so why do you want to use GUILayout?
ps: You can also use a GUI.Window and start a GUILayout.BeginArea within the Window. In that case the Window can't be resized by the containing elements. That of course means you have to set the size manually.