Tic Tac Toe Example GameLobby - TogetherGames/Public-Unity-CSharp GitHub Wiki
The GameLobby screen displays a list of GameInstances that the User is a part of. The User can create a new GameInstance or join/play an existing GameInstance by pressing one of the GameInstance buttons.
The Start() method calls GameInstanceManager.GetAll() method to retrieve all GameInstances that are either incomplete games the User is a member of, rematches to previous games the User has played, or open games that are still waiting for Users to join.
// List of Active Games
m_ActiveGameMask = GameStateMask.IN_PROGRESS | GameStateMask.POSSIBLE_REMATCH | GameStateMask.WAITING_FOR_PLAYERS;
Together.Instance.GameInstanceManager.GetAll(0, m_ActiveGameMask, 10, true, false, onGotAllGames);
The DisplayButtons() method displays a Create button, a Join button, and a list of GameInstances buttons for all GameInstances retrieved.
void DisplayButtons()
{
//Create and set all our buttons
if( GUI.Button(new Rect(10, 50, 100, 50), "Back"))
Application.LoadLevel("MainMenu");
if(m_bDisplayGames)
{
for (int i = 0; i < m_games.Length; i++)
{
if( GUI.Button(new Rect((Screen.width - 400) * 0.5f, 355 + (60 * i), 400, 50), m_displayStrings[i]))
OnGameClicked(m_games[i]);
}
}
//TODO need to grab the specific game Properties
if( GUI.Button(new Rect((Screen.width - 200) * 0.5f, 190, 200, 50), "Create Game"))
OnCreateButtonClicked ();
if( GUI.Button(new Rect((Screen.width - 200) * 0.5f, 250, 200, 50), "Join Random Game"))
OnJoinRandomButtonClicked ();
}
When the Create button is pressed, a new GameInstances is created using the GameInstanceManager.Create() method.
When the Create button is pressed, a new GameInstance is created by calling the GameInstanceManager.Create() method. In the onRandomGameJoined() callback method, the waitTime member is set to REFRESH_TIME which will cause the Update() method to immediately refresh the list of GameInstances.
void OnCreateButtonClicked()
{
PropertyCollection propertyCollection = new PropertyCollection();
propertyCollection.Name = "a";
propertyCollection.Set ("Data", "_________");
Together.Instance.GameInstanceManager.Create(
"", // gameInstanceType
0, // roomID
2, // maxUsers
false, // passTurn
propertyCollection, // gameInstanceProperties,
null, // gameInstanceUserProperties
onGameInstanceCreated); // callbackFunc
}
void onGameInstanceCreated(TogetherCallback tcb)
{
if (tcb.Success)
{
// Refresh the list of GameInstances during next Update() call.
waitTime = REFRESH_TIME;
}
else
Helper.Popup("Uh oh", tcb.Message, 0);
}
When the Join Random button is pressed, an attempt to join a random GameInstance will be made using the GameInstanceManager.JoinRandom() method. In the onRandomGameJoined() callback method, the waitTime member is set to REFRESH_TIME which will cause the Update() method to immediately refresh the list of GameInstances.
void OnJoinRandomButtonClicked()
{
Together.Instance.GameInstanceManager.JoinRandom(
"", // gameInstanceType
null, // gameInstanceUserProperties
onRandomGameJoined); // callbackFunc
}
void onRandomGameJoined(TogetherCallback tcb)
{
if (tcb.Success)
{
// Refresh the list of GameInstances during next Update() call.
waitTime = REFRESH_TIME;
}
else
Helper.Popup("Uh oh", tcb.Message, 0);
}
When a GameInstance button is pressed, the GameInstances pressed is assigned to the Helper.UserData member before the GameInstance scene is loaded. The Helper.UserData member is used to pass initialization data from one screen to another.