Search Unity

C# 9 Support for Unity

Discussion in 'General Discussion' started by harrywyt428, Nov 11, 2020.

  1. harrywyt428

    harrywyt428

    Joined:
    Jan 30, 2020
    Posts:
    8
    C# 9 has just been released alongside with .NET 5. Will the support for C# 9 rolled out to Unity soon?
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    1,939
    Probably, but...

    ...Not likely.
     
    UlfhedinNord and wlwl2 like this.
  3. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    17,309
    Unity is typically at least one year behind the release of a new specification. C# 8 is only starting to become available with Unity 2020.2.
     
  4. MDADigital

    MDADigital

    Joined:
    Apr 18, 2020
    Posts:
    2,198
    I rather see them move to. NET 5 all together. Mono is a mess. . Net 5 is faster than il2cpp in many cases even
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,723
    Is .Net 5 available for all of Unity's supported platforms?
     
  6. MDADigital

    MDADigital

    Joined:
    Apr 18, 2020
    Posts:
    2,198
    They can keep the mono runtime for crap platforms ;)
     
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,723
    My guess is they haven't moved off Mono already specifically to avoid the mess of supporting both though.
     
    MadeFromPolygons likes this.
  8. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    957
    There is a whole thread about that here
     
    Joe-Censored likes this.
  9. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    5,658
    We do plan to support C# 9, although we don't have any ETA for it yet.
     
    veasily, User340, Tanner555 and 7 others like this.
  10. MDADigital

    MDADigital

    Joined:
    Apr 18, 2020
    Posts:
    2,198
    Using .NET 5 or Mono is the real question :D
     
  11. harrywyt428

    harrywyt428

    Joined:
    Jan 30, 2020
    Posts:
    8
    Yeah... Mono is a mess. I wonder would there be a massive API change if we migrate from Mono to .NET 5.
     
  12. MDADigital

    MDADigital

    Joined:
    Apr 18, 2020
    Posts:
    2,198
    NET 5 does not support app domains which unity uses to reload when you change code.
     
    Joe-Censored likes this.
  13. harrywyt428

    harrywyt428

    Joined:
    Jan 30, 2020
    Posts:
    8
    Is it a Mono-specific feature..?
     
    HoL0DoK_ likes this.
  14. MDADigital

    MDADigital

    Joined:
    Apr 18, 2020
    Posts:
    2,198
    No full framework
     
  15. harrywyt428

    harrywyt428

    Joined:
    Jan 30, 2020
    Posts:
    8
    Or if it is not available in .NET 5, are there any alternatives for Unity to recompile/reload on code changed?
     
  16. OndrejP

    OndrejP

    Joined:
    Jul 19, 2017
    Posts:
    172
    .NET Core has collectible assemblies and it seems that .NET 5 has it too:
    https://docs.microsoft.com/en-us/do...time_Loader_AssemblyLoadContext_IsCollectible

    Collectible assemblies could theoretically improve "Domain reload situation".
    Instead of tearing down whole AppDomain and recreating it, it could be possible to just collect Script assemblies and load newer version.

    There's a lot of potential problems. First, all references to Script Assemblies and any types and their instances would have to be collected. So Unity would have to destroy and recreate objects (like it's doing now). Some objects might stay alive (like Textures...). Also all references between collected and non-collected assemblies would have to be cleaned up (e.g. subscribed events, objects stored in static fields...).

    In worst case, it should be possible to make it same as now (collect all assemblies including UnityEngine, UnityEditor, ... and reload them).

    I wonder what is being considered for Unity future, whether Mono is set in stone or whether they're considering .NET Core or .NET 5.

    EDIT:
    It seems AppDomain removal and AssemblyLoadContext are coming to Mono as well:
    https://www.mono-project.com/news/2020/08/24/native-loader-net5/
     
    Last edited: Dec 28, 2020
  17. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    5,658
    Yes, this is being considered for the future. We've done some prototyping with CoreCLR and assembly load context as a a replacement for App Domain reloading. It is a breaking change for many projects, but it is likely something that we can work though. We don't have any specific Unity release information to share about this yet though.
     
  18. kurroarashi

    kurroarashi

    Joined:
    Dec 22, 2018
    Posts:
    21
    Not gonna lie, I really need that return type covariance.
     
    MilenaRocha likes this.
  19. OndrejP

    OndrejP

    Joined:
    Jul 19, 2017
    Posts:
    172
    It will make ICloneable and similar use cases so much better :)
     
  20. natteo

    natteo

    Joined:
    Nov 10, 2013
    Posts:
    2
    Any chance (if it is even at all possible) to just get that C# 9 pattern matching into the current C# 8 inside Unity? Or will I just have to wait till C# 9 support comes along?
     
  21. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    5,658
    No, I don't expect us to back port a specific C# 9 feature. I do expect we will move to C# 9, but I don't have an ETA for that.
     
    veasily and Joe-Censored like this.
  22. MDADigital

    MDADigital

    Joined:
    Apr 18, 2020
    Posts:
    2,198
    Please consider NET 5 CLR at the same time, the Mono CLR performance is way behind
     
    Tanner555 likes this.
  23. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    5,658
    Indeed, the use of CoreCLR as a JIT runtime instead of Mono is something that we have prototyped. Again, I have no information about an ETA or release though.
     
    MMXXXVIII, veasily, Tanner555 and 6 others like this.
  24. timothy-eckstein

    timothy-eckstein

    Joined:
    Mar 9, 2013
    Posts:
    1
    I love having the features we do have. Thank you for all your hard work! I'm aware of the pain it is to convert a small project from Framework to Core, let alone a much larger project from Mono to Core at a low-level. So again, thank you all so much for even considering it.
     
    Ryiah, angrypenguin and hasugg like this.
  25. Kontolion

    Kontolion

    Joined:
    Feb 16, 2021
    Posts:
    1
    if c# 9 / .net 5 comes to unity, i think i'm curious the use of record type in unity, along with any other key features in there
     
    Ziplock9000 likes this.
  26. Ziplock9000

    Ziplock9000

    Joined:
    Jan 26, 2016
    Posts:
    278
    .NET 6 Preview 1 is out
     
    Oktawian likes this.
  27. NoDumbQuestion

    NoDumbQuestion

    Joined:
    Nov 10, 2017
    Posts:
    186
    Ryiah likes this.
  28. stevphie123

    stevphie123

    Joined:
    Mar 24, 2021
    Posts:
    5
    Hi, when c# 9 pattern matching would be added?

    Please consider it to at least we beta test it in 2021(non-lt) version
     
  29. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,723
    Did you read the rest of the thread? They aren't backporting individual C# 9 features. With 2021.2 alpha already available to download, it is way too late to request any major changes like this for the 2021 cycle.

     
  30. Bonkurazu

    Bonkurazu

    Joined:
    Oct 13, 2013
    Posts:
    5
    So the thing is, C# 9 pattern matching looks like it's there?
    What's new in Unity 2021.2.0 Alpha 11 - Unity (unity3d.com)
    2021.2.0a11 changelog says they have langversion 9 and a subset of features, including "Pattern matching enhancements." I don't know if that's what you wanted.
     
    Ribbanya likes this.
  31. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,723
     
  32. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    1,791
    There is a Visual Studio issue (using VS community 2019 - best free one?) around this in that by default we seem to get suggestions to apply C# 9 formatting shortcuts like the new.. new(); support (Vector3 vec = new(x, y, z) ) which do not compile in 2019 LTS
     
  33. oscarAbraham

    oscarAbraham

    Joined:
    Jan 7, 2013
    Posts:
    99
    It happened to me after updating both Visual Studio and the Visual Studio Editor extension from UPM. It was fixed after going to Edit → Preferences... → External Tools, and then clicking the "Regenerate project files" button.
     
    Mazyod and acceptable_simulacrum like this.
  34. Xenerade

    Xenerade

    Joined:
    Dec 10, 2017
    Posts:
    228
    Any updates?
     
  35. GliderGuy

    GliderGuy

    Joined:
    Dec 14, 2018
    Posts:
    246
    I recommend you check out this thread. ;)
     
    Xenerade likes this.
unityunity