Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

how can i pass parameters to a window creating function in c#

Discussion in 'Immediate Mode GUI (IMGUI)' started by sparhawk, Sep 4, 2013.

  1. sparhawk

    sparhawk

    Joined:
    Mar 17, 2013
    Posts:
    9
    im creating a drop down menu, where when a button is pressed, a window pops up containing buttons...each button corresponds to an item in a list. the problem is the method that calls the window function is static, and cant contain any variables for gui style etc. how can I pass those type of parameters to the window creating method.
     
  2. Patico

    Patico

    Joined:
    May 21, 2013
    Posts:
    886
    You can pass GUIStyle as 5-th parameter in GUI.Window() function.
     
  3. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    You can pass them with a static variable, that is then visible from outside of the window function. :)
     
  4. sparhawk

    sparhawk

    Joined:
    Mar 17, 2013
    Posts:
    9
    I tried that, but I want to be able to reuse the dropdown menu several times with different content, the way a normal gui control like a button is used... static variables only hold one value across all of the components so I end up with several lists all with the same thing.
     
  5. sparhawk

    sparhawk

    Joined:
    Mar 17, 2013
    Posts:
    9
    yeah but how would I pass a gui style for the buttons that are on the window
     
  6. Patico

    Patico

    Joined:
    May 21, 2013
    Posts:
    886
    Each window function get a WindowId parameter (it's first paramater), you can use like a key to take a style from presaved collection (like Dictionary<int, GUIStyle> as in code bellow).

    Code (csharp):
    1.  
    2. Dictionary<int, GUIStyle> styles = new Dictionary<int, GUIStyle>();
    3.  
    4. void OnGUI()
    5. {
    6.    styles.Add(1, new GUIStyle());
    7.    GUI.Window(1, DoMyWindow, "Window 1");
    8. }
    9.  
    10. void DoMyWindow(int windowId)
    11. {
    12.    GUI.Button(new Rect(0,0, 100, 30), "button", styles[windowId]);
    13. }
    14.  
     
  7. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    Or do not use GUI.Window at all :)

     
  8. Aledend

    Aledend

    Joined:
    Apr 26, 2018
    Posts:
    1
    A lambda expression should work just fine here.

    Code (CSharp):
    1. void OnGUI()
    2. {
    3.     GUIStyle style = new GUIStyle();
    4.     Rect rect = new Rect();
    5.     GUI.Window(1, rect, (int id) => { WindowContent(style); }, "Window");
    6. }
    7.  
    8. void WindowContent(GUIStyle style)
    9. {
    10.    //...
    11. }
     
  9. King_of_L1mbs

    King_of_L1mbs

    Joined:
    Jun 15, 2019
    Posts:
    22
    Thabnks for necroing this - it worked :D