Tic Tac Toe Example Login - TogetherGames/Public-Unity-CSharp GitHub Wiki

The Login screen is the first screen displayed when the TicTacToe application runs.

The OnGUI() method displays a label showin the Together ServerIP the TicTacToe application talks to. Login.cs also displays a Login button that when clicked, will send a network message to log the user in.

void OnGUI()
{
	string serverIP = Together.Instance.ServerIP;

	//Create and set our labels
	GUI.Label(new Rect((Screen.width - 200) * 0.5f, 15, 200, 100), "Login Scene", m_TitleStyle);
	GUI.Label(new Rect((Screen.width - 400) * 0.5f, 250, 400, 300), "ServerIP = " + serverIP, m_TextStyle);
	
	//Create and set the login button
	if( GUI.Button(new Rect((Screen.width - 100) * 0.5f, (Screen.height - 100 ) * 0.5f, 100, 50), "Login"))
	{
		Debug.Log("Login:  Logging in User.");
		LoginUser();
	}
}

The LoginUser() method is called whenever the user clicks on the Login button.

public void LoginUser()
{
	Together.Instance.LoginUser(onLoggedIn);
}
public void onLoggedIn(PlaysTogether.TogetherCallback tcb)
{	
	Debug.Log("onLoggedIn "+ tcb);
	if (tcb.Success)
	{
		Debug.Log("  Successfully logged in the user.");
		Application.LoadLevel("MainMenu");
	}
	else
		Helper.Popup("Uh oh", tcb.Message, 0);
}

When Togther.Instance.LoginUser() is called, the callback function onLoginUser() is passed in.

When the onLoginUser() callback function is executed, the Success member of TogetherCallback is examined to determine if an error occured. If an error occured, an error alert is immediately displayed. If the User was successfully logged in, the application enters the Main state.

In general, it is always good practice to wait for the response from the server before moving on to the next step.