BOX1 - gui Game.cs script

From Unify Community Wiki
Jump to: navigation, search
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Text;
using SmartFoxClientAPI;
using SmartFoxClientAPI.Data;
 
public class gui_Game : MonoBehaviour {
 
	private static SmartFoxClient client;
	private string statusMessage = "";
	private int myId;
 
	void Start () {
		SmartFoxClient client = NetworkController.GetClient();
		myId = client.myUserId;
		string myName = client.myUserName;
		statusMessage = "Successful login for player " + myName;
		Debug.Log("Player login successful");
 
		SFSEvent.onConnectionLost += OnConnectionLost;
		SFSEvent.onDebugMessage += OnDebugMessage;
	}
 
	void OnConnectionLost() {
		statusMessage = "Connection lost / no connection to server";
		UnregisterSFSSceneCallbacks();
		client.Disconnect();
		Application.LoadLevel("sc_Login");
	}
 
	private void UnregisterSFSSceneCallbacks() {
		// This should be called when switching scenes, so callbacks from the backend do not trigger code in this scene
		SFSEvent.onConnectionLost -= OnConnectionLost;
		SFSEvent.onDebugMessage -= OnDebugMessage;
	}
 
	public void OnDebugMessage(string message) {
		//Debug.Log("[SFS DEBUG] " + message);
	}
 
	// Update is called once per frame
	void Update () {
 
	}
 
	// this is called to update the status window
	public void UpdateStatusMessage(String message) {
		Debug.Log("Status Message= " + statusMessage );
		statusMessage = message;
	}
 
	void OnGUI() {
		// logout button
		if ( GUI.Button(new Rect(10, 10, 100, 24), "Logout")  ) {
			client = NetworkController.GetClient();
			client.SendPublicMessage(client.myUserName + " disconnected");
			client.Logout();
			client.Disconnect();
			UnregisterSFSSceneCallbacks();
			Application.LoadLevel("sc_Login");
		}
 
		// Draw box for status messages, if one is given
		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);
		}
	}
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox