【发布时间】:2018-07-01 13:25:26
【问题描述】:
我目前正在尝试将 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 程序集,其中在其引用中显示了 netstandard 的 v2.0。当我将库的目标降低到 .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 中的错误。我也加入了screenshot of the error。
【问题讨论】:
-
您能发布产生该错误的代码吗?
-
@Draco18s 显示代码不会有太大帮助。我所做的只是从 .NET Standard 2.0 库中实例化一个类,一个简单的“Foo f = new Foo()”。它在 Visual Studio 中也完全可以编译,只是 Unity 拒绝玩得很好。
-
是的,但如果我知道 what
Foo你正在调用new,我可以调查该对象类型的情况。 -
Fixed link。 @Kay URL 必须在 () 中,所以它是
[link text](url) -
@AllenRufolo 恐怕不会。 Unity 似乎很想杀死 .NET 脚本后端,所以我只是承认失败并开始研究 IL2CPP。不幸的是,这个项目被搁置了,所以我在那里也没有取得太大进展。
标签: c# unity3d .net-standard hololens