Article directory
1. Add basic components
- NetworkManager
- Player
- Scene
2. Create NetworkManager component
Create an empty object namedNetworkManager
Select the one you just created NetworkManager
and add NetworkManager
the script
chooseUnityTransport
Finally, save it to complete the configuration.
3.Create Player
Create a capsule, name it Player
, and add a Network Object
component
Assets
Create a Prefabs
folder in the directory and drag the object you just created into it
Then press Delete to delete thePlayer
Click NetworkManager
and Player
drag inPlayer Prefab
Create one prefabs Lists
, name it PrefabLists
, double-click to open it and Player
drag it in
After creating it, NetworkManager
import this in
In this follow-up, you can create a role so that each Client can choose to load the role.
4. Create the ground
Open the compilation settings and add a scene
5. Create GameManager
Create a script in Scripts
the folderGameManager
using UnityEngine;
using Unity.Netcode;
public class GameManager : MonoBehaviour
{
private void Update()
{
if (Input.GetKeyDown(KeyCode.O))
{
NetworkManager.Singleton.StartHost();
}
if (Input.GetKeyDown(KeyCode.P))
{
NetworkManager.Singleton.StartClient();
}
}
}
Mount NetworkManager
below
6. Compile and run
Set up windowed operation
Then Build
click
7. Test connection
One program presses O
the key to create a host, and the other presses P
the key to join the host.
Although the two roles overlapped, it still ran successfully.
Afterword
The explanation here is very rough, and the tutorial will be updated slowly later.
Official tutorial: https://docs-multiplayer.unity3d.com/netcode/current/tutorials/get-started-ngo/