Monologue

Hey,

I want to make a small box where a text should be in. I already know how to make a box:

GUI.Box(new Rect(x, x, Screen.width, Screen.height),"test");

But how can I add a button in the box, so if I press the button, the box should dissappear.

public bool showBox = true;

void OnGUI() {
    if (showBox) {
        GUI.Box(new Rect(x, x, Screen.width, Screen.height),"test");
        if (GUI.Button(new Rect(x, y, width, height), "Close")) {
            showBox = false;
        }
    }
}

First of all thanks! How can I adjust both x, so the screen should be nearly ‘full’.

You could scale it relative to the Screen dimensions:

public bool showBox = true;

void OnGUI() {
    if (showBox) {
        var marginX = Screen.width / 10; // Fill most of the screen but...
        var marginY = Screen.height / 10; // ...leave a 10% margin on each side.
        GUI.Box(new Rect(marginX, marginY, Screen.width - 2 * marginX, Screen.height - 2 * marginY),"test");

        // The close button is 100x40 and centered at the bottom of the screen:
        if (GUI.Button(new Rect(Screen.width / 2 - 50, Screen.height - marginY - 40, 100, 40), "Close")) {
            showBox = false;
        }
    }
}

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.

Does this work?

GUI.Label(rect, "Line1 \n Line2");

I already tried it.

Try this:

GUI.Label(rect, "Line1" + System.Environment.NewLine + "Line2");

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.

Sorry, I had a typo. The line should be:

if (GUI.Button(new Rect(Screen.width / 2 - 50, Screen.height - marginY - 40, 100, 40), "Close")) {

I also solved the problem before you posted it. Anyway thanks!