在Unity中使用dynamic keyword/.NET 4.6功能

Cit*_*rus 6 c# database unity-game-engine

我正在尝试将GraphQL实现到Unity3D(版本2017.1.0f3 Personal).我使用的是.NET 4.6(实验版),但尽管如此,Unity还是不支持动态关键字.这很奇怪,因为.NET 4.0是.NET的一部分.在Unity中除外.我正在谷歌搜索一些解决方案如何让它工作,但没有动态关键字的解决方案.错误是这样的:

Severity    Code    Description Project File    Line    Suppression State
Error   CS1980  Cannot define a class or member that utilizes 'dynamic' 
because the compiler required type 
'System.Runtime.CompilerServices.DynamicAttribute' cannot be found. Are you 
missing a reference?    skiing-prototype (1)    D:\skiing-prototype 
(1)\Assets\Scripts\GraphQL.cs   62  Active
Run Code Online (Sandbox Code Playgroud)

这是使用GraphQL C#客户端的唯一警告.还有人试过它才能让它发挥作用吗?我还没有找到任何更大的努力来让它运行起来.

编辑:

我在这里使用这个客户端:https://github.com/bkniffler/graphql-net-client

这也是visual studio的一个错误,但在Unity控制台中它也会显示错误,会暂时更新

Assets/Scripts/GraphQL.cs(80,16): error CS1980: Dynamic keyword requires 
`System.Runtime.CompilerServices.DynamicAttribute' to be defined. Are you 
missing System.Core.dll assembly reference?
Run Code Online (Sandbox Code Playgroud)

这是统一编辑器错误,这似乎与visual studio中的错误相同

Pro*_*mer 10

第一步是检查Unity是否从MS站点识别这两个基本的C#6功能.

1.尝试"索引初始化器"功能:

private Dictionary<int, string> webErrors = new Dictionary<int, string>
{
    [404] = "Page not Found",
    [302] = "Page moved, but left a forwarding address.",
    [500] = "The web server can't come out to play today."
};
Run Code Online (Sandbox Code Playgroud)

2.然后"字符串插值"功能:

private string FirstName = "";
private string LastName = "";
public string FullName => $"{FirstName} {LastName}";
Run Code Online (Sandbox Code Playgroud)

如果他们给你那么错误的问题是不是只是dynamic关键字但Visual Studio中无法识别有问题的.NET版本被统一设置.

从评论部分,Unity无法编译第一个示例.


逐个完成这些步骤以获得可能的修复.千万不能忽略他们.

1.转到编辑 - >项目设置 - >播放器 - >其他设置 - >配置 - >脚本运行时版本 - >实验(.Net 4.6等效).

2.转到编辑 - >项目设置 - >播放器 - >其他设置 - >配置 - > Api兼容级别 - > .NET 4.6

3 .Restart统一编辑器和Visual Studio.你必须重启两者.

测试上面的两个C#功能.如果他们工作,那么dynamic关键字也应该如此.如果他们不这样做,那么继续前进#4.

4.更新Visual Studio.这是非常重要的.将Visual Studio更新到最新版本/补丁.

5.如果您仍然无法获得上述两个C#6功能,则重新安装Visual Studio和Unity,然后再次执行步骤#1#2,因为缺少某些文件.

6.最后,如果您同时使用C#6功能但动态关键字仍无效,则从Unity 2017.1更新到Unity 2017.2.此版本修复了许多.NET问题.

请注意,我使用Unity 2017.2dynamic关键字没有任何问题.此外,GraphQL工作正常.


Ben*_*Ben 7

我似乎找到了解决办法

导航至Edit > Project Settings > Player > Other Settings > Configuration > API Compatibility Level并更改.NET Standard 2.0.NET 4.x

这立即消除了编译器错误,并允许我使用动态关键字运行代码。

让我知道这是否有用