In this article series, we will be building a small networked multiplayer demo using Unity 5's Networking High-Level API, or HLAPI. Even if what we will build is simple, our example will try to cover the following key concepts which should help you build larger game projects using the HLAPI:
In Part 1, we will cover communication between the client and the server using NetworkBehaviours, SyncVars, and Commands.
In Part 2, we will implement client-side prediction and server reconciliation.
In Part 3, we will take a quick look at quality of service channels.
By keeping our project's scope as small as possible, hopefully it would be easier to extract and understand the networking principles demonstrated so that you can transfer and apply the concepts to your own games.
Basic familiarity with Unity is assumed. In particular, you should know what MonoBehaviours are, what their relationship with GameObjects is, and how/when their event functions such as Awake, Start, Update, etc. are called. No prior experience with or knowledge about Unity Networking is needed.
Our implementation is based on the architecture described by Gabriel Gambetta in his article series Fast-Paced Multiplayer. In particular, refer to Part 2 of that series to understand how we will implement client-side prediction and server reconciliation in our project.
It should be stated that this article is basically an annotated excerpt of the document Converting a Single Player Game to Multiplayer from the Unity documentation. I would recommend this document to anyone looking to get up and running with the HLAPI as quickly as possible, and is a great resource if you're looking for a checklist-style guide to using the HLAPI.
Download The Project
The project folder is available for download. Most of the project is driven by only one custom MonoBehaviour, and it is only 115 lines long with whitespace. Downloading the sample project folder is recommended so that you can go over the code and actually run the project on your system which, together with this article, will hopefully help you better understand how it all works.
Gee, this article is too long, I do not feel like reading all of this right now...
I know that some developers out there learn better by reading code directly instead of starting with an article - the project folder download is a good jump-off point for you if you are one of them.
I also plan to write a few more articles about Unity Networking after this series and release some more sample multiplayer projects, so if you are interested in those, let us stay in touch and I will let you know when those become available.