markiodev / Networker

A simple to use TCP and UDP networking library for .NET. Compatible with Unity.
MIT License
477 stars 77 forks source link

Not working with Unity :( #43

Closed etiennepinchon closed 5 years ago

etiennepinchon commented 5 years ago

Hi,

I spent the whole day trying to make this project work with Unity but unfortunately I still cannot import the dll without errors. I tried many different things and it seems like the necessary dependencies are not included in the build. Here is the kind of errors I am getting when importing the dll of Networker:

Unable to resolve reference 'Microsoft.Extensions.Logging.Abstractions'. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspector.
Unable to resolve reference 'Microsoft.Extensions.Configuration.Abstractions'. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspector.
Unable to resolve reference 'Microsoft.Extensions.Logging'. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspector.
Unable to resolve reference 'Microsoft.Extensions.DependencyInjection.Abstractions'. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspector.
Unable to resolve reference 'Microsoft.Extensions.DependencyInjection'. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspector.
Unable to resolve reference 'Microsoft.Extensions.Options.ConfigurationExtensions'. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspector.

Any idea what I am doing wrong or how to fix it?

Thank you, Etienne

markiodev commented 5 years ago

I'll have a go at adding a project to Unity today and let you know if I get it working. It's been a while since I used Unity with Networker but need to make sure it's still OK!

markiodev commented 5 years ago

I got it working for .NET 4.* but not for standard 2.0, there's some horrible reference conflicts going on

Change your Unity project to target .NET 4.* image

• Create a new C# project which references Networker and targets .NET 4.7.2 • Set to Release • Build • Go to output and copy the .DLL files, put them into a folder in your Unity project called Plugins • Create new script for your network code in unity project which derives from MonoBehavior

etiennepinchon commented 5 years ago

That worked! Thank you very much @MarkioE

Additionally I found a way to bundle all the dlls together to makes things a bit cleaner in Unity using ILRepacker by adding the following code in the vs project:


        <ItemGroup>
            <InputAssemblies Include="$(TargetPath)" />
            <InputAssemblies Include="@(ReferencePathWithRefAssemblies)" Condition="'%(filename)' == 'Microsoft.Extensions.DependencyInjection'" />
      <InputAssemblies Include="@(ReferencePathWithRefAssemblies)" Condition="'%(filename)' == 'Microsoft.Extensions.DependencyInjection.Abstractions'" />
      <InputAssemblies Include="@(ReferencePathWithRefAssemblies)" Condition="'%(filename)' == 'Microsoft.Extensions.Logging'" />
      <InputAssemblies Include="@(ReferencePathWithRefAssemblies)" Condition="'%(filename)' == 'Microsoft.Extensions.Logging.Abstractions'" />
      <InputAssemblies Include="@(ReferencePathWithRefAssemblies)" Condition="'%(filename)' == 'Microsoft.Extensions.Logging.Configuration'" />
      <InputAssemblies Include="@(ReferencePathWithRefAssemblies)" Condition="'%(filename)' == 'Microsoft.Extensions.Configuration'" />
      <InputAssemblies Include="@(ReferencePathWithRefAssemblies)" Condition="'%(filename)' == 'Microsoft.Extensions.Configuration.Abstractions'" />
      <InputAssemblies Include="@(ReferencePathWithRefAssemblies)" Condition="'%(filename)' == 'Microsoft.Extensions.Options.ConfigurationExtensions'" />
      <InputAssemblies Include="@(ReferencePathWithRefAssemblies)" Condition="'%(filename)' == 'Microsoft.Extensions.Options'" />
      <InputAssemblies Include="@(ReferencePathWithRefAssemblies)" Condition="'%(filename)' == 'Microsoft.Extensions.Logging'" />
      <InputAssemblies Include="@(ReferencePathWithRefAssemblies)" Condition="'%(filename)' == 'Microsoft.Extensions.Primitives'" />
      <InputAssemblies Include="@(ReferencePathWithRefAssemblies)" Condition="'%(filename)' == 'Microsoft.Extensions.Configuration.Binder'" />
      <InputAssemblies Include="@(ReferencePathWithRefAssemblies)" Condition="'%(filename)' == 'System.Memory'" />
      <InputAssemblies Include="@(ReferencePathWithRefAssemblies)" Condition="'%(filename)' == 'System.Runtime.Extensions'" />
      <InputAssemblies Include="@(ReferencePathWithRefAssemblies)" Condition="'%(filename)' == 'System.Buffers'" />
      <InputAssemblies Include="@(ReferencePathWithRefAssemblies)" Condition="'%(filename)' == 'System.ComponentModel.Annotations'" />
      <InputAssemblies Include="@(ReferencePathWithRefAssemblies)" Condition="'%(filename)' == 'System.Numerics.Vectors'" />
      <InputAssemblies Include="@(ReferencePathWithRefAssemblies)" Condition="'%(filename)' == 'System.Runtime'" />
      <InputAssemblies Include="@(ReferencePathWithRefAssemblies)" Condition="'%(filename)' == 'System.Runtime.CompilerServices.Unsafe'" />

        </ItemGroup>

        <ILRepack AllowDuplicateResources="false" DebugInfo="true" Internalize="true" InputAssemblies="@(InputAssemblies)" OutputFile="$(TargetPath)" Parallel="true" TargetKind="SameAsPrimaryAssembly" />
    </Target> ```