Multiplayer UI using Mirror

Hey all, im pretty new to the mirror package and im trying to build a simple multiplayer game thats basically played using UI elements only (buttons). A big part of the game is announcements that show up like - “Player X’s Turn” for example.

At the moment the announcement only shows up in the host game, if the announcement came from the Networkbehaviour class that belongs to the player it would be easy to solve with a simple ClientRPC function but i want the UI functions to run from a different class that handles the UI elements.

what is the correct way to implement this? does the UIHandler need to be inhereting from any network class? would love some tips regarding this subject.

thanks in advance,
Amit Wolf.

The correct way might be a little involved, so here’s a very quick and very dirty way of updating text for all players. It might raise more questions than answers but will hopefully give you some ideas. I’ve gone with end turn rather then next turn as it saves cycling through players. Player netId is used just for a unique identifier.

public class UIHandler : NetworkBehaviour
{
    public TextMeshProUGUI text;

    public void OnClick()
    {
        uint netId = NetworkClient.localPlayer.netId;

        Debug.Log("OnClick netId: " + netId);

        CmdEndTurn(netId);
    }

    [Command(requiresAuthority = false)]
    public void CmdEndTurn(uint netId)
    {
        Debug.Log("CmdEndTurn netId: " + netId);
        RpcTurnEnded(netId);
    }

    [ClientRpc]
    public void RpcTurnEnded(uint netId)
    {
        Debug.Log("RpcTurnEnded netId: " + netId);
        text.text = "Player " + netId + " turn ended";
    }
}

The idea is to share one UIHandler among all clients and have it update the UI on all of them. To get it working do the following in your online scene:

  • Create an empty game object and add the UIHandler script and a Network Identity
  • Create a button, for the OnClick listener add the above’s OnClick method
  • Create a text UI element (TextMeshPro) and drag that to the UIHandler text field
  • Run the project from the offline scene and see if it falls over

What should happen is, the OnClick method is called which in turn calls CmdEndTurn with the netId. This method is run on the server which in turn calls RpcTurnEnded which is run on all the clients and should update the text with the netId of the turn ending player.

At least, that’s the theory. :slight_smile: