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
Unity uses the open-source . NET platform to ensure that applications you make with Unity can run on a wide variety of different hardware configurations. The . NET platform supports a range of languages and API libraries.
In C# 4.0, a new type is introduced that is known as a dynamic type. It is used to avoid the compile-time type checking. The compiler does not check the type of the dynamic type variable at compile time, instead of this, the compiler gets the type at the run time.
NET Framework, because before Unity 2017.1, Unity has been using a . NET 3.5 equivalent scripting runtime, missing years of updates. With the release of Unity 2017.1, Unity introduced an experimental version of its scripting runtime upgraded to a . NET 4.6, C# 6.0 compatible version.
Unity Technologies is migrating to . NET Core, a move that will bring modern . NET to Unity developers and also promises to enhance the technology for . NET developers in general.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With