简体   繁体   中英

Building a UWP App in Unity using TcpClient

I am trying to build a UWP app in unity using a C# plugin that utilizes TcpClient. The plugin uses System.Net.Sockets. When I build a standalone executable, the app runs perfectly on my local machine. However, when I try to build it for the Universal 10 platform, it does not work. This is important, because my aim is to deploy the app on the Hololens.

I get error CS0246 in the console:

The type or namespace TcpClient could not be found (are you missing a using directive or an assembly reference?)

I've come across people who have run into similar problems getting TcpClient to work with Unity. I tried installing the pre-release System.Net.Sockets 4.1 package per this solution: https://github.com/dotnet/corefx/issues/5939

I opened the C# project in Visual Studio and I successfully installed version 4.1. However, I was met with the same error when I tried building again. Here is my packages.config file:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="System.IO" version="4.0.0" targetFramework="net461" />
  <package id="System.Net.Primitives" version="4.0.10"  targetFramework="net461" />
  <package id="System.Net.Sockets" version="4.1.0-beta-23516" targetFramework="net461" />
  <package id="System.Runtime" version="4.0.0" targetFramework="net461" />
  <package id="System.Threading.Tasks" version="4.0.0" targetFramework="net461" />
</packages>

I've also come across this solution, which uses StreamSocket from the Windows.Networking.Sockets package instead: How to connect to Unity game server socket from UWP app via sockets?

I've considered just rewriting all of the code to work with StreamSocket. However, this would be difficult and error-prone since I did not write the original code.

The code I am dealing with is the data-streaming script included with Brekel Pro Face 2. I would post some snippets from the code, but I am not sure if Brekel would be ok with that.

Any ideas?

The Problem

Unity targets Mono, which requires the plugin to be a .NET platform DLL. However, Hololens supports Windows Universal applications, which requires the plugin to be a Universal Window platform (UWP) DLL. Therefore, it is impossible to use the same plugin for both Unity and Hololens. If it works for Unity, building for Hololens will fail.

The solution

The solution is to use two versions of the plugin; a .NET platform DLL and a UWP DLL. Both DLLs should have identical interfaces in order to allow for referencing classes, methods, etc from any of the two DLLs using the same code.

Solution Steps

Assuming that you have created the two DLL files (x.dll) and (x.uwp.dll) with identical interfaces (ie namespace, public class names, etc), the solution should be as easy as the following:

  1. Create a Plugins folder under the Assets folder in your Unity project.
  2. Drag and drop the (x.dll) file into the Plugins folder and select it.
  3. From the Inspector window under Select platforms for plugin, make sure that only Editor' is checked.
  4. Create a subfolder named WSA under the Plugins folder.
  5. Drag and drop the (x.uwp.dll) file into the WSA folder and select it.
  6. From the Inspector window under Select platforms for plugin, make sure that only WSAPlayer is checked.

Now, Unity will use x.dll when you run the project on your machine and use x.uwp.dll when you build your project for Hololens.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM