lidgren / lidgren-network-gen3

Lidgren Network Library
https://groups.google.com/forum/#!forum/lidgren-network-gen3
MIT License
1.19k stars 331 forks source link

Android/iOS support with Unity #75

Open nelisk opened 8 years ago

nelisk commented 8 years ago

So recently I came across with an annoying incompatibility: I can't use the library with Unity Engine to build an Android game.

I can't add the reference to Mono.Android because it requires a higher framework version than the mono 3.5 and unfortunately unity only support up to mono 3.5 as far I know.

Do someone know how to fix this and use Lidgren with Unity and Android/iOS?

AgentFire commented 8 years ago

So, what exactly is wrong with this library?

nelisk commented 8 years ago

To make this library work with android games made with Unity Engine, it needs to be compiled with the ANDROID symbol. When adding this symbol, the solution will "switch" to the android mode, requiring the Android namespace to access native structures (to get the IP address, I think). Now comes the problem:

To get the Android namespace I must add the Mono.Android (xamarin) reference, but this reference requires Mono 4.0 and Unity currently doesn't supports Mono 4.0, so I can't add the reference and consequently I can't build the library and can't use it.

I don't know if i'm making some mistake here, I'm not an expert, sorry.

jagt commented 8 years ago

I can confirm it's working nicely on iOS with Unity 5.3.4. Note that you have to use the UNITY compile flag. I haven't tried Android yet, will report back soon.

EDIT: I can now confirm it's working on Android w/ mono/il2cpp backends.

jmcgraw94 commented 6 years ago

Friendly necropost for the wandering soul looking for help with this. There's a lot of good information scattered about several threads online, however few include encompassing instructions. If you're attempting to use Ligdren on Unity Android mobile, here are the steps you'll need to take. My setup for this process included Unity 2017.2.0f3 Personal and Visual Studio Community 2017. My target and minimum Android API levels in Unity are Android 7.1 'Noutgat' (API level 25) and Android 5.0 'Lollipop' (API level 21) respectively. Lidgren was successfully tested on a Google Pixel XL running Android 8.1.0.

You'll need to build your own dll for Unity with a few changes having been made. Here's how you'll go through the process of building a Unity android compatible dll.

1) Download and open the latest version of Lidgren from the repo if you haven't already 2) With the solution loaded, right click on References under the Lidgren.Network project in Visual Studio's Solution Explorer. Select 'Add Reference' and click 'Browse> Browse...' 3) Navigate to Unity's managed dlls folder (C:\Program Files\Unity\Editor\Data\Managed) and add 'UnityEngine.dll'. This will make Unity's data types visible to Lidgren and should clear any undefined references in PlatformUnityExtras.cs if there were any. 4) Expand the References explorer to ensure UnityEngine is present. Click on UnityEngine and in the 'UnityEngine Reference Properties' ensure that 'Copy Local' is set to true. This will place an extra copy of the UnityEngine dll in your output folder when you build. 5) Press (Alt + Enter) or Right Click on the Lidgren.Network library in the Solution Explorer and select 'Properties' 6) Under 'Application > Target framework', select 'Unity 3.5 .net Subset Base Class Libraries' from the drop down. In targeting .NET 3.5 the dll becomes actually usable by Unity 7) Move to the 'Build' tab of the properties window and under 'General', include 'UNITY _CONSTRAINED_ ANDROID' in the Conditional compilation symbols. This will switch out which functionality gets compiled into the dll 8) Save and compile the project 9) Check your build folder (..\Lidgren.Network\bin\Debug...) for your Unity mobile ready Ligdren dll 10) Copy all the contents of the build folder into a new folder within Unity's Asset folder, you may chose to structure your directory to resemble something like (..\MyUnityProject\Assets\Plugins\Lidgren...) 11) Close and Reopen your Unity C# project in Visual Studio and look at the Solution Explorer. Make sure each of Visual Studio's projects includes Lidgren,Network as Unity should automatically have done. 12) Include Lidgren.Network in your usings and you should be good to go! Deploy a small test demo to mobile and check that it works. I hope these instructions were applicable and easy to follow.

As a note, Unity will likely produce an error and question your placement of an extra UnityEngine.dll in the Assets folder. There are ways to tell Lidgren where to find the UnityEngine.dll, however this saves the most amount of time. Clear that error and it won't show up again.

Obviously, if you're building to Android, ensure that you've already gone through the process of setting up Unity Android support. There are plenty of tutorials online if you haven't already.