Home > Game and CG, Research > Unity 3D and RakNet 4

Unity 3D and RakNet 4

Several days ago I tried to Connect Unity 3 (Client)  and RakNet 3.731  (Server) for my research project. When I read the server log, there is some message ID that was send by Client,  that was not recognized by Server.  ID “131”, I couldn’t find it in the RakNet Library Code. Maybe something has change in the Unity’s RakNet (that’s what I thought). then I tried to find a way how to make the server and the client can use the same RakNet Library. There is away to do that, but I need Unity Pro in order to load RakNet Library that is coded in C++.

I didn’t satisfy with that way, so i tried to find other solution. yeah with unity regular I still can load C# dll. With “Swig”, I can generate C# dll from c++ project. that’s amazing ^^. So this is the way :

For Client Side (Windows):

  1. Download Unity 3, RakNet 4, Swig
  2. Generate the Swig files
    1. Open the windows terminal
    2. Go to RakNet Swig directory
      cd C:\RakNetDirectory\DependentExtensions\Swig
    3. execute swig command (I followed this turorial)
      C:\Swig\swig.exe -c++ -csharp -namespace RakNet -I”PATH_TO_RAKNETSOURCE” -I”SwigInterfaceFiles” -outdir SwigOutput\SwigCSharpOutput -o SwigOutput\CplusDLLIncludes\RakNet_wrap.cxx SwigInterfaceFiles\RakNet.i

      • “PATH_TO_RAKNETSOURCE” is the source code directory of raknet (c++ code)
      • That command will generate c# code and RakNet_wrap.cxx that will be needed in the next step
  3. Create the C# DLL. I used Microsoft Visual Studio 2008 Express Edition with Service Pack 1, CodeBlock user maybe will have little problem with “integer data type”.
    1. Create CLR empty project
    2. Import RakNet source files with “Add Existing Items”
    3. Import RakNet_wrap.cxx with “Add Existing Items”
    4. Setting the Project Properties
      • Common Properties
        Target Framework : .NET Framework 2.0
      • Configuration Properties
        • General
          Configuration Type : Dynamic Library (.dll)
          Character Set : Not Set
        • C/C++
          • General
            Additional Directory : C:\PathToRakNetSource
          • Preprocessor
            Preprocessor Definitions : WIN32;_DEBUG;_RAKNET_DLL
          • Code Generation : Multi-threaded Debug DLL (/MDd)
        • Linker
          • ws2_32.lib $(NOINHERIT)
    5. Build C# DLL
  4. Create Unity Project
  5. Copy DLL and C# files to Unity Project directory
  6. Maybe RakNetListRakNetSmartPtrRakNetSocket.cs need to be remove from project
  7. Load DLL with Unity C# Script
    This is Example how to perform connection :
    using UnityEngine;
    using System;
    using System.Collections;
    using RakNet;
    public class ConnectGUI : MonoBehaviour {
    public static string remoteIP = “”;
    RakPeerInterface myClient;
    SocketDescriptor scktDist;

    void Awake() {
    myClient = RakPeerInterface.GetInstance();
    scktDist = new SocketDescriptor();
    void OnGUI(){

    // if not yet connected
    myClient.Connect(remoteIP, 25000, “”,0);

Categories: Game and CG, Research
  1. BlinD
    July 11, 2011 at 1:05 am

    only work with unity pro !!!

    • txcom2003
      July 11, 2011 at 10:06 am

      No, it’s also work on unity free.

  2. BlinD
    July 26, 2011 at 10:32 pm

    I work on this step-by-step instruction

    with Unity free

    DllNotFoundException: RakNet
    RakNet.RakNetPINVOKE+SWIGExceptionHelper..cctor () (at Assets/Raknet/RakNetPINVOKE.cs:106)

    Same Scene work fine with 30Day Test Unity Pro.

    inside RakNetPINVOKE.cs many [DllImport] , they only work with Unity Pro

    • txcom2003
      July 27, 2011 at 7:31 pm

      Which version of RakNet you are using ? currently i’m using 4.0.
      I’m sorry, for step 5, please copy C# files only. Then after build the game (.Exe file), copy the RakNet.dll in the same directory of .Exe file.
      Good Luck.

  3. BlinD
    July 28, 2011 at 11:49 pm

    This give the same error. That will not work.
    I only wanted say in my comment, that need not try an Unity Free users .

  4. asd
    December 7, 2011 at 5:08 pm

    I am not exactly sure what doesnt work for you but as long as i tried e.g. mysql dll’s work fine with free unity.

  5. txcom2003
    December 9, 2011 at 11:43 pm

    well, i guest it’s better to make video tutorial for that. ^^

  6. April 10, 2013 at 7:58 pm

    Does this tutorial also work in Android and IOS ?

  7. Jan
    May 24, 2013 at 10:28 am

    Spent the whole day trying to get this to work. In the end I got it. Thanks!
    And yes, it does work with unity free. Two years too late but you get the DLLNotFoundException if you forget to copy the dll file into your unity project root directory.

  8. psfgh
    February 16, 2015 at 10:59 pm

    Hello I can’t get it to work with your tutorial there are many errors in c# files that I can’t fix them.
    Can you upload your c# files and raknet_wrap.h & raknet_wrap.cxx

  9. julian96
    January 5, 2016 at 1:08 am

    Hey Ther, i don’t know if you are still using this Website, but i have a Question!
    how am i able to connect to a Raknet server.exe via Unity, i hope you can answere this….

  10. July 22, 2016 at 12:00 am

    Anyone have a tutorial for Mac?

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: