在 Unity 中使用动态关键字/.NET 4.6 功能 [英] Use the dynamic keyword/.NET 4.6 feature in Unity

查看:125
本文介绍了在 Unity 中使用动态关键字/.NET 4.6 功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to implement GraphQL into Unity3D (version 2017.1.0f3 Personal). I am using .NET 4.6 (Experimental), but despite this, Unity does not support dynamic keyword. Which is odd, since .NET 4.0 it is the part of .NET. Except in Unity. I was googling for some solution how to get it work, but no solutions to the dynamic keyword. The error is this:

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)AssetsScriptsGraphQL.cs   62  Active

这是使用 GraphQL C# 客户端的唯一警告.有没有人尝试过让它工作?我还没有找到任何更大的努力来启动和运行它.

That is the only caveat of using GraphQL C# client. Has anyone tried it yet to get it work? I hadn't found any greater efforts to get it up and running yet.

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

I am using this client here: https://github.com/bkniffler/graphql-net-client

这也是 Visual Studio 的错误,但在 Unity 控制台中它也显示错误,会立即更新内容

Also this is an error from visual studio, but in Unity console it shows errors too, will update what exactly momentarily

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?

这是Unity编辑器的错误,好像和visual studio中的一样

this is the unity editor error, which seems to be the same that in visual studio

推荐答案

第一步是检查 Unity 是否识别 MS 站点.

The first step is to check if Unity recognizes these 2 basic C# 6 features from MS site.

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

1.Try "Index Initializers" feature:

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."
};

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

2. then "String Interpolation" feature:

private string FirstName = "";
private string LastName = "";
public string FullName => $"{FirstName} {LastName}";

如果他们给您错误,那么问题只是 dynamic 关键字,而是 Visual Studio 无法识别 .NET 版本的问题由 Unity 设置.

If they give you error then the problem is not just the dynamic keyword but a problem that Visual Studio cannot recognize the .NET version being set by Unity.

从评论部分,您的 Unity 未能编译第一个示例.

From the comment section your Unity failed to compile the first example.

逐一执行步骤以寻找可能的修复方法.不要不要跳过它们.

Go through the steps one by one for a possible fix. Do not skip of them.

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

1.Go to Edit --> Project Settings --> Player --> Other Settings --> Configuration --> Scripting Runtime Version --> Experimental (.Net 4.6 Equivalent).

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

2.Go to Edit --> Project Settings --> Player --> Other Settings --> Configuration --> Api Compatibility Level --> .NET 4.6

3.重新启动 Unity 编辑器和 Visual Studio.您必须重新启动两者.

3.Restart Unity Editor and Visual Studio. You must restart both.

测试上述两个 C# 功能.如果它们起作用,那么 dynamic 关键字也应该起作用.如果他们不这样做,则转到#4.

Test both C# features above. If they work then the dynamic keyword should as-well. If they don't then move on to #4.

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

4.Update Visual Studio. This is very important. Update the visual Studio to the latest version/patch.

5.如果您仍然无法编译上述两个 C#6 功能,请重新安装 Visual Studio 和 Unity,然后执行步骤 #1#2 再次,因为一些文件丢失了.

5.If you can't still get both C#6 features above to compile then Re-install both Visual Studio and Unity then perform step #1 and #2 again as some files are missing.

6.最后,如果您的两个 C#6 功能都可以使用,但 dynamic 关键字仍然无法使用,则从 Unity 2017.1 更新到 Unity 2017.2.此版本修复了许多 .NET 问题.

6.Finally, if you get both C#6 features working but the dynamic keyword is still not working then update from Unity 2017.1 to Unity 2017.2. This version fixed many .NET issues.

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

Note that I am using Unity 2017.2 with the dynamic keyword without any issue. Also, GraphQL is working fine.

这篇关于在 Unity 中使用动态关键字/.NET 4.6 功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