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();
                }
            }
        }
    }
}

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.