Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Unity Future .NET Development Status

Discussion in 'Experimental Scripting Previews' started by JoshPeterson, Apr 13, 2021.

  1. CaseyHofland

    CaseyHofland

    Joined:
    Mar 18, 2016
    Posts:
    548
    Not really? Even the official guidelines state:
    “You can take our conventions as-is, or modify them to suit your team's needs.”

    https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/coding-conventions

    The page is even called common C# code conventions to highlight that different conventions are just… more rare.

    That’s not to say that the Unity style can’t still be terrible, but it needs to be for a different reason than “doesn’t follow the common conventions”.
     
    Saniell, TangerineDev and cxode like this.
  2. stonstad

    stonstad

    Joined:
    Jan 19, 2018
    Posts:
    599
    Hey @JoshPeterson

    Xbox has certification requirements for data transmission. I'm learning that the Unity Mono .NET Runtime System.Net namespace (HttpClient/WebSockets) is disallowed. This is because it uses WinSock and not WinHTTP or XCurl. (public reference: https://learn.microsoft.com/en-us/gaming/gdk/_content/gc/policies/console/certification-requirements XR-134).

    Looking to the future, do know which OS network stack the new Unity CoreCLR .NET runtime will use on Windows and Windows console platforms? Would this be the same networking stack as .NET Standard 2.1 Unity or a different one?
     
  3. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,771
    Good question! We're expecting to use the same networking stack as normal .NET for all Windows-ish platforms. Honestly I've not dug into the details too much on this yet, but I expect that it would work with Xbox certification.

    Note that this will be different from the current Unity stack used with .NET Standard 2.1 support.
     
    bdovaz, stonstad and cxode like this.
  4. TheZombieKiller

    TheZombieKiller

    Joined:
    Feb 8, 2013
    Posts:
    258
    Since there are (currently) no plans to upgrade the C# language version until the .NET Core migration is completed, I decided to see how much work it would take to produce a tool to brute-force the upgrade. Turns out it was easier than I expected, and appears to work quite well.

    I have made the tool available in this repository, but keep in mind that Unity does not endorse using this, and you should not expect them to provide any support for it. You effectively forfeit your ability to make valid bug reports while using this.

    The tool works by making two changes to the Unity installation folder:
    • Symbolically linking the
      DotNetSdkRoslyn
      and
      NetCoreRuntime
      directories to the most recent .NET SDK installed on your machine.
    • Detecting the most recent version of the C# language supported by the version of Roslyn included in the aforementioned SDK, and patching the
      UnityEditor.Compilation.ScriptCompilerOptions
      constructor to use that instead of the current value of
      "9.0"
      .
    Note that due to Unity (usually) being installed under Program Files, the tool requires administrative privileges in order to successfully patch the installation.

    Contributions are welcome, in case anyone wishes to help improve the tool.

    EDIT: As a cool bonus, the upgraded Roslyn enables newer source generators like PolySharp to work in Unity!
     
    Last edited: Sep 6, 2023
  5. optimise

    optimise

    Joined:
    Jan 22, 2014
    Posts:
    2,029
    Hi @JoshPeterson. Looks like at Unity 2023 cycle deprecated AssemblyBuilder API and I can't use it anymore. I still need AssemblyBuilder to generate assembly for my use case. What's the replacement solution to get similar AssemblyBuilder result?
     
    bdovaz likes this.
  6. JesOb

    JesOb

    Joined:
    Sep 3, 2012
    Posts:
    1,081
    Second change can be replaces with csc.rcp file in Assets directory with content "-langversion:latest"
    need nor administrative privileges no patching
     
  7. TheZombieKiller

    TheZombieKiller

    Joined:
    Feb 8, 2013
    Posts:
    258
    An .rsp file will not affect the generated project files that are used in the IDE, so the development experience is much worse (among other problems). The .dll change works with the IDE integration plugins because they use the LanguageVersion property to determine what version number to present in the IDE experience.
     
  8. JesOb

    JesOb

    Joined:
    Sep 3, 2012
    Posts:
    1,081
    I have use rsp for many years now and generated projects always have version equal to what in rsp file.
    So dont know what you talking about o_O
     
  9. TheZombieKiller

    TheZombieKiller

    Joined:
    Feb 8, 2013
    Posts:
    258
    I just made a brand new project in Unity 2022.3.8f1, added a csc.rsp file containing
    -langversion:latest
    (I also tried with
    -langversion:10
    ) in the Assets folder, and all of the generated .csproj files still have a LangVersion of 9.0. Are you sure you don't have some other code that is manually post-processing the .csproj files? Many earlier attempts at bumping the language version would do that.
     
  10. Mindstyler

    Mindstyler

    Joined:
    Aug 29, 2017
    Posts:
    224
    This already exists / existed for quite a while: https://github.com/mob-sakai/CSharpCompilerSettingsForUnity
     
    Huszky likes this.
  11. TheZombieKiller

    TheZombieKiller

    Joined:
    Feb 8, 2013
    Posts:
    258
    As far as I know, this package has issues on Unity 2021.x and above (correct me if I'm wrong), and only supports up to C# 10. My intention was to create a tool that only updated Roslyn and the language version, and did so in a similar manner to how it would (presumably) be done officially.
     
    Deleted User, cxode and Mindstyler like this.
  12. CaseyHofland

    CaseyHofland

    Joined:
    Mar 18, 2016
    Posts:
    548
    @TheZombieKiller s tool actually gave me an idea for a request the other way around:

    @JoshPeterson will you make sure to enable .csproj to specify language versions? I’m a package creator, and need to think about backwards compatibility and lowest-allowed Unity version. I’ve set up a system where I can work on packages while working on games, but I always have to go back to check compatibility. It would be A-MA-ZING if I could work from, say Unity 2028.1, but keep my package pinned down to a .NET version from 3 years ago.

    I already have no doubt that the improved language stability will make huge strides in allowing package development to become more stable as well, but this would knock it out of the park for me.
     
  13. Mindstyler

    Mindstyler

    Joined:
    Aug 29, 2017
    Posts:
    224
    with the .net migration complete, you would just specify the language version in the csproj as you request anyway
     
  14. JesOb

    JesOb

    Joined:
    Sep 3, 2012
    Posts:
    1,081
    Just test it. on empty project and it works
    May be this is because I have use rider as IDE and it smart enough in generating csproj files because csc.rsp is file where you can define many project specific options about warinigs, nullability, additional referenced aassembiles or delete referenced assembiles etc. So dont taking it into account when generate csproj is just smell of bad tool but rider is pretty good tool so it just works as expected :)

    Before rider there was UnityVS and we really have csproj preprocessor for it because it was sucks :)
     
  15. TheZombieKiller

    TheZombieKiller

    Joined:
    Feb 8, 2013
    Posts:
    258
    Yeah, it sounds like the Rider integration plugin just looks for a response file and uses that to tweak the generated project. If you ask me, that's just another reason that the .dll patch is a better approach, because it's propagated throughout the whole pipeline, it doesn't require special casing in the IDE integration plugins.
     
  16. John0king

    John0king

    Joined:
    Jan 14, 2022
    Posts:
    2
    @JoshPeterson is there any alpha build/nightly build that we can "try", or a demo with `dotnet run`?
     
  17. TangerineDev

    TangerineDev

    Joined:
    Sep 28, 2020
    Posts:
    122
    The answer is most likely no.
    As long as they won't publish any information about the progress of the project, then there won't be a demo either...
     
  18. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,771
    We don't have a public demo or nightly build yet, sorry! I'll ping on this forum thread when we do have something to release though.
     
  19. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,771
    This is an interesting idea. To be honest, it is not something we have considered yet, but it seems like it would be possible, as @Mindstyler mentioned.

    We will think through the ramifications for it and give it a try! I can't promise yet how this will pan out, but I hope that we can make it work.
     
    cxode, Luxxuor, TangerineDev and 3 others like this.
  20. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    1,015
    I remember that some years ago in the forum special builds were published (outside the normal flow of alpha, beta, ...) let's call it "pre-alpha" to get feedback as soon as possible and have different builds faster without waiting for the normal release bureaucracy.

    It would be interesting to have something like that but of course I understand that it's not a good practice to skip flows / processes so maybe this was only done before when Unity as a company was smaller.
     
  21. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,771
    You are correct, we did that during a few transitions to new Mono versions in the past. It was a useful way to get feedback quickly. As you guessed, at the scale of Unity currently it is more difficult to do that. I don't expect that we will have a similar pre-alpha kind of release now.

    With that said, we do expect to release Unity running via CoreCLR via the normal alpha channels. I'll make sure to announce that here, as the feedback from this group will be invaluable during the alpha process.
     
  22. TheZombieKiller

    TheZombieKiller

    Joined:
    Feb 8, 2013
    Posts:
    258
    After some experimentation and testing, I've put together a list of new language features introduced in C# 10/11 and their current status in Unity.
     
    cxode, NotaNaN, oscarAbraham and 4 others like this.
  23. TheZombieKiller

    TheZombieKiller

    Joined:
    Feb 8, 2013
    Posts:
    258
    I spent some more time experimenting, and managed to figure out why Unity can't detect
    MonoBehaviour
    classes when they are defined using a file-scoped namespace. Starting from Unity 2022 (I assume?), Unity added a new
    Unity.SourceGenerators
    assembly which is responsible for discovering these classes. However, the code inside it only looks for
    NamespaceDeclarationSyntax
    , instead of
    BaseNamespaceDeclarationSyntax
    . This causes it to fail when it encounters
    FileScopedNamespaceDeclarationSyntax
    . I just added some additional functionality to UnityRoslynUpdater to patch this, and now file-scoped namespaces function exactly as you'd expect.
     
  24. PetrisPeper

    PetrisPeper

    Joined:
    Nov 10, 2017
    Posts:
    62
    .NET has been using SocketsHttpHandler (which is based on sockets) for HttpClient for a few years already, you can however force WinHttpHandler to be used by doing this: new HttpClient(OperatingSystem.IsWindows() ? new WinHttpHandler() : new SocketsHttpHandler());. The issue is that IIRC last time I've tried to use it in Unity, IL2CPP crashed when using it.
     
    stonstad likes this.
  25. stonstad

    stonstad

    Joined:
    Jan 19, 2018
    Posts:
    599
    Thank you PetrisPeper! This is a terrific solution for Xbox! You wouldn't happen to know if a similar switch exists for WebSockets? I'll DM you with results on whether it works for a PC build and Xbox build.
     
  26. TheZombieKiller

    TheZombieKiller

    Joined:
    Feb 8, 2013
    Posts:
    258
    Because this fix only works for Unity 2022 and above, I decided to look into how I could get file-scoped namespaces working properly in Unity 2021 as well. It took a lot of messing around, but I managed to create an extremely experimental patch that replaces the native C# file parsing code in the editor with a Roslyn-based solution (compiled via NativeAOT). It might work in Unity 2020 as well, but I have not tested with it. (And naturally, you should not install this patch into Unity 2022 or above -- it will break).

    The patch comes in the form of two .dll files (
    version.dll
    and
    CSharpHelper.dll
    ) that get placed next to the editor executable. Keep in mind that this patch has NOT been thoroughly tested, so remove those files if you start to encounter crashes.

    With this patch and UnityRoslynUpdater, it's now possible to use C# 10 and C# 11 (in an unsupported manner) in all current LTS versions of Unity.
     
  27. CaseyHofland

    CaseyHofland

    Joined:
    Mar 18, 2016
    Posts:
    548
    You’re over engineering the hell out of it: respect :cool:
     
    TangerineDev likes this.
  28. TheZombieKiller

    TheZombieKiller

    Joined:
    Feb 8, 2013
    Posts:
    258
    I admit I might have been having a bit too much fun with it :D
     
  29. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    326
    At the dawn of .NET Core, Microsoft tried to migrate to JSON (Project.json instead of Project.csproj). I remember the massive outcry from .NET developers, which forced Microsoft to back off and switch back to XML/csproj in .NET Core 1.0 Preview 3 (or 4). It was fun times...

     
  30. Deleted User

    Deleted User

    Guest

    .NET developers really are their own worst enemies
     
  31. TangerineDev

    TangerineDev

    Joined:
    Sep 28, 2020
    Posts:
    122
    Well again, I'm not an advocate of JSON either...
    It definitely is better than XML imo, but honestly TOML is really the most readable/structured as well as just being storage space efficient...

    Welp, it doesn't matter anyways now :p
     
    Qbit86, cxode and Deleted User like this.
  32. Mindstyler

    Mindstyler

    Joined:
    Aug 29, 2017
    Posts:
    224
    I didn't actually know about that. *shrug*
    well imo that looks way more noisy than the xml version, so i'm not too much of a fan of it.
    toml might have been an interesting substitute for it, but....toml gets really messy with lots of list-like elements,
    so i'd be more an advocate for yaml than toml.

    nevertheless, the .csproj xml structure has excellent intellisense and autocomplete, so i still think everyone who hasn't even tried it yet overblows the complexity quite a bit
     
    Nad_B, TeodorVecerdi, cxode and 3 others like this.
  33. CaseyHofland

    CaseyHofland

    Joined:
    Mar 18, 2016
    Posts:
    548
    Am I the only one who thinks
    package.json
    vs
    package.csproj
    is not such a big deal? I’m still gonna Google what my options are and get the right syntax for it either way.
     
  34. Huszky

    Huszky

    Joined:
    Mar 25, 2018
    Posts:
    106
    @JoshPeterson have the team considered replacing UPM with nuget packages that have content files? With content files author can distribute Assets, and raw source code too, which would still allow for custom compilations with platform specific preprocessor directives witouth having to use tfm for every platform
     
    TangerineDev and bdovaz like this.
  35. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,771
    That is an interesting idea, and not something that we have considered yet.

    Honestly, I don't expect that we will make to many packaging changes for the initial release of new .NET with Unity. We're pushing hard on the internal deadlines we have for the runtime and editor now, and we can't really take on additional scope.

    But this is something we will keep in mind for the future.
     
  36. Huszky

    Huszky

    Joined:
    Mar 25, 2018
    Posts:
    106
    I mean if the build tool migration is done nothing should technically stop developers from doing this with for example a custom props/target file?
     
  37. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,771
    I agree! The overall success of this thread and others like it is very much down to the involvement from all of you, so thank you! As usual, I've been really impressed with the Unity developer community.

    In the interest of keeping this forum thread on topic for .NET talk, I'll request that we don't follow up on the pricing discussion here.

    There are some good places to provide that feedback elsewhere on the forums. Thanks to those of you from this thread who have provided your feedback, and I encourage other to do the same via the proper channels.
     
  38. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,771
    Oh yes, true! I was aiming more at the replacement of the UPM aspect. I don't expect UPM to go away or be replaced as part of the initial work we are doing.
     
    bdovaz likes this.
  39. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    1,015
    domFC, HaraldNielsen, NotaNaN and 5 others like this.
  40. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,771
    Nad_B, WilsonCWong, NotaNaN and 3 others like this.
  41. marce155

    marce155

    Joined:
    Jun 8, 2014
    Posts:
    8
    I know we aren't supposed to talk about this anywhere except in the mega-thread.
    But I still want to say how sorry I'm feeling for the team and @JoshPeterson who still have to show the happy face while all their hard work could have been in vain.

    Was holding out in hope for the .NET rework done by the team, seriously considering that decision now. Wanted to say thank you anyway and all the best, we know it's not your fault!
     
  42. Deleted User

    Deleted User

    Guest

    yeah I meant it as a joke lol
     
    CaseyHofland likes this.
  43. TheZombieKiller

    TheZombieKiller

    Joined:
    Feb 8, 2013
    Posts:
    258
    Now that .NET 8 has entered release candidate status, the version of Roslyn that comes bundled with it will report 12.0 as the latest C# version.

    This means you can now use C# 12 in Unity through UnityRoslynUpdater. Keep in mind that the Visual Studio integration package has not yet been updated for this. So while C# 12 code will compile, you will get errors in the Visual Studio IDE.

    You can fix this manually in
    Editor\VisualStudioForWindowsInstallation.cs
    by adding
    new VersionPair(17,8, /* => */ 12,0),
    under the
    // VisualStudio 2022
    comment inside the
    WindowsVersionTable
    array.
     
    Last edited: Sep 14, 2023
    TeodorVecerdi likes this.
  44. ProtoTerminator

    ProtoTerminator

    Joined:
    Nov 19, 2013
    Posts:
    566
    Also, .Net8 is adding
    [UnsafeAccessor]
    , which Roslyn actually supports (unlike
    [IgnoreAccessChecksTo]
    ).
     
  45. Mindstyler

    Mindstyler

    Joined:
    Aug 29, 2017
    Posts:
    224
    Oh, got a link to something so i can read more about it?
     
  46. ProtoTerminator

    ProtoTerminator

    Joined:
    Nov 19, 2013
    Posts:
    566
    WinterboltGames likes this.
  47. TheZombieKiller

    TheZombieKiller

    Joined:
    Feb 8, 2013
    Posts:
    258
    Note that
    UnsafeAccessorAttribute
    is mainly intended for source generators (so that they are able to access implementation details of a type they are generating code for, without forcing you to mark members with
    internal
    ), and currently does not work with static classes or types you don't already have access to. If your use case fulfills its requirements though, it's very ideal -- it works with trimming, it doesn't involve runtime reflection, it can be inlined (unlike dynamically generated methods), etc.
     
    oscarAbraham and TangerineDev like this.
  48. Mindstyler

    Mindstyler

    Joined:
    Aug 29, 2017
    Posts:
    224
    Def good to see some familiar faces in the discussion tho. Like yourself. :)
     
    TheZombieKiller likes this.
  49. PetrisPeper

    PetrisPeper

    Joined:
    Nov 10, 2017
    Posts:
    62
    And also generics.
     
    oscarAbraham likes this.
  50. jiraphatK

    jiraphatK

    Joined:
    Sep 29, 2018
    Posts:
    250
    Just want to let you know that amid the broken trust and the lowest point of Unity era, .NET update is still the one thing that I genuinely care and want to see being completed. keep up the good work team!
     
    Nad_B, ledshok, Kabinet13 and 13 others like this.