The purpose of this tutorial is to test the Netcode packet and any errors that may occur.
This first tutorial only covers the functionality of the packet without implementations of sending data between client and server.
I used the newest version of Unity Hub version 3.3.0 and Unity version 2021.3.9f1.
here and here.
Here are the steps taken in brief:
I created a folder for my projects called Unity3DProjects and I set it for each new project.
I created a new project using Unity Hub called Netcode001.
After creation, the Unity 3D editor will open and if there are problems with the license, then you will have to download the license from your Unity 3D account or obtain/activate a new one.
I use a free Personal Edition license renewed and reactivated on Wed, Dec 20, 2017, my old license when I started working with Unity was around 2013.
These things are important for the learning part because the development of the Unity 3D game engine had substantial changes and a lot of bugs. That’s why not all tutorials cover all the issues needed to develop a game and some elements are still in the development process.
Another element you have to take care of is the interaction with the Visual Studio Community, I received errors when changing the version from 2019 to the new version 2022. The generation of build errors sent me to a new empty folder and I fixed it by reopening the project again without Visual Studio, obviously after it has already been set in the Unity 3D editor.
The setting is made from the File – Preferences menu
The installation of the Netcode package is done from the main menu: Window – Package Manager:
Click the plus sign Add in the Package Manager status bar and select Add package by name… and fill with:
1 | com.unity.netcode.gameobjects |
Wait for the package to be installed.
Using the right-click in the Hierarchy tab of the main Unity Window.
Select Create Empty and rename the GameObject with NetworkManager.
With this Unity game object, use click to Add Component in the Inspector Tab.
Search and select Netcode from the list shown.
Select NetworkManager Component from the list displayed and inside the NetworkManager component tab, locate the NetworkTransport field, and add these two scripts.
You need to check if the Select Transport is on the Select UnityTransport.
You can see also my scene has some new objects like a plane and a new game object named NetworkCommandLine.
You can add these two game objects, the plane at the root of the hierarchy and the NetworkCommandLine at the NetworkManager game object.
On the NetworkCommandLine game object use Add Component in the Inspector Tab to add a new C# script named NetworkCommandLine, see the source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | using System.Collections.Generic; using Unity.Netcode; using UnityEngine; public class NetworkCommandLine: MonoBehaviour { private NetworkManager netManager; void Start() { netManager = GetComponentInParent<NetworkManager>(); if (Application.isEditor) return; var args = GetCommandlineArgs(); if (args.TryGetValue("-mlapi", out string mlapiValue)) { switch (mlapiValue) { case "server": netManager.StartServer(); break; case "host": netManager.StartHost(); break; case "client": netManager.StartClient(); break; } } } private Dictionary<string, string> GetCommandlineArgs() { Dictionary<string, string> argDictionary = new Dictionary<string, string>(); var args = System.Environment.GetCommandLineArgs(); for (int i = 0; i < args.Length; ++i) { var arg = args[i].ToLower(); if (arg.StartsWith("-")) { var value = i < args.Length - 1 ? args[i + 1].ToLower() : null; value = (value?.StartsWith("-") ?? false) ? null : value; argDictionary.Add(arg, value); } } return argDictionary; } } |
Save this scene with a name into your folder project.
Use the main menu File – Build Settings… to build into executable into your project folder.
According to the second tutorial, you can test like a client or server with or not logging process.
1 2 3 | Netcode001.exe -mlapi server Netcode001.exe -mlapi client Netcode001.exe -mlapi server & Netcode001.exe -mlapi client |
If you use a text editor you can see all logging processes and steps.
You can see I have an old computer …
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | Direct3D: Version: Direct3D 11.0 [level 11.0] Renderer: NVIDIA GeForce GTS 450 (ID=0xdc4) Vendor: NVIDIA VRAM: 468 MB Driver: 23.21.13.8813 Begin MonoManager ReloadAssembly - Completed reload, in 0.404 seconds <RI> Initializing input. <RI> Input initialized. <RI> Initialized touch support. Direct3D: detected that using refresh rate causes time drift. Will stop trusting refresh rate until the game window is moved. Direct3D: detected that vsync is broken (it does not limit frame rate properly). Delta time will now be calculated using cpu-side time stampling until the game window is moved. UnloadTime: 1.248400 ms Setting up 1 worker threads for Enlighten. Memory Statistics: [ALLOC_TEMP_TLS] TLS Allocator StackAllocators : [ALLOC_TEMP_MAIN] Peak usage frame count: [1.0 KB-2.0 KB]: 31 frames, [2.0 KB-4.0 KB]: 167 frames, [2.0 MB-4.0 MB]: 1 frames Initial Block Size 4.0 MB Current Block Size 4.0 MB Peak Allocated Bytes 2.1 MB Overflow Count 0 [ALLOC_TEMP_Loading.AsyncRead] Initial Block Size 64.0 KB Current Block Size 64.0 KB Peak Allocated Bytes 207 B Overflow Count 0 [ALLOC_TEMP_Loading.PreloadManager] Initial Block Size 256.0 KB Current Block Size 256.0 KB Peak Allocated Bytes 27.2 KB Overflow Count 4 [ALLOC_TEMP_Background Job.Worker 8] Initial Block Size 32.0 KB Current Block Size 32.0 KB Peak Allocated Bytes 54 B Overflow Count 0 ... |