Note: UNet is deprecated, and will be removed from Unity in the future. A new system is under development. For more information and next steps, see the blog posts on Evolving multiplayer games beyond UNet and Navigating Unity’s multiplayer Netcode transition, and the FAQ. |
Many multiplayer games can use the Network ManagerA Networking component that manages the network state of a project. More info
See in Glossary to manage connections, but you can also use the lower-level NetworkServer and NetworkClient classes directly.
When using the High-Level API, every game must have a host server to connect to. Each participant in a multiplayer game can be a client, a dedicated server, or a combination of server and client at the same time. This combination role is the common case of a multiplayer game with no dedicated server.
For multiplayer games with no dedicated server, one of the players running the game acts as the server for that game. That player’s instance of the game runs a “local client” instead of a normal remote client. The local client uses the same Unity ScenesA Scene contains the environments and menus of your game. Think of each unique Scene file as a unique level. In each Scene, you place your environments, obstacles, and decorations, essentially designing and building your game in pieces. More info
See in Glossary and GameObjectsThe fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. A GameObject’s functionality is defined by the Components attached to it. More info
See in Glossary as the server, and communicates internally using message queues instead of sending messages across the network. To HLAPI code and systems, the local client is just another client, so almost all user code is the same, whether a client is local or remote. This makes it easy to make a game that works in both multiplayer and standalone mode with the same code.
A common pattern for multiplayer games is to have a GameObject that manages the network state of the game. Below is the start of a NetworkManager script. This script would be attached to a GameObject that is in the start-up Scene of the game. It has a simple UI(User Interface) Allows a user to interact with your application. More info
See in Glossary and keyboard handling functions that allow the game to be started in different network modes. Before you release your game you should create a more visually appealing menu, with options such as “Start single player game” and “Start multiplayer game”.
using UnityEngine;
using UnityEngine.Networking;
public class MyNetworkManager : MonoBehaviour {
public bool isAtStartup = true;
NetworkClient myClient;
void Update ()
{
if (isAtStartup)
{
if (Input.GetKeyDown(KeyCode.S))
{
SetupServer();
}
if (Input.GetKeyDown(KeyCode.C))
{
SetupClient();
}
if (Input.GetKeyDown(KeyCode.B))
{
SetupServer();
SetupLocalClient();
}
}
}
void OnGUI()
{
if (isAtStartup)
{
GUI.Label(new Rect(2, 10, 150, 100), "Press S for server");
GUI.Label(new Rect(2, 30, 150, 100), "Press B for both");
GUI.Label(new Rect(2, 50, 150, 100), "Press C for client");
}
}
}
This basic code calls setup functions to get things going. Below are the simple setup functions for each of the scenarios. These functions create a server, or the right kind of client for each scenario. Note that the remote client assumes the server is on the same machine (127.0.0.1). For a finished game this would be an internet address, or something supplied by the Matchmaking system.
// Create a server and listen on a port
public void SetupServer()
{
NetworkServer.Listen(4444);
isAtStartup = false;
}
// Create a client and connect to the server port
public void SetupClient()
{
myClient = new NetworkClient();
myClient.RegisterHandler(MsgType.Connect, OnConnected);
myClient.Connect("127.0.0.1", 4444);
isAtStartup = false;
}
// Create a local client and connect to the local server
public void SetupLocalClient()
{
myClient = ClientScene.ConnectLocalServer();
myClient.RegisterHandler(MsgType.Connect, OnConnected);
isAtStartup = false;
}
The clients in this code register a callback function for the connection event MsgType.Connect. This is a built-in message of the HLAPI that the script invokes when a client connects to a server. In this case, the code for the handler on the client is:
// client function
public void OnConnected(NetworkMessage netMsg)
{
Debug.Log("Connected to server");
}
This is enough to get a multiplayer application up and running. With this script you can then send network messages using NetworkClient.Send and NetworkServer.SendToAll. Note that sending messages is a low level way of interacting with the system.
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.