Unity3d - How 2 Network - My way to do it.

Messages
901
Reaction score
2,532
Points
790
Location
Netherlands
IF YOU EXPECT A NETWORKING TUTORIAL, GO BACK, THIS IS ONLY EXPLAINING HOW TO DO IT.

(Really tho, if I ever make up my mind I might post the actual framework code for anybody to use.)

So basically, today I've been working on getting Unity3D to use a different networking engine, because as anyone that ever used Unity3d will be able to tell you, the default SUCKS.

So, today I intergrated Lidgren into Unity3D. Here is how I did it!

So first things first, you're going to need Lidgren Network, I did not feel like compiling it myself, so I downloaded it. Here's my version.

So, all ya need to do to use this in MonoDevelop is create a new folder in your Assets folder, "Plugins", and just import the DLL into that folder.

Now you can just do
Code:
using Lidgren.Network
in any of your files!

So, now you've got Lidgren, and you'll notice a small problem... Lidgren does not support any kind of RPC... just UDP messages... well... shit, right? Nope!

Its really easy to create your own RPC system, like I did.

Here's how it works!

My system uses something called Net Jobs, this is basically a task the server gets to execute on its side, and then broadcast the result to other clients, kind of the net library in Garry's Mod.

So what does a Net Job look like? Well, here is a NetJob from my code:
Code:
public class SendPlayerInfo : NetJob {
   public string Identifier = "sendplayerinfo";
   public NetJobPriority Priority = NetJobPriority.PRIORITY_HIGH;
   public NetJobType Type = NetJobType.SendToServer;
  
   public virtual void OnSend(NetOutgoingMessage msg)
   {
     // Send User Name
     msg.Write (PlayerPrefs.GetString ("userName"));
     // Send Steam ID
     msg.Write (PlayerPrefs.GetString ("userSteam"));
     // Thats all!
   }
  
   public virtual void OnReceive(NetIncomingMessage msg)
   {
     // Do something...
   }
}

So lets go over these variables, shall we.

First comes the identifier, this is what the server/client sends first, this will be used to grab the NetJob from the stack of jobs, which is a simple Dictionary<string, NetJob>.

Second comes the priority, which is slightly more interesting, as it determines how the network job is executed, basically what high priority means is that it should be executed as soon as possible and before anything else is done, this I achieved by doing this in the FixedUpdate() void that Unity offers, the good thing about this is that you are not dependent on the framerate, it runs a fixed 60 times per second.

All the other net jobs, are smaller priority and are not too important, so they are thrown in with the normal update, which executes once per frame.

Third you can see the Type, this is used to determine where to send it... which is strange... because I made server and client seperate projects, but hey, if I ever decide to merge this will solve me loads of work seperating the Jobs.

And then you can see your normal handling!

Hope this can help you somewhat if you ever find yourself in need to design your own networking solution for a game engine!

P.S. here it is in action, a simple chat, thanks to AyJay for testing.
5AxbiDs.png
 
Top