Unity's NetCode multiplayer online game online battle tutorial (2)--Simple online implementation


1. Add basic components

  • NetworkManager
  • Player
  • Scene

2. Create NetworkManager component

Create an empty object namedNetworkManager

Select the one you just created NetworkManagerand add NetworkManagerthe script


chooseUnityTransport


Finally, save it to complete the configuration.


3.Create Player

Create a capsule, name it Player, and add a Network Objectcomponent

AssetsCreate a Prefabsfolder in the directory and drag the object you just created into it

Then press Delete to delete thePlayer

Click NetworkManagerand Playerdrag inPlayer Prefab

Create one prefabs Lists, name it PrefabLists, double-click to open it and Playerdrag it in


After creating it, NetworkManagerimport 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 Scriptsthe 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 NetworkManagerbelow


6. Compile and run

Set up windowed operation

Then Buildclick


7. Test connection

One program presses Othe key to create a host, and the other presses Pthe 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/

Guess you like

Origin blog.csdn.net/a924282761/article/details/132910734