首页 > 解决方案 > Unity 无法识别 .NET Standard 2.0 参考

问题描述

我目前正在尝试将 Hololens 与 Unity 结合使用。实验的一部分是与现有的 .NET Framework 应用程序交换数据。这意味着我有许多由两个应用程序共享的库,我已将它们重写为 .NET Standard 2.0 库,以便它们与这两个应用程序兼容。

但是,当我开始在 Unity 脚本中引用 .NET Standard 2.0 库时,Unity 会出现以下错误:

错误 CS0012:“对象”类型是在未引用的程序集中定义的。您必须添加对程序集“netstandard,Version=2.0.0.0,Culture=neutral”的引用。

我很难弄清楚是什么原因造成的。当我检查 Edit -> Project Settings -> Player 时,我将 Scripting Runtime Version 设置为 .NET 4.x Equivalent;.NET 的脚本后端;和 .NET Standard 2.0 的 Api 兼容性级别。当我双击该错误时,它会在 Visual Studio 中打开 Assembly-CSharp 程序集,其中在其引用中显示了 v2.0 的netstandard 。当我将库的目标降低到 .NET Standard 1.4 时,我不再有错误,但不幸的是我必须使用 2.0。

为了让 Unity 识别引用了netstandard v2.0,我需要设置什么?

我使用 Unity 2018.1.6f。我之前在Unity Answers上问过我的问题,但那里没有看到任何回复。

谢谢阅读。

编辑:根据请求,这里是 Unity 脚本中的代码:

using AugmentedReality.Device;

public class ServiceProvider
{
   #region Singleton
   private static ServiceProvider _instance;
   public static ServiceProvider Instance
   {
      get
      {
         if(_instance == null)
            _instance = new ServiceProvider();
         return _instance;
      }
   }
   #endregion

   public IOwnShipService OwnShipService;

   private ServiceProvider()
   {
      OwnShipService = new OwnShipService();
   }
}

以及 .NET Standard 2.0 库中的 OwnShipService:

namespace AugmentedReality.Device
{
   public interface IOwnShipService
   { }

   public class OwnShipService : IOwnShipService
   {
      public OwnShipService()
      { }
   }
}

这足以导致错误。我不断减少调用的代码量,看看是什么原因造成的。只需从库中实例化一个空类就足以触发 Unity 中的错误。我也包含了错误的屏幕截图

标签: c#unity3d.net-standardhololens

解决方案


尝试使用 IL2CPP 脚本后端。这有一个更大的 .NET 表面,然后 .NET 脚本后端和 .NET 脚本后端无论如何都被弃用了。


推荐阅读