Basic Unity Course: Displaying the Network Room List

1. Introduction

In game development, multiplayer features provide a variety of experiences. In this tutorial, we will learn how to display a network room list using Unity. This tutorial is written for beginners and is based on an understanding of basic Unity usage and C# programming.

2. Unity and Networking

Unity offers various networking solutions. UNet (Unity Network) is one of them, useful for setting up basic multiplayer environments. As of Unity 2020.1, UNet has been deprecated, and the new networking framework MLAPI has been introduced. This tutorial will focus on MLAPI.

3. Preparation

3.1. Installing Unity

To install Unity, download Unity Hub and install the desired version of Unity. MLAPI is supported on Unity version 2019.4 and above.

3.2. Adding the MLAPI Package

To add MLAPI to your project, use the Unity Package Manager. In the Unity editor, select ‘Window’ -> ‘Package Manager’, then select ‘Add package from git URL…’ and enter https://github.com/Unity-Technologies/Mirror.git.

4. Creating a New Unity Project

Create a new Unity project and select the 3D template. Once the project is created, you will be ready to add all necessary assets and networking-related scripts.

5. Setting Up the Scene

Create a new scene and add a basic camera and lighting. Add a skybox and a few 3D objects to create a test environment.

6. Adding a Network Manager

To manage the network, add a NetworkManager component to the scene. The NetworkManager is responsible for creating and managing network rooms. After creating the game object, set it up as follows:

NetworkManager manager = gameObject.AddComponent<NetworkManager>();

7. Setting Up UI to Display Room List

7.1. Adding UI Elements

To display the room list in the UI, add a Canvas and use Text and Button elements to construct the UI that will display the room list.

7.2. Writing UI Scripts

Write a script to display the room list. Below is an example of the basic code to retrieve room information.


using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System.Collections.Generic;

public class RoomList : MonoBehaviour
{
    public GameObject roomButtonPrefab;
    public Transform roomListContainer;

    void Start()
    {
        NetworkManager.singleton.OnMatchList += OnMatchList;
        NetworkManager.singleton.ListMatches(0, 20, "", OnMatchList);
    }

    void OnMatchList(bool success, string extendedInfo, List matches)
    {
        foreach (Transform child in roomListContainer)
        {
            Destroy(child.gameObject);
        }

        if (success)
        {
            foreach (var match in matches)
            {
                Instantiate(roomButtonPrefab, roomListContainer).GetComponentInChildren().text = match.name;
            }
        }
    }
}

8. Creating a Room

Additional functionality is needed to allow users to create a room by clicking a button. Implement room creation by adding the following function.


public void CreateRoom()
{
    NetworkManager.singleton.StartMatchMaker();
    NetworkManager.singleton.matchMaker.CreateMatch("RoomName", 2, true, "", "", "", 0, 0, OnMatchCreate);
}

void OnMatchCreate(bool success, string extendedInfo, MatchInfo match)
{
    if (success)
    {
        NetworkManager.singleton.StartHost();
    }
}

9. Joining a Room

Add a UI button to allow users to join a room and implement the functionality to join a room with the following code.


public void JoinRoom(MatchInfoSnapshot match)
{
    NetworkManager.singleton.StartMatchMaker();
    NetworkManager.singleton.matchMaker.JoinMatch(match.networkId, "", OnMatchJoin);
}

void OnMatchJoin(bool success, string extendedInfo, MatchInfo match)
{
    if (success)
    {
        NetworkManager.singleton.StartClient();
    }
}

10. Testing and Debugging

Once all scripts and UI are complete, test the multiplayer functionality in the Unity editor. Create a room and check the room list from another client to verify everything is working correctly.

11. Conclusion

This tutorial taught you how to display a network room list in Unity. Additional features such as room deletion, modifying room information, and UI improvements can provide a better user experience. We plan to continue covering in-depth topics on Unity and networking.

12. References