Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Monologue

Discussion in 'Immediate Mode GUI (IMGUI)' started by pehlivan2000, Jul 3, 2015.

  1. pehlivan2000

    pehlivan2000

    Joined:
    May 23, 2015
    Posts:
    20
    Hey,

    I want to make a small box where a text should be in. I already know how to make a box:
    Code (Javascript):
    1.  
    2. GUI.Box(new Rect(x, x, Screen.width, Screen.height),"test");
    3.  
    But how can I add a button in the box, so if I press the button, the box should dissappear.
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,533
    Code (csharp):
    1. public bool showBox = true;
    2.  
    3. void OnGUI() {
    4.     if (showBox) {
    5.         GUI.Box(new Rect(x, x, Screen.width, Screen.height),"test");
    6.         if (GUI.Button(new Rect(x, y, width, height), "Close")) {
    7.             showBox = false;
    8.         }
    9.     }
    10. }
     
  3. pehlivan2000

    pehlivan2000

    Joined:
    May 23, 2015
    Posts:
    20
    First of all thanks! How can I adjust both x, so the screen should be nearly 'full'.
     
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,533
    You could scale it relative to the Screen dimensions:
    Code (csharp):
    1.  
    2. public bool showBox = true;
    3.  
    4. void OnGUI() {
    5.     if (showBox) {
    6.         var marginX = Screen.width / 10; // Fill most of the screen but...
    7.         var marginY = Screen.height / 10; // ...leave a 10% margin on each side.
    8.         GUI.Box(new Rect(marginX, marginY, Screen.width - 2 * marginX, Screen.height - 2 * marginY),"test");
    9.  
    10.         // The close button is 100x40 and centered at the bottom of the screen:
    11.         if (GUI.Button(new Rect(Screen.width / 2 - 50, Screen.height - marginY - 40, 100, 40), "Close")) {
    12.             showBox = false;
    13.         }
    14.     }
    15. }
     
    Last edited: Jul 8, 2015
  5. pehlivan2000

    pehlivan2000

    Joined:
    May 23, 2015
    Posts:
    20
    Thanks, but how can I also 'write' brackets in the text? Just like in HTML, C++ (CLI) etc.: \n

    There's also approaching an error:

    BCE0024: The type 'UnityEngine.Rect' does not have a visible constructor that matches the argument list '(int, int, int)'.

    I tried to fix it, but I couldn't do it.
     
    Last edited: Jul 8, 2015
  6. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,533
    Does this work?
    Code (csharp):
    1. GUI.Label(rect, "Line1 \n Line2");
     
  7. pehlivan2000

    pehlivan2000

    Joined:
    May 23, 2015
    Posts:
    20
    I already tried it.
     
  8. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,533
    Try this:
    Code (csharp):
    1. GUI.Label(rect, "Line1" + System.Environment.NewLine + "Line2");
     
  9. pehlivan2000

    pehlivan2000

    Joined:
    May 23, 2015
    Posts:
    20
    As I mentioned, I've still a problem:

    BCE0024: The type 'UnityEngine.Rect' does not have a visible constructor that matches the argument list '(int, int, int)'.

    The new line is working though.
     
  10. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,533
    Sorry, I had a typo. The line should be:
    Code (csharp):
    1. if (GUI.Button(new Rect(Screen.width / 2 - 50, Screen.height - marginY - 40, 100, 40), "Close")) {
     
  11. pehlivan2000

    pehlivan2000

    Joined:
    May 23, 2015
    Posts:
    20
    I also solved the problem before you posted it. Anyway thanks!