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 MLAPI-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 MLAPI Package via URL#

See the Install MLAPI guide to install the MLAPI package.

Creating Network Manager and selecting the Transport#

In this section we will add a Network Manager and add a Transport to our project.

  1. Right click in the Hierarchy tab of the Main Unity Window.

  2. Select Create Empty.

  3. Rename the GameObject NetworkManager.

    tip

    We renamed the GameObject because:

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

    You have now created a new GameObject called NetworkManager.

  4. Select NetworkManager.

  5. Click Add Component in the Inspector tab.

  6. Select MLAPI from the list shown.

  7. Select NetworkManager component from the list displayed.

  8. Inside the NetworkManager Component tab, locate the NetworkTransport field.

  9. Click "Select Transport".

  10. Select UnetTransport.

  11. 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. Create a 3D Object->Capsule

  2. Rename it Player.

  3. Add a NetworkObject component.

  4. Click the Assets folder.

  5. Create a new Folder and call it Prefabs.

  6. Make Player a prefab by dragging it to Prefabs folder you just created.

  7. Delete Player from scene.

    tip

    We remove Player, because we will be using the network library to spawn the player. The library cannot track objects that start in the scene.

  8. Select NetworkManager.

  9. Inside the NetworkManager Component tab, locate the NetworkPrefabs field.

  10. Click + to create a slot.

  11. Drag this player prefab from above into the new empty slot.

  12. Select Default Player Prefab.

    important

    When you select the Default Player Prefab , 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 the default selected for any prefab the game will crash on client connect.

    note

    You may see the following error reported There is no NetworkPrefab Marked as a PlayerPrefab. Once you have completed the above steps you can clear the error.

  13. Create a 3D Object->Plane, centered at (0,0,0).

  14. Save your scene.

Testing the basic network building blocks#

Now we run a test to check what we have built so far.

  1. Click Play.
  2. The Editor will start, and you will just see the plane.
  3. Without stopping the editor's play mode, navigate to the NetworkManager component in the Hierarchy tab (it will be underneath DontDestroyOnLoad).
  4. Inside the NetworkManager Inspector tab. scroll down and find the Start Host button.
  5. If you click it, you will see the player capsule spawn.
  6. Stop play mode.

Creating a command line helper#

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

  1. Click the Assets folder.

  2. Create a new Folder and call it Scripts.

  3. Create a script called NetworkCommandLine.

  4. Right click on the NetworkManager in the hierarchy view and choose Create Empty.

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

  5. Rename this child GameObject NetworkCommandLine.

  6. Inside the NetworkCommandLine Inspector tab., click Add Component.

  7. Click Scripts and add the NetworkCommandLine.cs script you created earlier.

  8. Open the NetworkCommandLine.cs script.

  9. Edit the NetworkCommandLine.cs script to match the following.

tip

You can copy the script from here and paste it into your file.

  1. Select the code sample.
  2. Click Copy in the top right corner.
  3. Paste it into your code editor.
Click to show/hide the Code.
using System.Collections.Generic;
using MLAPI;
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. Back in the Editor, select File > Build Settings > Player Settings...
  2. In the Resolution and Presentation tab change Fullscreen Window mode into Windowed mode.
  3. Save your scene.
tip

If you are on a Pro Unity license, you may want to disable the 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 folder called Build.
  3. Name the binary GoldenPath.
  4. Your project will build, and it will launch, and you should see the plane.
  5. Quit your app.
  6. Launch from the command line.
For Windows you should do the following:
  1. Open CMD.
  2. Enter the following:
note

You may get a UAC prompt requesting prermission 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
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 the .exe which maybe more convenient.

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

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.