Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

the GUI.Window's WindowFunction in C# _help!_

Discussion in 'Immediate Mode GUI (IMGUI)' started by Randy-Edmonds, Nov 7, 2008.

  1. Randy-Edmonds

    Randy-Edmonds

    Joined:
    Oct 10, 2005
    Posts:
    1,122
    How do I use the GUI.Window in C#? Every example I can find is done in javascript. I'm having problems getting the WindowFunction parameter to work.

    Should I be creating a Delegate for the WindowFunction?

    thanks.
     
  2. Randy-Edmonds

    Randy-Edmonds

    Joined:
    Oct 10, 2005
    Posts:
    1,122
    I'm doing something like this (but the Window doesn't appear)....
    Code (csharp):
    1.  
    2. private GUI.WindowFunction winFunc;
    3. private Rect winRect;
    4.  
    5. void Start()
    6. {
    7.    winRect = new Rect(10,10,100,100);
    8.    winFunc = new GUI.WindowFunction(this.ShowWindow,0);
    9. }
    10.  
    11. void OnGUI()
    12. {
    13.    winRect = GUI.Window(0,winRect,winFunc,"Window Name");
    14. }
    15.  
    16. void ShowWindow(int windowId)
    17. {
    18.    Debug.Log("testing. This never displays!");
    19. }
     
  3. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    Does GUI.WindowFunction even exist? I can't see it in the docs.

    Anyway, here's how I'd translate the Javascript sample code for GUI.Window:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GUIWindowTest : MonoBehaviour
    5. {
    6.     Rect windowRect = new Rect(20, 20, 120, 50);
    7.  
    8.     void OnGUI()
    9.     {
    10.         // Register the window. Notice the 3rd parameter
    11.         windowRect = GUI.Window(0, windowRect, DoMyWindow, "My Window");
    12.     }
    13.  
    14.     // Make the contents of the window
    15.     void DoMyWindow(int windowID)
    16.     {
    17.         if(GUI.Button(new Rect(10,20,100,20), "Hello World"))
    18.             Debug.Log("Got a click");
    19.     }
    20. }
    21.  
     
  4. Randy-Edmonds

    Randy-Edmonds

    Joined:
    Oct 10, 2005
    Posts:
    1,122
    Neil,

    Your example is the first way I tried it, but it didn't work. So then I decided that the 3rd parameter must be a c# function pointer (aka Delegate). Yes the GUI.WindowFunction does exist.
    However, so far, neither way works. The Debug text isn't even displaying, so the "DoMyWindow" function isn't being called.




    :?: :?
     
  5. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    Funny, that code works for me.

    EDIT: I just tried it like this, and it also works:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GUIWindowTest : MonoBehaviour
    5. {
    6.     Rect windowRect = new Rect(20, 20, 120, 50);
    7.     GUI.WindowFunction windowFunction;
    8.  
    9.     void Start()
    10.     {
    11.         windowFunction = DoMyWindow;
    12.     }
    13.  
    14.     void OnGUI()
    15.     {
    16.         windowRect = GUI.Window(0, windowRect, windowFunction, "My Window");
    17.     }
    18.  
    19.     void DoMyWindow(int windowID)
    20.     {
    21.         if(GUI.Button(new Rect(10,20,100,20), "Hello World"))
    22.             Debug.Log("Got a click");
    23.  
    24.         GUI.DragWindow();
    25.     }
    26. }
    I imagine GUI.WindowFunction is just delegate void Function(int), which maybe explains why it isn't documented in the list of classes.

    I think you must have some other problem that's preventing this from working.
     
  6. Randy-Edmonds

    Randy-Edmonds

    Joined:
    Oct 10, 2005
    Posts:
    1,122
    Your c# example, is working?


    EDIT: Nevermind. I found the problem.
     
  7. Randy-Edmonds

    Randy-Edmonds

    Joined:
    Oct 10, 2005
    Posts:
    1,122
    I just discovered that if I remove "useGUILayOut = false", the Window is displayed! (I didn't show this in my code example, but it was in my real code.)

    Code (csharp):
    1.  
    2. void Awake()
    3. {
    4.    useGUILayout = false;  // This keeps the GUI Window from displaying!!!
    5. }
    FYI... I'm doing iPhone development, the useGUILayout=false line is recommended for performance reasons.
     
  8. DR9885

    DR9885

    Joined:
    Mar 13, 2010
    Posts:
    4
    actually the safer line of code would be "window.Show", the reason I suggest this is because if unity makes any updates Show may have more functionality than a simple variable...

    for those of you just tuning in here is a much cleaner example of code in C#

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4. public class Window : EditorWindow {
    5.     string myString = "Hello World";
    6.     bool groupEnabled;
    7.     bool myBool = true;
    8.     float myFloat = 1.23f;
    9.  
    10.     Rect windowRect = new Rect(0, 0, 100, 100);
    11.     int index = 0;
    12.  
    13.     // Add menu named "My Window" to the Window menu
    14.     [MenuItem ("Window/My Window")]
    15.     static void Init () {
    16.         // Get existing open window or if none, make a new one:
    17.         Window window = (Window)EditorWindow.GetWindowWithRect(typeof(Window), new Rect(0,0,200,200), true, "Window");
    18.         window.Show ();
    19.     }
    20.    
    21.     void OnGUI () {
    22.         GUILayout.Label ("Base Settings", EditorStyles.boldLabel);
    23.             myString = EditorGUILayout.TextField ("Text Field", myString);
    24.        
    25.         groupEnabled = EditorGUILayout.BeginToggleGroup ("Optional Settings", groupEnabled);
    26.             myBool = EditorGUILayout.Toggle ("Toggle", myBool);
    27.             myFloat = EditorGUILayout.Slider ("Slider", myFloat, -3, 3);
    28.  
    29.  
    30.  
    31.         EditorGUILayout.EndToggleGroup ();
    32.     }
    33. }
     
  9. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    that does not exist on normal gui, only on editor objects
     
  10. ravinder

    ravinder

    Joined:
    Aug 25, 2010
    Posts:
    150
    Hey thanks man for the code.
    I have been trying to do this for hours.