6

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)\Assets\Scripts\GraphQL.cs   62  Active

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.

EDIT:

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

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?

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

Programmer
  • 121,791
  • 22
  • 236
  • 328
Citrus
  • 1,162
  • 1
  • 16
  • 38
  • Are you referencing the Microsoft.CSharp.dll? see [here](https://stackoverflow.com/questions/11417047/c-sharp-dynamic-compilation-and-microsoft-csharp-dll-error) – Jonathan Tyson Aug 10 '17 at 15:44
  • No I don't since under Unity I can't quite pass around any kind of dll from visual studio, just from this plugins folder – Citrus Aug 10 '17 at 16:04
  • Also because of this error CS1703: An assembly `Microsoft.CSharp' with the same identity has already been imported. Consider removing one of the references Assets/Plugins/Microsoft.CSharp.dll (Location of the symbol related to previous error) Microsoft.CSharp.dll (Location of the symbol related to previous error) Compilation failed: 1 error(s), 0 warnings – Citrus Aug 10 '17 at 16:06
  • What's the Visual Studio version? – Programmer Aug 10 '17 at 16:11
  • visual studio 2017 community – Citrus Aug 10 '17 at 16:13
  • Ok final question. Can you check if another 4.6 feature is working? I just want verify this before adding my answer. – Programmer Aug 10 '17 at 16:13
  • like which feature, if I may ask? – Citrus Aug 10 '17 at 16:14
  • it does not work at all, unfortunately – Citrus Aug 10 '17 at 16:20

2 Answers2

12

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

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. then "String Interpolation" feature:

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

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.

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.Go to Edit --> Project Settings --> Player --> Other Settings --> Configuration --> Scripting Runtime Version --> Experimental (.Net 4.6 Equivalent).

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

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

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.Update Visual Studio. This is very important. Update the visual Studio to the latest version/patch.

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.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.

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

Programmer
  • 121,791
  • 22
  • 236
  • 328
7

I seem to have found a solution

Navigate to Edit > Project Settings > Player > Other Settings > Configuration > API Compatibility Level and change from .NET Standard 2.0 to .NET 4.x

This immediately removed the compiler error and allowed me to run code using the dynamic keyword.

Let me know if that was useful

Ben
  • 394
  • 7
  • 13