Version: 2018.4
The Network Manager HUD in Matchmaker mode
Debugging Information

Converting a single-player game to Unity Multiplayer

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 this blog post.

This document describes steps to converting a single player game to a multiplayer game, using the new Unity Multiplayer networkingThe Unity system that enables multiplayer gaming across a computer network. More info
See in Glossary
system. The process described here is a simplified, higher level version of the actual process for a real game; it doesn’t always work exactly like this, but it provides a basic recipe for the process.

NetworkManager set-up

  • Add a new GameObjectThe 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
    to the SceneA 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 rename it “NetworkManager”.
  • Add the NetworkManager component to the “NetworkManager” GameObject.
  • Add the NetworkManagerHUDA Networking component that creates a UI menu that allows you to control the network state of your game using your Network Manager. More info
    See in Glossary
    component to the GameObject. This provides the default UI(User Interface) Allows a user to interact with your application. More info
    See in Glossary
    for managing the network game state.

See Using the NetworkManager.

Player PrefabAn asset type that allows you to store a GameObject complete with components and properties. The prefab acts as a template from which you can create new object instances in the scene. More info
See in Glossary
set-up

  • Find the Prefab for the player GameObject in the game, or create a Prefab from the player GameObject
  • Add the NetworkIdentityA Networking component that allows you to assign an identity to your GameObject for the network to recognize it as a Local Player GameObject or a Server Only GameObject. More info
    See in Glossary
    component to the player Prefab
  • Check the LocalPlayerAuthority box on the NetworkIdentity
  • Set the playerPrefab in the NetworkManager’s Spawn Info section to the player Prefab
  • Remove the player GameObject instance from the Scene if it exists in the Scene

See Player ObjectsA High Level API (HPAPI) object that represents the player on the server and has the ability to run commands (which are secure client-to-server remote procedure calls) from the player’s client. More info
See in Glossary
for more information.

Player movement

  • Add a NetworkTransform component to the player Prefab
  • Update input and control scriptsA piece of code that allows you to create your own Components, trigger game events, modify Component properties over time and respond to user input in any way you like. More info
    See in Glossary
    to respect isLocalPlayer
  • Fix CameraA component which creates an image of a particular viewpoint in your scene. The output is either drawn to the screen or captured as a texture. More info
    See in Glossary
    to use spawned player and isLocalPlayer

For example, this script only processes input for the local player:

using UnityEngine;
using UnityEngine.Networking;

public class Controls : NetworkBehaviour
{
    void Update()
    {
        if (!isLocalPlayer)
        {
            // exit from update if this is not the local player
            return;
        }

        // handle player input for movement
    }
}

Basic player game state

  • Make scripts that contain important data into NetworkBehaviours instead of MonoBehaviours
  • Make important member variables into SyncVars

See State Synchronization.

Networked actions

  • Make scripts that perform important actions into NetworkBehaviours instead of MonoBehaviours
  • Update functions that perform important player actions to be commands

See Networked Actions.

Non-player GameObjects

Fix non-player prefabs such as enemies:

  • Add the NetworkIdentify component
  • Add the NetworkTransformA Networking component that allows you to synchronize the movements of GameObjects across the network. More info
    See in Glossary
    component
  • Register spawnable Prefabs with the NetworkManager
  • Update scripts with game state and actions

Spawners

  • Potentially change spawner scripts to be NetworkBehaviours
  • Modify spawners to only run on the server (use isServer property or the OnStartServer() function)
  • Call NetworkServer.Spawn() for created GameObjects

Spawn positions for players

  • Add a new GameObject and place it at player’s start location
  • Add the NetworkStartPosition component to the new GameObject

Lobby

  • Create Lobby Scene
  • Add a new GameObject to the Scene and rename it to “NetworkLobbyManager”.
  • Add the NetworkLobbyManager component to the new GameObject.
  • Configure the Manager:
    • Scenes
    • Prefabs
    • Spawners
The Network Manager HUD in Matchmaker mode
Debugging Information
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961