Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Question How to enable c# 8?

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

  1. Crayz

    Crayz

    Joined:
    Mar 17, 2014
    Posts:
    194
    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
     
    Dinamytes likes this.
  2. Schodemeiss

    Schodemeiss

    Joined:
    Jan 4, 2018
    Posts:
    43
    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:
    28
    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:
    28
    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:
    28
    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:
    28
    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:
    212
    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.
     
    useraccount1 likes this.
  8. visose

    visose

    Joined:
    Nov 2, 2014
    Posts:
    28
    In alpha releases I guess we're the basic testing.
     
  9. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,906
    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:
    1,052
    This!

    Made by a Unity employee @xoofx

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

    Ramobo

    Joined:
    Dec 26, 2018
    Posts:
    212
    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:
    212
    Why not Library? Definitely not the root of Library either — something like `Library\GeneratedSolution`.
     
  13. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    1,052
    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:
    212
    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,906
    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:
    28
    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

    Microsoft Employee

    Joined:
    Feb 21, 2018
    Posts:
    118
    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:
    779
    How do I get C#8 happening with Rider?
     
    phobos2077 likes this.
  19. tertle

    tertle

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

    transat

    Joined:
    May 5, 2018
    Posts:
    779
    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:
    266
    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.
     
  22. transat

    transat

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

    transat

    Joined:
    May 5, 2018
    Posts:
    779
    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,447
    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:
    779
    Whoops. Cheers
     
  26. visose

    visose

    Joined:
    Nov 2, 2014
    Posts:
    28
    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. print_helloworld

    print_helloworld

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

    Deleted User

    Guest

    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 by a moderator: 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. Deleted User

    Deleted User

    Guest

  33. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,362
    how do i get this to work?
    upload_2021-2-24_10-10-1.png
     
  34. zorbaq

    zorbaq

    Joined:
    Apr 27, 2019
    Posts:
    2
    I don't think the current version of Unity's Mono supports indices. System.Index is not available in .NET Framework 4.x nor .NET Standard 2.0
     
    Last edited: May 20, 2021
  35. Buckmaster

    Buckmaster

    Joined:
    Nov 20, 2014
    Posts:
    4
    ERROR: please use language version 10.0 or greater