Thursday, November 10, 2016

Lidgren Networking Library + Unity

          Unity 5 supplies us with 2 different libraries integrated with the IDE. However, during the game making process you may realize that none of these are what you looking fot. The first one, the legacy API, for example, doesn't allow a client to connect to 2 different servers at same time, and that's a HUGE limitation. Your game may, for example, need to connect to a master server, plus a chat server while connected to a game server.
          The libary that comes with Unity 5 allows multiple connections, however, there's a long learning curve till you master it well enough in order to build complex networking systems.
          Another important factor is the performance. High level APIs and libraries, such as the Unity legacy networking may make use of different APIs and too many function calls in order to achieve a task. A simple message sending to another peer may require many function calls, for example, and that slows down the process a little bit.
          The most basic implementation of networking features in the .NET (and Mono) environment is the Sockets library. It is a low-level library that allows you to connect and send messages between peers, in a client-server or p2p architecture. So, the closer the library is to the Sockets library, the faster it is.

Why the .NET Socket slows down the development

 

          The library is fast and efficient; however, you'll have to code a lot more. For example: messages are handled as simple byte arrays, so you need to write functions in order to convert them to the desired type. That's just one example, there are also lots of other aspects you have to deal it, such as handling disconnections, encryption, socking bindings, etc.

What is the Lidgren Network?

 

          It is a free open-source  library built on top of ,NET Sockets. It allows you to stablish a connection between two or more peers and share messages between that. And that's ALL you need for a networked game and application.

Why should I use Lidgren Network?

 

          It comes with almost every feature you need in order to build a complete networked game or application. Since it doesn't rely on Unity, it can be used with other game engines and supports consoles and mobile. That also allows you to build your server as a console or Windows forms application, while your game is made on Unity, for example. In general, this library gives you much more freedom and basically no limits when it comes to architecture; you can create any kind of networking architecture and try new fancy ideas.

How to import Lidgren Network into your Unity Project

  1. Download the library source
  2. Open the project on Visual Studio
  3. Build it as a release
  4. Browse the project fils, and copy and Lidgren.Network.dll from the bin folder
  5. On Unity, paste it among your project files

Setting up a basic server

  1. Create a new console project on Visal Studio or MonoDevelop
  2. Add the Lidgren.Network.dll reference to your project
  3. Add the import Lidgren.Network to your code

No comments:

Post a Comment