Basic Unity Multiplayer Game With Networker – Part 1

Welcome to the first ever tutorial for Networker. I first started developing Networker because I wanted a simple to user networking library which supported my favourite development features such as Dependency Injection and .NET Core.

Before we get started I would like to make an important point. Networker is a general use networking library. This means it will not magically do things for your game like a standard game networking library might, such as player interpolation or object management. These concepts will need to be implemented yourself.

What you will learn

Part 1 – Create a server with Networker
Part 2 – Adding configuration and logging
Part 3 – Create a client with Networker
Part 4 – Sending packets
Part 5 – Add your client to a Unity project
Part 6 – Basic database access

What you will need

Visual Studio 2017+
Patience
Basic C# knowledge
Basic NuGet knowledge

Creating your server

Great – Let’s get started!

First you’ll need to open up Visual Studio and create a new project which should be a .NET Core Console Application. Call it Tutorial.Server.

Right click on your newly created project and edit the file. Add in the following line to pull in the latest Networker NuGet package.

<PackageReference Include="Networker" Version="3.0.0" />

We also need to make a choice at this point. Which packet formatter are we going to use? Right now there are two choices. ZeroFormatter or protobuf-net

For this tutorial we are going to use the protobuf-net formatter, so we need to add in this NuGet package too.

<PackageReference Include="Networker.Extensions.ProtobufNet" Version="3.0.0" />

Your project file should now look something like this

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Networker" Version="3.0.0" />
    <PackageReference Include="Networker.Extensions.ProtobufNet" Version="3.0.0" />
  </ItemGroup>

</Project>

Let’s code!

Now that our project is setup correctly we can start adding our server code. We are going to use the ServerBuilder class to help us create our server instance.

For our example we are going to use both TCP and UDP which are supported by Networker.

static void Main(string[] args)
{
    var server = new ServerBuilder()
        .UseTcp(1000)
        .UseUdp(1001)
        .UseProtobufNet()
        .Build();

    server.Start();

    while (server.Information.IsRunning)
    {
        Thread.Sleep(10000);
    }
}

There you have it, you’ve created your server instance and your application is now listening for TCP connections on port 1000 and UDP is listening on port 1001.

In the next part we will look at adding logging and using config files.

Leave a comment

Your email address will not be published. Required fields are marked *