Developing Multiplayer Games Using Unity with MLAPI

February 09, 2022

by Antonio, Software Developer at Wovenware

One of my favorite hobbies growing up was playing video games. The truth is that they made my youth a cheerful one, filled with excitement. So, naturally, I wondered about how they work. This is where my adventure into the world of computing started.

Video games are a great medium to tell stories. It is an interactive medium- where you can be part of the experience. Video games revolutionized humanity’s greatest skills, storytelling. If you are reading this, you too probably have a few stories you would love to share interactively or maybe you’re like me, you want to have an interactive experience with others.

The Unity Game Engine

Today, I will teach you how to create a small multiplayer experience using one of the most powerful game engines out there- the Unity game engine. It is a great engine for game making, as it comes with a slew of premade tools that you can mix and match at you’re leisure to craft your game.

A newly integrated tool is the Unity Multiplayer Networking library or MLAPI. This tool allows you to create remote procedure calls with annotations, and it also offers security that was lacking in Unity’s previous networking library.

Let’s Get Started

  • You’re going to create a new project and name it:  “MyMLAPITest”. Make sure to select the template of 3D.
Graphical user interface, application

Description automatically generated

Creating our Simple Multiplayer Experience

First, we need to create our network manager for the sample scene in which we will be building our game. Right-click on the hierarchy tab and click on “Create Empty”. Like in the screenshot below:

A screenshot of a computer

Description automatically generated with medium confidence

Now, to this empty Gameobject, we will be adding a component to it. Click on the “Gameobject” we just created.


Graphical user interface, website

Description automatically generated

Next, in the Inspector tab, click on “Add component” and then type “Network Manager”. Add that component to the empty Gameobject.


A screenshot of a computer

Description automatically generated with medium confidence

Next, we need to select a transport for our networking. Click on the “UnetTransport” in the select a transport part of the network manager. Note that you don’t need to know what transport is, just know that it is part of what MLAPI uses for networking.

A screenshot of a computer

Description automatically generated

We are now ready to create the player prefab for when players connect to our server. If you don’t know what a prefab is, it’s a collection of game objects and components that can be stored in a disk and reused in scenes. It is an abstraction that allows us to manipulate complex composite game objects.

Let’s start creating a player prefab by creating a cube as is shown below. Right-click on the scene hierarchy tab and then select “3D Object > Cube”.


A screenshot of a computer

Description automatically generated with medium confidence

Next, we are going to make the cube a network Gameobject by adding to it a component, as we did for the network manager. The difference is this time we are going to add two components: one called NetworkTransform and the other called NetworkObject

A screenshot of a computer

Description automatically generated
A screenshot of a computer

Description automatically generated with medium confidence

Now, we are going to save our prefab as a file by dragging and dropping the PlayerPrefab Gameobject into the tab below which shows us the project’s files.

A screenshot of a computer

Description automatically generated with medium confidence

Now, we add the prefab to the list of prefabs instantiated when a player joins a server. We do this by clicking on the empty game object we created to have the network manager component. Then, on the NetworkPrefabs part, we click on the “+” icon and drag the player prefab from the file system. Make sure to click on the checkbox “Default Player Prefab”.

A screenshot of a computer

Description automatically generated with medium confidence

Next, we need to download the HelloWorldManager from: https://raw.githubusercontent.com/Apo45ty/GameJam2-GenreMash/master/Assets/Scripts/HelloWorldManager.cs


Download it to the Assets folder inside our current project. Then, add this script as a component to the NetworkManager.

A screenshot of a computer

Description automatically generated with medium confidence

We then add physics to our game. We do this by adding a rigid body to our player prefab. Click on the prefab and then in the inspector tab, adding a component name “RigidBody”. 

A screenshot of a computer

Description automatically generated with medium confidence

Here, we can make a point to add material to the PlayerPrefab cube. For more details, you can visit this tutorial: https://www.c-sharpcorner.com/article/how-to-apply-materials-shaders-and-textures-to-objects-in-unity2/

Next, we add terrain upon which our players can fall into. Right-click on the Scene Hierarchy tab and click on “3D Object > Terrain”.

A screenshot of a computer

Description automatically generated with medium confidence

Now, we move our terrain below the position where our players spawn. To do this, click on the terrain object in the scene view and then look at the inspector and edit the values that appear on the transform component to the following:” X:-500, Y:-10, Z:-500″.

A screenshot of a computer

Description automatically generated with medium confidence

Move the camera so that you can see the new players fall on the terrain. To do this, we again click on the scene view, but this time we select the object named Main Camera and then move to the inspector and edit the values that appear on the transform component to the following: “X:0, Y:–10, Z:-10”.

A screenshot of a computer

Description automatically generated with medium confidence

Testing the Game

Let’s compile our game and run it. To do this, go to “File > Build Settings > Player Settings”  and in fullscreen mode, select “Windowed” and put in the desired resolution.


A screenshot of a computer

Description automatically generated with medium confidence

Build the game by clicking on “File > Build Settings” and then on “build”. Select the folder where your game will be saved and then open two instances of it.


A screenshot of a computer

Description automatically generated with medium confidence

Once the game is built, open two windows and test that every time a player connects, a new cube is created and dropped. It should look something like this :


A screenshot of a computer

Description automatically generated

Have Fun

In this multiplayer game example, we utilize an authoritative server as our network topology. This means that all clients connect to a central client(Host) who is running the logic for the game. Know that networked games may have other different topologies. Another type of topology would be peer-to-peer, usually used on Real-Time Strategy games.

In this tutorial, we did not have to code because we utilized the build-in code that MLAPI comes with. But, for the more complex games, you can apply a tool that MLAPI comes with. The tool is called remote procedure calls– a powerful way to create network-enabled software. 

MLAPI wraps around Unity libraries, allowing you to use NetworkedObjects. NetworkedObjects are the basic building block of all network-enabled game objects. In the body of networked objects, MLAPI allows you to specify remote procedure calls from server-to-single-client, server-to-all-clients, or client-to-server. This allows us to implement any kind of network behavior we wish for our game to have. 

Apart from enabling remote procedure calls, MLAPI comes with special access modifiers that allow us to put in place locks and safeguards against hacking or other player modifying an object they should not be able to. 

If you want to see more examples of games built with Unity and MLAPI, you can view a game I made in it at: https://apo45ty.itch.io/rocketsoccer. I made this for the Genre Mash mini biweekly Itch.io Gamejam. The source code for it can be found here: https://github.com/Apo45ty/GameJam2-GenreMash.

Games are the reason why I got into computers, and I can truly say I am very happy with my decision. Computer science is a very challenging and changing field and it is constantly growing. Video games are a very good way to get into computer science, as they allow you to visually see your work in action. The knowledge of networking I gain through my participation in game jams helps me better understand some of the technology stacks out there.

Sign up for our Monthly Newsletter:

Tags:

Share Share Share Share