Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question IMessagingService example

Discussion in 'Netcode for GameObjects' started by ratio_l, Feb 27, 2024.

  1. ratio_l

    ratio_l

    Joined:
    Mar 2, 2021
    Posts:
    20
    Hello! im implementing a friends system for my multyplayer game using Unity friends service, netcode and relay. Now i need to invite friend to lobby. ive heard that IMessagingService interface and its MessageAsync methods are a good way to do so. But i couldnt find any code examples of this interface, can someone please share? and i dont understand how other client recieve the message.
     
  2. qwetito

    qwetito

    Joined:
    Jul 22, 2021
    Posts:
    4
    Inviting Friends to a Lobby Using Unity Services

    Hello there! I hope it's not too late. It's funny because I'm working on the same thing. My approach to this problem includes using the Unity Friends service, the Unity Lobbies Service, and Photon Fusion.

    Also I'm using the UGS Friends sample https://docs.unity.com/ugs/en-us/manual/friends/manual/samples/friends-sample

    Setting Up the UI
    1. In the Unity Friends sample, you add a button to invite friends to the lobby within the “FriendEntryViewUGUI.prefab.”
    2. You extended the IFriendsListView interface with an Action<string> called onInvite.
    Handling Friend Invitations
    Code (CSharp):
    1.  
    2. public class FriendsViewUGUI : ListViewUGUI, IFriendsListView
    3. {
    4.    // ...
    5.  
    6.    public override void Refresh()
    7.    {
    8.        // ...
    9.        foreach (var friendsEntryData in m_FriendsEntryDatas)
    10.        {
    11.            // Instantiate friend entries and set up buttons
    12.            var entry = Instantiate(m_FriendEntryViewPrefab, m_ParentTransform);
    13.            // ...
    14.            entry.inviteFriendButton.onClick.AddListener(() =>
    15.            {
    16.                onInvite?.Invoke(friendsEntryData.Id);
    17.            });
    18.            // ...
    19.        }
    20.    }
    21. }
    22.  
    Sending Invitations
    1. In your RelationshipManager.cs, you’ve to define a Message class to hold sender information and message content.

    public class Message
    {
    public string Name { get; set; }
    public string Content { get; set; }
    }

    1. When inviting a friend, you send the lobby ID along with the invitation message.
    2. Subscribing to the onInvite action, you call SendMessageAsync to send the invite message.
    Code (CSharp):
    1.  
    2. m_FriendsListView.onInvite += SendMessageAsync;
    3.  
    4. async void SendMessageAsync(string id)
    5. {
    6.    await SendInviteMessage(id, LobbyId);
    7. }
    8.  
    9. async Task SendInviteMessage(string playerId, string messageContent)
    10. {
    11.    try
    12.    {
    13.        var message = new Message
    14.        {
    15.            Name = m_LoggedPlayerProfile.Name,
    16.            Content = messageContent
    17.        };
    18.  
    19.        await FriendsService.Instance.MessageAsync(playerId, message);
    20.    }
    21.    catch (FriendsServiceException e)
    22.    {
    23.        Debug.LogError(e);
    24.    }
    25. }
    26.  
    Handling Received Messages
    1. In your LobbyManager, you subscribe to the MessageReceived action.
    2. When a message arrives, you extract the sender’s name and content.
    3. You then join the lobby using the extracted lobby code.
    Code (CSharp):
    1.  
    2. void Start()
    3. {
    4.    // ...
    5.    FriendsService.Instance.MessageReceived += e =>
    6.    {
    7.        var messageData = e.GetAs<Message>();
    8.        Debug.Log("MessageReceived EventReceived: " + messageData.Name + " - " + messageData.Content);
    9.        JoinLobbyByCode(messageData.Content);
    10.    };
    11. }
    12.  
    13. private async void JoinLobbyByCode(string lobbyCode)
    14. {
    15.    try
    16.    {
    17.        JoinLobbyByCodeOptions options = new JoinLobbyByCodeOptions
    18.        {
    19.            Player = GetPlayer()
    20.        };
    21.  
    22.        _joinedLobby = await LobbyService.Instance.JoinLobbyByCodeAsync(lobbyCode, options);
    23.        Log("Join lobby by code: " + lobbyCode);
    24.        PrintPlayers(_joinedLobby);
    25.    }
    26.    catch (LobbyServiceException ex)
    27.    {
    28.        Debug.Log(ex);
    29.    }
    30. }
    31.  
    Feel free to ask questions or provide further insights. Happy coding!
     
    ratio_l likes this.