Skip to main content

Golden Path Foundation Module

In this tutorial we will build on the work we have already done in Hello World and add in a few more features. As there are some minor differences between this tutorial and the Hello World series we will be starting afresh. You do not have to have completed either of the Hello World tutorials to complete this one, however, if you are new to Unity then we recommended that you complete them in order to familiarise yourself with Unity.

Requirements#

This tutorial requires an Netcode for GameObjects (Netcode)-supported version of Unity (2019.4+).

Create a new project in Unity#

  1. Open the Unity Hub.
  2. Click New.
  3. Select type ‘3D’
  4. Rename the project 'GoldenPath'.
note

For some of the Golden Path Training modules you may already have a project named GoldenPath for these cases we recommended you name the project GoldenPath_<modulenumber> where modulenumber> is the current Golden Path Module number.

  1. Select the location to save the project.
note

Make note of this path. You will need it later in the tutorial, when you are testing building the project from the command line. For this guide we used a Mac and our path was ~/dev/mlapi-golden-path/. On a Windows machine your path may be slightly different.

You will also need it when you create a clone of the Project for future modules.

Import Netcode Package via URL#

See the Install Netcode guide to install the Netcode package.

Creating Network Manager and selecting the Transport#

  1. Right-click in the Hierarchy tab of your Unity Project Window.
  2. Select Create Empty.
  3. Rename the GameObject NetworkManager.
    note

    We renamed the GameObject because:

    • It makes it easier to refer to later.
    • There should only be one NetworkManager object that contains the NetworkManager component. You may get unexpected results if you create more than one NetworkManager.

You have created a new GameObject called NetworkManager. Now we will add the NetworkManager component and select the transport type.

  1. Select NetworkManager.
  2. Click Add Component in the Inspector tab.
  3. Select *Netcode from the list.
  4. Select NetworkManager component.
  5. Inside the NetworkManager component tab, locate the NetworkTransport field. It may have a warning icon stating that a transport is required for Netcode to work.
  6. From the Select Transport... dropdown, select UnetTransport.
  7. Save your scene.

Creating an object to spawn for each connected player#

This section adds in a player object and spawns it for each connected player.

  1. Right-click in the Hierarchy tab of your Unity Project Window.

  2. Select 3D Object > Capsule, and rename it Player.

  3. While Player is selected, add a NetworkObject component by clicking Add Component in the Inspector tab, select Netcode, then NetworkObject.

  4. From the Projects tab, right-click the Assets folder, hover over Create, and select Folder. Name this folder Prefabs.

  5. Drag your recently created Player object to your new Prefabs folder. This makes the Player object a prefab.

  6. Right-click and delete Player from the scene.

    tip

    We remove Player, because we will be using the built-in functionality of the NetworkManager to spawn the player. Doing so will make the NetworkManager spawn a player object for each connecting client and automatically give that client ownership over their player object.

    If you do not remove Player, a player object will exist at scene load that doesn't react with the NetworkManager.

  7. Select your NetworkManager object from the Hierarchy tab.

  8. Go to the Inspector tab > NetworkManager component > NetworkPrefabs.

  9. Click + to create a slot.

  10. Drag the Player prefab from Assets > Prefabs to the new empty slot

    1. Drag the prefab also into the Player Prefab slot.
    important

    When you drop the prefab into the Player Prefab slot, you are telling the library that when a client connects to the game, automatically spawn this prefab as the character for the connecting client. If you do not have any prefab set as the Player Prefab no player object will be spawned.

  11. Finish this section by right-clicking in the Hierarchy tab, hover over 3D Object, select Plane.

  12. Save your scene.

Testing the basic network building blocks#

Now we will test to see if everything works as expected.

  1. Click Play. A DontDestroyOnLoad scene will appear on the Hierarchy tab.
  2. Select the NetworkManager object under DontDestroyOnLoad. Then click Start Host under NetworkManager component on the Inspector tab. A Player(Clone) object will appear under your sample scene on the Hierarchy tab.

