Automatically adjusting GUIText Window Size

Hi all!

I’m creating a little script to attach to individual gameobjects, so when the player clicks on the object, it’ll shoot up a text window in the middle of the screen to describe what the object is (think point and click adventure).

So, I have the text popping up and displaying whenever I click on an object, but I’m wanting the window to automatically adjust size based off of how much text is being displayed and want it to always be in the center of the screen.

Here’s the script that I pulled and adapted for my own uses:

private var wasClicked: boolean = false;
var displayedText = "DEFAULT TEXT.  UPDATE THIS ON THE GAMEOBJECT";

function OnMouseUp() {
    wasClicked = true; // turns on button.
}
function OnGUI() {
    if (wasClicked) {
        
        if (GUI.Button(new Rect (200, 100, 500, 100), displayedText)) {
            // do other stuff when button clicked.
            wasClicked = false; // turns off button.
        }
    }
}

Another thing I’d like to happen is when I click the mouse again, it would close the text window.

Thanks so much!

I just realized that I made that a button and not a box or text area…so I’m going to have to reconfigure this to bring up a text box in the middle of the screen and then I want a mousedown anywhere on the screen to make it go away. Hrm.

Okay - I did a changeover to this thing:

private var showBox: boolean = false;
var LookStateDisplayedText = "DEFAULT LOOK TEXT.  UPDATE THIS ON THE GAMEOBJECT";

function OnMouseDown () {
    if (Input.GetMouseButtonDown) 
    showBox = showBox == false ? true : false;
 
}

function OnGUI () {
    if (showBox) GUILayout.Box(LookStateDisplayedText);

}

Now the only things I need to figure out are how to put this window on the center of the screen (I couldn’t get the GUIStyles stuff to work as expected) and to be able to press the mouse button ANYWHERE on the screen to get the window to go away, instead of having to click on the gameobject again.

Any thoughts?