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

The ChatRoom screen displays a ChatRoom. A list of Users in the ChatRoom and ChatMessages belonging to the ChatRoom are all displayed. A series of buttons is displayed on the left allowing the User to: Join the ChatRoom, Leave the ChatRoom, Delete the ChatRoom, Delete the last ChatMessage, Mark the top ChatMessage as read, Display the Friend Lobby screen so the User can invite a friend, and Send a Message.

The Start() method calls GetChatRoomDetails(). The GetChatRoomDetails() method gets the details of the current ChatRoom via the ChatRoom.GetDetails() method.

void GetChatRoomDetails()
{
	Debug.Log ("Call GetChatRoomDetails");
	m_chatRoom.GetDetails( m_chatRoom.ChatRoomID, 0, onGotChatRoomDetails);
}

The DisplayText() method displays all the Users in the ChatRoom and all the ChatMessages belonging to the ChatRoom.

void DisplayText()
{
    //input field see if they enter anything for a message
    m_input = GUI.TextField(new Rect(10, 520, 300, 25), m_input);

    GUI.Label(new Rect((Screen.width - 300) * 0.5f, 15, 300, 100), "Chat Room", m_TitleStyle);
    GUI.Label(new Rect((Screen.width - 325), 75, 300, 100), "Users", m_TitleStyle);
    GUI.Label(new Rect((Screen.width - 300) * 0.5f, 200, 300, 100), "Message", m_TitleStyle);

    //The Messages
    if (m_messages != null)
    {
        for (int i = 0; i < m_messages.Length; i++)
        {
            GUI.Label(new Rect((Screen.width - 300) * 0.5f, 250 + (25 * i), 300, 40), m_messages[i], m_TextStyle);
        }
    }

    //The Users
    if (m_userNames != null)
    {
        for (int i = 0; i < m_userNames.Length  ; i++)
        {
            GUI.Label(new Rect((Screen.width - 325), 125 + (25 * i), 300, 40), m_userNames[i], m_TextStyle);
        }
    }
}

The DisplayButtons() method displays all the buttons on the left side of the screen.

void DisplayButtons()
{
    //Create and set the buttons
    if( GUI.Button(new Rect(10, 50, 100, 40), "Back"))
        GoToChatRoomLobby();

    if( GUI.Button(new Rect(10, 120, 180, 40), "Join Room"))
    {
        Debug.Log("Join Room");
        m_chatRoom.Join(onMessageAction);
    }
    if( GUI.Button(new Rect(10, 175, 180, 40), "Leave Room"))
    {
        Debug.Log("Leave Room");
        //Leave the current room
        m_chatRoom.Leave(onDeletedOrLeft);
    }
    if( GUI.Button(new Rect(10, 230, 180, 40), "Delete Room"))
    {
        Debug.Log("Delete Room");
        Together.Instance.ChatRoomManager.Delete(m_chatRoom.ChatRoomID, onDeletedOrLeft);
    }
    if( GUI.Button(new Rect(10, 285, 180, 40), "Delete Last Message"))
    {
        Debug.Log("Delete Last Message");
        if (m_chatRoom.GetChatMessageCount() > 0)
        {
            ChatMessage chatMessage = m_chatRoom.GetChatMessage(m_chatRoom.GetChatMessageCount()-1);
            m_chatRoom.DeleteMessage(chatMessage.ChatMessageID, onMessageAction);
        }
    }
    if( GUI.Button(new Rect(10, 340, 180, 40), "Mark Last Message As Read"))
    {
        Debug.Log("Mark Last Mesage As Read");
        if (m_chatRoom.GetChatMessageCount() > 0)
        {
            ChatMessage markMessage = m_chatRoom.GetChatMessage(m_chatRoom.GetChatMessageCount()-1);
            m_chatRoom.MarkMessageAsRead(markMessage.ChatMessageID, onMessageAction);
        }
    }
    if( GUI.Button(new Rect(10, 395, 180, 40), "Invite Friend"))
    {
        OnInviteFriendButtonClicked();
    }
    if( GUI.Button(new Rect(10, 450, 180, 40), "Send Message"))
    {
        SendTextMessage();
    }
}

When the Join Room button is pressed, the ChatRoom is joined using the ChatRoom.Join() method.

When the Leave Room button is pressed, the ChatRoom is left using the ChatRoom.Leave() method.

When the Delete Room button is pressed, the ChatRoom is deleted using the ChatRoomManager.Delete() method.

When the Delete Last Message button is pressed, the last ChatMessage is deleted using the ChatRoom.DeleteMessage() method.

When the Mark Last Message As Read button is pressed, the last ChatMessage is marked as read using the ChatRoom.MarkMessageAsRead() method.

When the Invite Friend button is pressed, the FriendLobby screen is displayed allowing the User to select a friend to invite to the ChatRoom.

When the Send Message button is pressed, the text in the text field below the button is submitted to the ChatRoom.