Creating a command line helper#

This command line helper will launch our project outside Unity and make testing builds easier.

  1. Right-click the Assets folder and create a new folder by hovering over Create and selecting Folder. Name it Scripts.

  2. Create a script called NetworkCommandLine by right-clicking on your Scripts folder, hovering over Create and selecting C# Script.

  3. In the Hierarchy menu, right-click on the NetworkManager and choose Create Empty.

    This will create an empty GameObject with NetworkManager as its parent.

  4. Rename this child GameObject NetworkCommandLine.

  5. With the new NetworkCommandLine object selected, click Add Component from the Inspector tab.

  6. Select Scripts from the drop-down and click on the NetworkCommandLine.cs script you created earlier.

  7. Open the NetworkCommandLine.cs script by double-clicking from the Project tab > Assets > Scripts. It will open in your text editor

  8. Edit the NetworkCommandLine.cs script to match the following:

How to Copy

We recommend that you use the Copy function in our code blocks to reduce errors when copying and pasting content. Hover over the block and select the Copy button that appears in the upper-right corner of the code block.

Click to show/hide the Code.
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;
}
}
  1. Paste the copied code into your code editor.
  2. Save your changes. Your script will reload in the Unity Editor.
  3. Back in the Editor, select File > Build Settings > Player Settings.... Beneath Settings for PC, Mac, & Linux Standalone, click Resolution and Presentation to open the section options.
  4. From Resolution > Fullscreen Mode, change Fullscreen Window to Windowed.
  5. Back to the Editor main window, save your scene.
tip

If you are using a Pro Unity license, you may want to disable the splash screen by unchecking Splash Image > Splash Screen > Show Splash Screen.

Testing the command line helper#

Now we will test that the command line helper script works.

  1. Select File > Build and Run.
  2. Create a new folder called Build inside your Golden Path project folder.
  3. Save As the binary GoldenPath.
  4. Your project will build and launch in a new window, and you should see the plane.
  5. Quit your app.
  6. Now to launch from the command line.
For Windows you should do the following:
  1. Open your Command Prompt.
  2. Enter the following. Be sure to change the noted section < > of both commands to your project.
note

You may get a UAC prompt requesting permission for the binary to run you should allow it.

Server:

<Path to Project>\Build\GoldenPath.exe -mlapi server

Client:

<path to project>\Build\GoldenPath.exe -mlapi client

To run these commands on a single line:

GoldenPath\Build\GoldenPath.exe -mlapi server & GoldenPath\Build\GoldenPath.exe -mlapi client

Example:

C:\Users\sarao>GoldenPath\Build\GoldenPath.exe -mlapi server & GoldenPath\Build\GoldenPath.exe -mlapi client
important

On Windows, no standard out stream exists by default, so you will need to view the Debug.log file to see the outputs. You can find the Debug.log files in:

C:\Users\username\AppData\LocalLow\CompanyName\ProductName\Player.log

Where the CompanyName should default to DefaultCompany for a new project and ProductName should be equal to the project's name.

Alternatively you can modify the Windows commands to create a log.txt file in the same folder as your GoldenPath folder.

Modify the commands as follows:

Server:

<Path to Project>\Build\GoldenPath.exe -logfile log-server.txt -mlapi server

Client:

<Path to Project>\Build\GoldenPath.exe -logfile log-client.txt -mlapi client

Example (Running as a single command line):

C:\Users\sarao>GoldenPath\Build\GoldenPath.exe -logfile -log-server.txt -mlapi server & GoldenPath\Build\GoldenPath.exe -logfile log-client.txt -mlapi client

Next Steps with Goldenpath#

You should now save the golden path foundation project as you will be building on it in future modules.

For more information on the relevant concepts introduced here please refer to the following sections of the documentation:

Special Thanks

This guide would not have been possible without the hard work and support of Matt Walsh, Unity.