Hello! Today, we will learn how to create a simple network lobby screen using Unity. In this tutorial, we will implement the room creation feature and explain how users can enter or create rooms. Let’s understand the basic concepts of network game development and practice using Unity’s networking features.
1. Unity and Networking
Unity is a powerful game engine that provides various features for network game development. The networking API provided by Unity, UNET (Unity Networking), helps to easily implement various networking functionalities. However, UNET is no longer actively supported, and it’s advisable to use Unity’s new networking solutions, Mirror or Photon. This tutorial will focus on creating a network room using Mirror.
2. Project Setup
Create a new project in Unity. Set the basic template of the project to 3D and name the project ‘NetworkLobby’. Then, let’s proceed with the following steps.
2.1 Installing the Mirror Package
1. In the top menu of the Unity Editor, select Window > Package Manager.
2. Click the + button and select Install from git URL….
3. Enter https://github.com/vis2k/Mirror.git in the URL input field.
4. Once the installation is complete, the Mirror package will appear in the package list.
2.2 Setting Up the Basic Scene
1. Select File > New Scene to create a new scene.
2. Create a Canvas to configure the basic UI. Right-click in the Hierarchy window and select UI > Canvas to add a new Canvas.
3. Add UI elements under the Canvas. Add buttons, input fields, and text to create the lobby screen.
3. Setting Up the Network Manager
The core of a network game is the Network Manager. The Network Manager manages the connection between clients and the server. In this process, we will set up the Network Manager and write basic code for room management.
3.1 Adding the NetworkManager
1. In the Hierarchy window, right-click and select Network > NetworkManager to add a new network manager.
2. Select the NetworkManager object and adjust the settings in the inspector. Remove the Fake Player Prefab in Spawn Info and add TelepathyTransport in the Transport section.
3.2 Writing a Custom Script
Write a controlling script to implement room creation and entry features. Create a new C# script and name it NetworkLobbyManager.cs.
using Mirror;
using UnityEngine;
using UnityEngine.UI;
public class NetworkLobbyManager : NetworkManager
{
public InputField roomNameInput;
public Button createRoomButton;
public Button joinRoomButton;
public override void OnStartServer()
{
Debug.Log("Server started!");
}
public void CreateRoom()
{
if (string.IsNullOrEmpty(roomNameInput.text))
{
Debug.Log("Room name cannot be empty!");
return;
}
// Handle room creation
Debug.Log("Room created: " + roomNameInput.text);
NetworkServer.Listen(12345);
}
public void JoinRoom()
{
// Handle room entry
Debug.Log("Joined room: " + roomNameInput.text);
networkAddress = roomNameInput.text;
StartClient();
}
public void OnEnable()
{
createRoomButton.onClick.AddListener(CreateRoom);
joinRoomButton.onClick.AddListener(JoinRoom);
}
public void OnDisable()
{
createRoomButton.onClick.RemoveListener(CreateRoom);
joinRoomButton.onClick.RemoveListener(JoinRoom);
}
}
This script contains the basic logic for creating and entering rooms. It allows users to create or enter a room based on the room name they input.
4. UI Configuration
To improve user experience, adjust the UI layout and styles to configure the UI elements more specifically.
4.1 Configuring Inside the Canvas
1. Add a Panel under the Canvas. This panel will be the main background of the lobby.
2. Add a title text above the panel (e.g., “Game Lobby”).
3. Add room name input field along with buttons for creating and joining rooms.
4.2 Connecting Button Events
Connect the create and join room buttons to the methods of the network manager. In the Unity editor, assign the relevant methods to the buttons’ onClick() events.
5. Testing and Debugging
Once all functionalities are implemented, it is necessary to conduct final testing. Run several instances of the Unity editor to check if clients can create and enter rooms.
5.1 Running the Server
Click the Start Game button in the Unity editor to run the server. Ensure that the server is operating correctly.
5.2 Running the Client
With the server running, execute the client in another instance and enter the room. This process will allow you to see multiple clients interacting.
6. Conclusion
In this tutorial, we learned how to create a simple network lobby screen using Unity. We implemented basic features for room creation and entry using Mirror, and demonstrated how to enhance user experience through simple UI configuration and event handling.
Network game development becomes more fascinating the more you learn. Based on the basic knowledge gained in this tutorial, I encourage you to explore more complex network systems.
Thank you!