Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ochreous · Oct 24, 2013 at 01:32 AM · c#guiwindow

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();
                 }
             }
         }
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ochreous · Oct 24, 2013 at 04:38 AM 0
Share

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?

avatar image Bunny83 · Oct 25, 2013 at 03:29 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# GUI Destroy or Disable? 1 Answer

FPS keep a loadout 0 Answers

How to increase space between two things with GUILayout.BeginHorizontal C# 1 Answer

How to dynamically change the text in Unity(Augmented Reality + NYARtoolkit(C#)) ? 0 Answers

C# How to Drag and Scale with Mouse Window 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges