Gui Window

Hi, I have this script which connects to SmartFox. I want to put the login section inside of a GUIwindow box so I can center it and make it look nice. But I am unable to figure this out with that Line 16 if (smartFox.IsConnected()) { part in the way. However, I need that to still remain.

void OnGUI() {
	        
		// server IP in bottom left corner
		GUI.Label(new Rect(10, Screen.height-25, 200, 24), "Server: " + serverIP);
 
		// quit button in bottom right corner
		if ( Application.platform != RuntimePlatform.WindowsWebPlayer ) {			
			if ( GUI.Button(new Rect(Screen.width-150, Screen.height - 50, 100, 24), "Quit") ) {
				smartFox.Disconnect();
				UnregisterSFSSceneCallbacks();
				Application.Quit();
			}
		}
 		
		// Show login fields if connected and reconnect button if disconnect
		if (smartFox.IsConnected()) { 
			GUI.Label(new Rect(140, 40, 130 ,100), "Username: ");
			username = GUI.TextField(new Rect(25, 60, 375, 30), username, 25);
			if ( GUI.Button(new Rect(25, 160, 175, 50), "Login")  || (Event.current.type == EventType.keyDown  Event.current.character == '\n')) {
				smartFox.Login(zone, username, "");
			}
		} else {
			if ( GUI.Button(new Rect(25, 160, 175, 50), "Reconnect")  || (Event.current.type == EventType.keyDown  Event.current.character == '\n')) {
			Application.LoadLevel("sc_City");
			}
		}
 
		// Draw box for status messages, if one is given
		// Contains some logic to parse message of multiple lines if necessary
		if (statusMessage.Length > 0)
		{
			int boxLength = 61;							// define length of status box
			int messageLength = statusMessage.Length;	// get length of status message
			string originalMessage = statusMessage;		// copy message in to work string
			string formattedMessage = "";				// define output message string
			int i = 0;
			while (i + boxLength < messageLength)		// iterate and add newline until over length
			{
				formattedMessage = formattedMessage + originalMessage.Substring(i,boxLength) + "\n";
				i = i + boxLength;
			}
			// add last piece of original message
			formattedMessage = formattedMessage + originalMessage.Substring(i,  boxLength - (i + boxLength - messageLength));
			// draw status box with message
			GUI.Box (new Rect (Screen.width - 420,10,400,48), formattedMessage);
		}
 
	}

Is there a question? What is it that you are unable to figure out?

EDIT:

So something like:

GUI.Window (0, windowRect, DoMyWindow, "My Window");

// Make the contents of the window
function DoMyWindow (windowID : int) {
    GUI.Label(new Rect(140, 40, 130 ,100), "Username: ");
    username = GUI.TextField(new Rect(25, 60, 375, 30), username, 25);

    if ( GUI.Button(new Rect(25, 160, 175, 50), "Login")  || (Event.current.type == EventType.keyDown  Event.current.character == '\n')) {
        smartFox.Login(zone, username, "");
	}

is what you wanted?

Thanks, but no that isn’t it.

I am aware that is the line of code that goes there.

In your example, on line 4, you took out the "if (smartFox.IsConnected()) { ". Like I said, I still need that line of code to remain. I could not figure out how to insert the Gui.Window and keep that code too.

void OnGUI
{
    if (smartFox.IsConnected()) {
        GUI.Window (0, Rect (0, 0, 200, 200), DoMyWindow, "My Window");
    }
}

void DoMyWindow (int windowID) {
    GUI.Label(new Rect(140, 40, 130 ,100), "Username: ");
    username = GUI.TextField(new Rect(25, 60, 375, 30), username, 25);
    if ( GUI.Button(new Rect(25, 160, 175, 50), "Login")  || (Event.current.type == EventType.keyDown  Event.current.character == '\n')) {
        smartFox.Login(zone, username, "");
    }
}

Ah I tried something like that last night except I left the GUI.Window (0, Rect (0, 0, 200, 200), DoMyWindow, “My Window”); part under void OnGUI() {.

Will give this a go after work thank you.

Thanks, but this did not work. Were you suggesting I insert this code to the code I posted?

Figured it out. Just had to move the void LoginWindow(int windowID) { to a different place.

void OnGUI() {
		GUI.Window (0, new Rect(Screen.width / 4, Screen.height / 4, Screen.width / 2, Screen.height / 2 - 70),
			LoginWindow, "Login");
		}
		// server IP in bottom left corner
	void LoginWindow(int windowID) {
		GUI.Label(new Rect(10, Screen.height-25, 200, 24), "Server: " + serverIP);
 
		// quit button in bottom right corner
		if ( Application.platform != RuntimePlatform.WindowsWebPlayer ) {			
			if ( GUI.Button(new Rect(Screen.width-150, Screen.height - 50, 100, 24), "Quit") ) {
				smartFox.Disconnect();
				UnregisterSFSSceneCallbacks();
				Application.Quit();
			}
		}
 		
		// Show login fields if connected and reconnect button if disconnect
		
		if (smartFox.IsConnected()) {
			GUI.Label(new Rect(140, 40, 130 ,100), "Username: ");
			username = GUI.TextField(new Rect(25, 60, 375, 30), username, 25);
			if ( GUI.Button(new Rect(25, 160, 175, 50), "Login")  || (Event.current.type == EventType.keyDown  Event.current.character == '\n')) {
				smartFox.Login(zone, username, "");
			}
			
		} else {
			if ( GUI.Button(new Rect(25, 160, 175, 50), "Reconnect")  || (Event.current.type == EventType.keyDown  Event.current.character == '\n')) {
			Application.LoadLevel("sc_City");
			}
		}

Thank you Pirs01, got me on track.