Tic Tac Toe Example MainMenu - TogetherGames/Public-Unity-CSharp GitHub Wiki
The MainMenu screen is displayed immediately after successfully logging in. From this screen, the User may visit the GameLobby, Leaderboards, Achievements, UserAchievements, Items, UserItems and ChatRooms. The user may also send a user notification to themselves, visit the Register screen to perform either Facebook or Custom registration, and add some stats to their ClientUserProfile's properties.
The DisplayText() method displays the User's UserID, Username, Name, and stats contained in the User's properties.
void DisplayText()
{
int labelY = 60;
int labelYStep = 25;
//Create and set the labels
GUI.Label(new Rect((Screen.width - 300) * 0.5f, 15, 300, 100), "Main Menu Scene", m_TitleStyle);
GUI.Label(new Rect((Screen.width - 300) * 0.5f, labelY, 300, 100), "UserID = " + m_User.UserID, m_TextStyle);
labelY += labelYStep;
GUI.Label(new Rect((Screen.width - 300) * 0.5f, labelY, 300, 100), "UserName = " + m_User.Username, m_TextStyle);
labelY += labelYStep;
GUI.Label(new Rect((Screen.width - 300) * 0.5f, labelY, 300, 100), "Name = " + m_User.Name, m_TextStyle);
labelY += labelYStep + 10;
if (m_User.Properties != null)
{
GUI.Label(new Rect((Screen.width - 300) * 0.5f, labelY, 300, 100), "Coins = " + m_Coins, m_TextStyle);
labelY += labelYStep;
GUI.Label(new Rect((Screen.width - 300) * 0.5f, labelY, 300, 100), "Cash = " + m_Cash, m_TextStyle);
labelY += labelYStep;
GUI.Label(new Rect((Screen.width - 300) * 0.5f, labelY, 300, 100), "Stars = " + m_Stars, m_TextStyle);
labelY += labelYStep;
GUI.Label(new Rect((Screen.width - 300) * 0.5f, labelY, 300, 100), "Score = " + m_Score, m_TextStyle);
labelY += labelYStep;
}
}
The DisplayButtons() method displays all the buttons on the screen. Most simply transition the application to the appropriate scene.
void DisplayButtons()
{
//Create and set our buttons
if (GUI.Button(new Rect(10, 50, 100, 50), "Logout"))
{
Application.LoadLevel("Login");
}
if (GUI.Button(new Rect(10, 110, 100, 50), "Register"))
{
Application.LoadLevel("Register");
}
if (GUI.Button(new Rect((Screen.width - 140) * 0.5f,(Screen.height - 210 ) * 0.5f, 140, 50), "Game Lobby"))
{
Together.Instance.AddAnalytic("EnterMode", "GameLobby", "", null);
Application.LoadLevel("GameLobby");
}
if (GUI.Button(new Rect(10, (Screen.height + 450 ) * 0.5f, 140, 50), "Create Notification"))
{
OnCreateNotificationButtonClicked();
}
if (GUI.Button(new Rect((Screen.width - 110), 50, 100, 50), "Add Stats"))
{
OnUpdateStatsButtonClicked();
}
if (GUI.Button(new Rect((Screen.width - 140) * 0.5f,(Screen.height - 100) * 0.5f, 140, 50), "Leaderboards"))
{
OnViewLeaderboardsButtonClicked();
}
if (GUI.Button(new Rect((Screen.width - 140) * 0.5f,(Screen.height + 10 ) * 0.5f, 140, 50), "Achievements"))
{
OnViewAchievementsButtonClicked();
}
if (GUI.Button(new Rect((Screen.width - 140) * 0.5f,(Screen.height + 120 ) * 0.5f, 140, 50), "User Achievements"))
{
OnViewUserAchievementsButtonClicked();
}
if (GUI.Button(new Rect((Screen.width - 140) * 0.5f,(Screen.height + 230 ) * 0.5f, 140, 50), "Items"))
{
OnViewItemsButtonClicked();
}
if (GUI.Button(new Rect((Screen.width - 140) * 0.5f,(Screen.height + 340 ) * 0.5f, 140, 50), "User Items"))
{
OnViewUserItemsButtonClicked();
}
if (GUI.Button(new Rect((Screen.width - 140) * 0.5f,(Screen.height + 450 ) * 0.5f, 140, 50), "Chat Rooms"))
{
OnViewChatRoomsButtonClicked();
}
if (GUI.Button(new Rect((Screen.width - 140) * 0.5f,(Screen.height + 560) * 0.5f, 140, 50), "Wall Post"))
{
OnWallPostButtonClicked();
}
}
The OnCreateNotificationButtonClicked() method creates and sends a Together UserNotification to the logged in User.
void OnCreateNotificationButtonClicked()
{
Together.Instance.UserNotificationManager.Create(
m_User.UserID, // destUserID
"Query", // type
"Notification sent from MainMenu.", // message
0, // originalGameInstanceID
0, // gameInstanceID
0, // achievementID
0, // chatRoomID
0, // itemID
0, // itemCount
"", // socialType
"", // socialID
onCreatedUserNotification); // callbackFunc
}
The OnUpdateStatsButtonClicked() method increments the (Coins, Cash, Stars, Score) properties on the User object.
void OnUpdateStatsButtonClicked()
{
int coins = int.Parse(m_User.Properties.GetEx("Coins", "0"));
float cashValue;
string cash = m_User.Properties.GetEx("Cash", "0.00");
int stars = int.Parse(m_User.Properties.GetEx("Stars", "0"));
int score = int.Parse(m_User.Properties.GetEx("Score", "0"));
coins += 10;
stars += 10;
score += 10;
cashValue = float.Parse(cash);
cashValue += 10.0f;
cash = "" + cashValue;
m_User.Properties.Set ("Coins", coins.ToString());
m_User.Properties.Set ("Stars", stars.ToString());
m_User.Properties.Set ("Score", score.ToString());
m_User.Properties.Set ("Cash", cash.ToString());
Debug.Log("Modifying User...");
Debug.Log(" Together.Instance = " + Together.Instance);
Debug.Log(" Together.Instance.User = " + Together.Instance.User);
Together.Instance.User.Modify(onUserModified);
}
void onUserModified(TogetherCallback tcb)
{
Debug.Log("onUserModified()");
if (tcb.Success)
refreshText();
else
Helper.Popup("Uh oh", tcb.Message, 0);
}