Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Help Wanted How to enable c# 8?

Discussion in '2020.2 Beta' started by Crayz, May 28, 2020.

  1. Crayz

    Crayz

    Joined:
    Mar 17, 2014
    Posts:
    152
    I just updated to 2020.2.0a12 via Hub, patch notes say compiling scripts with c# 8 is now available but my project is still on c# 7.3
     
  2. Schodemeiss

    Schodemeiss

    Joined:
    Jan 4, 2018
    Posts:
    15
    Same. I was waiting for this feature, but the default projects get set to C# 7.3 still :-(
     
  3. visose

    visose

    Joined:
    Nov 2, 2014
    Posts:
    11
    In the Visual Studio Editor package, ProjectGeneration.cs file you find the following:

    Code (CSharp):
    1.             var targetLanguageVersion = "latest"; // danger: latest is not the same absolute value depending on the VS version.
    2.  
    3.             if (m_CurrentInstallation != null && m_CurrentInstallation.SupportsCSharp8)
    4.             {
    5.                 // Current installation is compatible with C# 8.
    6.                 // But Unity has no support for C# 8 constructs so far, so tell the compiler to accept only C# 7.3 or lower.
    7.                 targetLanguageVersion = "7.3";
    8.             }
    My guess is they need to update this package and remove that if statement. I tried a few C# 8 language features and they seem to work. You can manually edit the autogenerated .csproj and change the LangVersion value to 'latest' to not get errors in Visual Studio, but it will be overwritten at some point.

    EDIT:
    A solution for now is to install the previous version 2.0.0 of the Visual Studio Editor package.
    That if statement was added only on the latest 2.0.1 version.
    https://docs.unity3d.com/Packages/com.unity.ide.visualstudio@2.0/changelog/CHANGELOG.html
     
    Last edited: May 28, 2020
  4. visose

    visose

    Joined:
    Nov 2, 2014
    Posts:
    11
    Another option if you want to stick with the 2.01 version is to cut/paste the package from /Library/PackageCache/ to /Packages/ and comment out those lines manually.
     
  5. visose

    visose

    Joined:
    Nov 2, 2014
    Posts:
    11
    And if you're brave, create a csc.rsp text file in the root of your project and add the line
    -nullable:enable
     
  6. visose

    visose

    Joined:
    Nov 2, 2014
    Posts:
    11
    It looks like
    <Nullable>enable</Nullable>
    is ignored by Visual Studio on old style .csproj files. The only way to get the warnings is to add
    #nullable enable
    to each .cs file.
    Would be nice if the VS editor package updated to the new csproj style.
     
  7. Ramobo

    Ramobo

    Joined:
    Dec 26, 2018
    Posts:
    166
    Ah, of course it appears that they didn't do basic testing.

    It would also be nice if the generated solution was placed somewhere in the Library folder so it doesn't clutter the project root. Also, NuGet support in the Package Manager, but that's another story.
     
    banan1234 likes this.
  8. visose

    visose

    Joined:
    Nov 2, 2014
    Posts:
    11
    In alpha releases I guess we're the basic testing.
     
    banan1234, phobos2077 and Ramobo like this.
  9. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,783
    Not library, but definitely not root. I use docfx to generate my documentation which needs to know where the csprojs are generated, so as long as they're in one consistent self contained location I'm happy. Definitely don't like how cluttered root gets once you start using asmdefs and external plugins a lot
     
    Crayz likes this.
  10. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    792
    This!

    Made by a Unity employee @xoofx

    https://github.com/xoofx/UnityNuGet
     
  11. Ramobo

    Ramobo

    Joined:
    Dec 26, 2018
    Posts:
    166
    Hold up. The .NET Core prototype, now this? Both were made by him, and neither got picked up officially.

    EDIT: Oh, spoke too soon. It's a limited selection, not a proper integration.
     
  12. Ramobo

    Ramobo

    Joined:
    Dec 26, 2018
    Posts:
    166
    Why not Library? Definitely not the root of Library either — something like `Library\GeneratedSolution`.
     
  13. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    792
    The reason is because not all packages target .NET Standard 2.0 or work across all platforms.

    But changing that json you could easily support any package supporting .NET Standard 2.0 really easy.

    Unfortunately, it's like this...

    In fact, it's even worse, Unity reinventing the wheel and making a mess of it:

    https://docs.unity3d.com/Packages/com.unity.nuget.newtonsoft-json@2.0/manual/index.html
     
  14. Ramobo

    Ramobo

    Joined:
    Dec 26, 2018
    Posts:
    166
    Can't you just check the dependency list? They have
    .NETStandard,Version=v2.0
    there. And it should be the user's responsibility to check whether a package supports all platforms that they're targeting.
    NuGet mirror packages... yeah. Goddammit.

    EDIT: Uh, yeah, I see I didn't quite read the FAQ.
     
  15. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,783
    That could work, disregard my straight "no" for library, it just needs to be self contained and reasonable to find. Since package cache gets its own top level folder it feels like a top level solution folder would be fine
     
    Ramobo likes this.
  16. visose

    visose

    Joined:
    Nov 2, 2014
    Posts:
    11
    I tried editing the visual studio package to change the location of the solution and project files. It seems to require simple changes to only 3 methods. I only tested it briefly but seems to be working.

    Code (CSharp):
    1.         public string ProjectFile(Assembly assembly)
    2.         {
    3.             return Path.Combine(ProjectDirectory, "Library", "VSSolution", $"{m_AssemblyNameProvider.GetAssemblyName(assembly.outputPath, assembly.name)}.csproj");
    4.          // Change Library and VSSolution to any other path.
    5.         }
    6.  
    7.         public string SolutionFile()
    8.         {
    9.             return Path.Combine(FileUtility.Normalize(ProjectDirectory), "Library", "VSSolution", $"{InvalidCharactersRegexPattern.Replace(m_ProjectName, "_")}.sln");
    10.         // Same as above.
    11.         }
    12.  
    13.         string EscapedRelativePathFor(string file)
    14.         {
    15.             var projectDir = FileUtility.Normalize(ProjectDirectory);
    16.             file = FileUtility.Normalize(file);
    17.             var path = SkipPathPrefix(file, projectDir);
    18.  
    19.             if (!Path.IsPathRooted(path)) // you need to add this
    20.                 path = Path.Combine("..\\..\\", path); // you need to add this
    21. ...
    22.  
    There's also a WriteAllText method where you could make it check/add the new folder in case it doesn't exist.
     
    Ramobo likes this.
  17. John_MSFT

    John_MSFT

    Joined:
    Feb 21, 2018
    Posts:
    48
    I filed a bug for this in the Tools for Unity and we'll investigate the fix. For future, let us know via the Help > Report a Problem menu in Visual Studio too.
     
    phobos2077 likes this.
  18. transat

    transat

    Joined:
    May 5, 2018
    Posts:
    772
    How do I get C#8 happening with Rider?
     
    phobos2077 likes this.
  19. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    2,517
    Just seems to work for me.
     
  20. transat

    transat

    Joined:
    May 5, 2018
    Posts:
    772
    Without needing to do anything?
    Tried inserting this in my code but it won't let me...
    Code (CSharp):
    1. foreach(var item in 1..5)
    2.             {
    3.                 Debug.Log(item);
    4.             }
    Doesn't recognise 1..5 as a range.
     
  21. TheZombieKiller

    TheZombieKiller

    Joined:
    Feb 8, 2013
    Posts:
    85
    You need the System.Index and System.Range types, which aren't part of .NET Standard 2.0. There is currently no official package available to add them either. You can either copy their code into your project, or use an unofficial NuGet package that provides them such as Sdcb.System.Range or IndexRange.
     
    phobos2077 and transat like this.
  22. transat

    transat

    Joined:
    May 5, 2018
    Posts:
    772
    phobos2077 likes this.
  23. transat

    transat

    Joined:
    May 5, 2018
    Posts:
    772
    Response from JetBrains..."If you open your Unity project in Rider, then in Explorer change Unity view to Solution one, right-click on project and click Properties you can select preferred version of the C# and enable Nullable reference types."
     
  24. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,437
    This wouldn't work even if it did recognize it as a Range. Ranges by themselves are not iterable, especially ranges like (1..^5) or (..)
     
    transat likes this.
  25. transat

    transat

    Joined:
    May 5, 2018
    Posts:
    772
    Whoops. Cheers
     
  26. visose

    visose

    Joined:
    Nov 2, 2014
    Posts:
    11
    transat likes this.
  27. skeletonbears

    skeletonbears

    Joined:
    Jul 13, 2018
    Posts:
    1
    hey guys, sorry for jumping in late—any idea on how I might get C#8 to work with VSCode and the 2020 alpha? all help is much appreciated!
     
  28. jmparavicini

    jmparavicini

    Joined:
    Nov 28, 2013
    Posts:
    5
    Since unity regenerates the csproj files everytime, this works until the next recompilation. My guess is, that there needs to be an option inside unity .asmdef files.
     
  29. Lelepoop

    Lelepoop

    Joined:
    Nov 14, 2016
    Posts:
    126
    You can modify the csproj and sln files after they get generated within Unity automatically as well via editor scripts
     
  30. van800

    van800

    Joined:
    Mar 10, 2018
    Posts:
    11
    For Rider 2020.2, (I am one of the devs) there should be no need to modify csproj.
    We auto-detect C# lang level by either presence of CSharp80Support folder or analyzing Roslyn in Unity installation.
    If this is not happening for you for some unknown reason - please report a bug from Rider with Help->Report a bug.
     
    Last edited: Aug 10, 2020
  31. oscarvgg

    oscarvgg

    Joined:
    Nov 21, 2013
    Posts:
    1
    To avoid Unity resetting my solution properties every time, I added a
    Directory.Build.props
    file with the following content to the root of my project


    Code (XML):
    1.  
    2.  
    3. <Project>
    4.  
    5.   <PropertyGroup>
    6.     <Nullable>enable</Nullable>
    7.     <LangVersion>8.0</LangVersion>
    8.   </PropertyGroup>
    9. </Project>
    10.  
    Hope this helps you!
     
  32. van800

    van800

    Joined:
    Mar 10, 2018
    Posts:
    11
unityunity