Search Unity

I want to get window width and height, but how?

Discussion in 'Immediate Mode GUI (IMGUI)' started by Ravel, Aug 11, 2012.

  1. Ravel

    Ravel

    Joined:
    Nov 21, 2010
    Posts:
    605
    I write in C# and i cant seem to find how to get thecurrent window width and height. I want to resize the window so that the content stays in place.
     
  2. ModStoryGames

    ModStoryGames

    Joined:
    Apr 27, 2012
    Posts:
    179
    For in-game Windows:

    Code (csharp):
    1.  
    2. Rect windowPosition = new Rect(left:0, top:0, width:128, height:256);
    3.  
    4. void OnGUI()
    5. {
    6.     windowPosition = GUI.Window(0, windowPosition, WindowMethod, "My Window");
    7. }
    8.  
    9. void WindowMethod(int id)
    10. {
    11.    GUILayout.BeginHorizontal();
    12.    if (GUILayout.Button("Change"))
    13.    {
    14.       windowPosition.width += 100;
    15.       windowPosition.height += 100;
    16.    }
    17.    GUILayout.FlexibleSpace();
    18.    GUILayout.Label("This label will always be against the right edge.");
    19.    GUILayout.EndHorizontal();
    20.    GUILayout.FlexibleSpace();
    21.    GUILayout.Label("This label will always be against the bottom edge.");
    22. }
    23.  

    For EditorWindows:

    Code (csharp):
    1.  
    2. var window = EditorWindow.GetWindow<MyWindow>();
    3. window.position = new Rect(x, y, width, height);
    4. window.minSize = new Vector2(width, height);
    5. window.maxSize = new Vector2(width, height);
    6.  
    Hope that helps.
     
  3. Ravel

    Ravel

    Joined:
    Nov 21, 2010
    Posts:
    605
    Thanks ths info is usefull indeed, but actuallyi i wanted to get this code to work:

    Code (csharp):
    1.  
    2.  
    3.             GUI.Window(0,new Rect(0,0,optionsX,optionsY),OptionsWindow,"Options");
    4. ...
    5.     void OptionsWindow(int windowID)
    6.     {
    7.         Rect position = new Rect(10,30,[U][COLOR="red"]windowWidth[/COLOR][/U],[COLOR="red"][U]windowHeight[/U][/COLOR]);
    8.         Rect pageSize = new Rect(0,0,1024,768);// Actual Page Size 
    9.         scrollPosition = GUI.BeginScrollView(position,scrollPosition,pageSize,false,true);
    10.        
    11.         // Scroll Page content
    12.         toggle = GUI.Toggle(new Rect(10,600,100,25),toggle,"bottom toggle");       
    13.        
    14.         GUI.EndScrollView();
    15.                
    16.     }
    17.  
     
  4. ModStoryGames

    ModStoryGames

    Joined:
    Apr 27, 2012
    Posts:
    179
    If you use GUILayout.Window, it will size itself to fit your Scroll View and all that. Just use GUILayout controls instead of GUI controls. If you want to stick with GUI controls, you need to modify optionsX and optionsY inside the OptionsWindow method to change the window's size.
     
  5. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,073
    I love to resurrect this thread.
    Especially since it was created the same year I have joined the community.
    And after all this years IMGUI is still kicking my butt.
    I just want my GUILayout.BeginScrollView to fit inside my GUILayout.window.
    And additionaly even when I have specified alwaysShowVertical = false and alawaysShowHorizontal = false I have this scrollbars visible.

    This is my code after many different attempts:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4.  
    5. public class DeveloperMenuComponent : MonoBehaviour {
    6.  
    7.  
    8.     Rect winRect = new Rect(0, 0, 500, Screen.height);
    9.     private void OnGUI() {
    10.             winRect = GUILayout.Window(0, winRect, WinProc, "Developer Menu");
    11.     }
    12.  
    13.  
    14.     Vector2 scrollPosition;
    15.     void WinProc(int id) {
    16.         scrollPosition = GUILayout.BeginScrollView(scrollPosition, false, false);
    17.  
    18.         GUILayout.Label("Text");
    19.         GUILayout.Button("Button");
    20.  
    21.         GUILayout.EndScrollView();
    22.  
    23.     }
    24.  
    25. }
    26.  
    I rage quitted unity3d today !!!