Converting a Single Player Game to Multiplayer
This document describes steps to convert a single player game to a multiplayer game using the new networking system. The process described here is a simplified, higher level version of the actual process for a real game, and wont work exactly like this for every game, but it provides a basic recipe for the process.
NetworkManager Setup
- Add a new game object to the scene and rename it to “NetworkManager”.
- Add the NetworkManager component to the new game object.
- Add the NetworkManagerHUD component to the game object. This will provide the default UI for managing network game state.
See Using the NetworkManager.
Player Prefab Setup
- Find the prefab for the player object in the game, or create a prefab from the player object.
- Add NetworkIdentity component to player prefab.
- Check the LocalPlayerAuthority box on the NetworkIdentity.
- Set the playerPrefab in the “Spawn Info” section on the NetworkManager to the player prefab
- Remove the player object instance from the scene if it exists in the scene
See Player Objects.
Player Movement
- Add a NetworkTransform component to the player prefab
- Update input and control scripts to respect isLocalPlayer
- Fix camera 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 Objects
Fix non-player prefabs such as enemies:
- Add NetworkIdentify component
- Add NetworkTransform component
- Register spawnable prefabs with 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 OnStartServer() function
- Call NetworkServer.Spawn() for created objects
Spawn Positions for players
- Add new game object and place at player start location
- Add NetworkStartPosition component to new game object
Lobby
- Create Lobby Scene
- Import multiplayer-lobby package
- Add GuiLobbyManager prefab to scene
- Configure the manager
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。