Page 1 of 1

ConnectionGUI Example Script

Posted: 26 Mar 2011, 00:29
by Petroleum
Hi,

tried to implement the ConnectionGUI example C# Script, and I can't get anything to happen:

Code: Select all

using UnityEngine;
using System;
using Sfs2X;
using Sfs2X.Core;
using Sfs2X.Logging;

[RequireComponent(typeof(AudioSource))]

public class MainScreen : MonoBehaviour 
{ 
	//----------------------------------------------------------
	// Setup variables
	//----------------------------------------------------------
	public string ip = "127.0.0.1";
	public int port = 9933;
	private string statusMessage = "";
	private SmartFox smartFox;
	private bool success = false;
	//----------------------------------------------------------
	
	public Texture2D backgroundTexture;
    public string password = "",
    username = "",
    loginErrorMessage = " ",
    blockField = "*";

    private bool noName = false,
	noPassword = false
	;
	
	public GUIStyle s;
    private float timer = 0;

	private void StartSmartFox() {
		smartFox = new SmartFox();
		smartFox.AddEventListener(SFSEvent.CONNECTION, OnConnection);
		smartFox.AddLogListener(LogLevel.DEBUG, OnDebugMessage);
		smartFox.Connect(ip, port);
	}
	
	private void FixedUpdate() {
		if (success) smartFox.ProcessEvents();
	}

	public void OnConnection(BaseEvent evt) {
		success = (bool)evt.Params["Success"];
		if (success) 
		{
			success = true;
			statusMessage = "Connection Sucessfull!";
		}
		else
		{
			success = false;
			statusMessage = "Cannot Connect!";
		}
	}
	
	public void OnDebugMessage(BaseEvent evt) {
		string message = (string)evt.Params["Message"];
		Debug.Log("[SFS DEBUG] " + message);
	}
	
    private void Awake() 
    {	
		GameObject MainMusic = GameObject.Find("title");
		DontDestroyOnLoad(MainMusic); // Start the Sound.
	}

	// Update is called once per frame 
	private void Update() {
		if (timer <= 7.3f) timer += Time.deltaTime;
	}

	private void OnGUI() { 
		// Main Screen
		GUI.DrawTexture (new Rect (0, 0, Screen.width, Screen.height), backgroundTexture);
		if (timer >= 7.3f) LoginGUI();
		
		GUI.Label(new Rect(10, 10, 500, 100), "Status: " + statusMessage);
	}

	public void LoginGUI() {
		//PlayerPrefs.DeleteAll();
		// Login 
		GUI.Label(new Rect(Screen.width/2-170, Screen.height-150, 100, 100), "Username: "); 
		username = GUI.TextField(new Rect(Screen.width/2-100, Screen.height-150, 200, 20), username, 40); 

		GUI.Label(new Rect(Screen.width/2-170, Screen.height-125, 100, 100), "Password: "); 
		password = GUI.PasswordField(new Rect(Screen.width/2-100, Screen.height-125, 200, 20), password, "*"[0], 40); 
		
		if (noName) 
		{
			GUI.Label(new Rect(Screen.width/1.5f, Screen.height-150,500,100), "Please Provide a username!", s);
		}
		if (noPassword) 
		{
			GUI.Label(new Rect(Screen.width/1.5f, Screen.height-125,500,100), "Please Provide a Password!", s);
		}
		
		if (GUI.Button(new Rect(Screen.width/2-140, Screen.height-80, 100, 30), "Login")) 
		{ 
				if (String.IsNullOrEmpty(username))
				{
					noName = true;
					noPassword = false;
					return;
				}
				if (String.IsNullOrEmpty(password))
				{
					noPassword = true;
					noName = false;
					return;
				}
				else
				{
					StartSmartFox();
					if (success)
					Application.LoadLevel("CharMain");
				}
		}

        if (GUI.Button(new Rect(Screen.width / 2 + 30, Screen.height - 80, 100, 30), "Cancel"))
        {
			smartFox.Disconnect();
        }
	}
} 
What is wrong?

Posted: 26 Mar 2011, 08:17
by appels
I don't see anything wrong at first sight, try to troubleshoot the script.
Add some debug messages in the different steps of the proces and see where it stops