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. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    487
    It's not Core anymore since 2020. Microsoft dropped the “Core” suffix (in favor of no suffix) starting from .NET 5.
     
  2. JesOb

    JesOb

    Joined:
    Sep 3, 2012
    Posts:
    1,081
    Syntax sugar works
    everything else not :)

    I have use it for many years in production projects
    Every Unity version has C# compiler with support for roughly 1 C# version bigger than default option but you can use only syntax sugar from it.
     
    Anthiese likes this.
  3. Mindstyler

    Mindstyler

    Joined:
    Aug 29, 2017
    Posts:
    224
    Since the csc.rsp is thrown around a lot here lately, i want to make everyone aware that you don't need the .rsp anymore. If you go to Edit -> Project Settings -> Player -> Additional Compiler Arguments
    you can actually just add
    -langversion:latest
    or
    -langversion:preview
    or even
    -nullable
    as well as other arguments to get the desired outcome without polluting the project with an extra file, as well as easier managing of these values.
     
    sammtan, Nad_B, TangerineDev and 3 others like this.
  4. TheZombieKiller

    TheZombieKiller

    Joined:
    Feb 8, 2013
    Posts:
    258
    Sure you can. Those are language features, not something defined by .NET Standard (which defines APIs, not language features). In fact, it's even easier to do this in an external .dll because you have more control over how it gets compiled.

    Also worth noting that using this approach and/or the .rsp file approach won't work properly with the Visual Studio integration (JetBrains Rider integration will parse the .rsp file and report the right language version though). Not sure about Visual Studio Code integration.

    You also cannot use file-scoped namespaces with MonoBehaviours and ScriptableObjects if you upgrade the language version this way, because when Unity parses your .cs files to locate the types defined in them, it doesn't do it in a way that is compatible with file-scoped namespaces. To fix that you need to patch the editor, or just use UnityRoslynUpdater to do that automatically.
     
    TangerineDev, oscarAbraham and JesOb like this.
  5. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,771
    Yes, we expect some breaking changes, specifically around enter/exit playmode behavior in the Unity editor. Current Unity and Mono use the AppDomain concept to reload code. This does not exist in .NET Core, so we will use AssemblyLoadContext instead. There are some differences between the two that will lead to breaking changes for some projects.

    We will have full details and documentation as we get closer to shipment.
     
    TangerineDev likes this.
  6. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,771
    The csc.rsp should only impact the C# compiler. Mono and IL2CPP both use the same C# compilation part of the build pipeline, so I don't expect it to impact IL2CPP at all.
     
    JesOb likes this.
  7. optimise

    optimise

    Joined:
    Jan 22, 2014
    Posts:
    2,029
    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?
     
  8. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,771
    I don't know the specific details about this, but I can find out. Do you mind opening a new forum thread for this question though? I don't think it is specific to .NET Modernization, and I'd like to centralize the discussion on a different thread so that it is easier for others to find.
     
    optimise likes this.
  9. ejl103

    ejl103

    Joined:
    Dec 21, 2021
    Posts:
    4
    Hi, can I have any hope that when the editor moves to the .NET runtime the game will no longer run as part of the same process as the editor? The current way things work make working with native plugins a nightmare as you have various issues with hot reloading (solved on Windows easily enough.. well we've done the work already) but on Mac when you have either Objective-C or Swift code (basically forced to use most Apple API's) dylibs will not unload on dlcose() due to the way the runtimes work in those languages.

    The other issue you have is that crashes in native code bring down the editor, with a chunk of work can be partitially mitigated on Windows, again not so much on mac with no unloading.

    Thanks, Ed.
     
  10. optimise

    optimise

    Joined:
    Jan 22, 2014
    Posts:
    2,029
    I would like to know which part of dots will break when official upgraded to CoreCLR? Is that breaking changes just exactly the same with at game object land?
     
  11. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,771
    Hey Ed! This is something we have looked at a good bit internally. For the initial new .NET release of Unity the answer is no, the playmode of the game running in the editor will not be moved out of process. We will not be able to address the issues your raised (which are valid).

    Longer term, I do believe we will move the playmode out of process. This is a common source of problems, so I hope it is something we can correct.
     
  12. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,771
    I don't expect any DOTS to be impacted by breaking changes more than any other code. We will make sure that DOTS works completely with the Unity editor on CoreCLR before we get to a release.
     
    optimise likes this.
  13. PineFilip

    PineFilip

    Joined:
    Jul 7, 2022
    Posts:
    1
    Hey @JoshPeterson. First of all, thank you for being very active in this thread, we appreciate it. I have been following this thread and have been excited for this change for quite some time, but I can't help myself becoming slightly impatient.

    I understand that this is a monumental task, but I am also slightly annoyed that Unity, being the most popular engine in the world (with most resources and close relationship with Microsoft) does not already have this supported. With recent Unity scandal, I have been researching quite a few different engines and a lot of those running C# already have .NET 7 supported. I have a feeling that Unity management does not know what should be prioritized.

    So, I wanted to ask:
    1. How big is the team currently working on this incentive (as in number of people)?
    2. I know you can't give any dates, but could you give an estimation of % being done (rough estimate)?
    3. Could you give lower and upper bound time estimates when we could approximately expect this update?
    4. What are currently the biggest/hardest problems that you are solving?
    5. Are there any problems that need to be solved, but yet haven't been started?

    Once again Josh, you have been wonderful in this thread and I have no doubt this will get finished under your leadership. This "rant" was not directed at you, rather at Unity management. Thank you.
     
  14. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,771
    These are great questions (many of which I would be asking if I was on the user side of this as well)! Unfortunately I cannot share answers to all of them, due to some reasonable constraints we have on external communication about internal resourcing and priorities.

    I can share a few answers though:

    The largest issue is the need to make the Unity engine and editor code safe for the CoreCLR GC. We've been at this for a while now, and are coming to the end of this work internally, which is rather thrilling! There are additional problems around code reload in the Unity editor using AssemblyLoadContext instead of AppDomain that are similar - we need to change the assumptions Unity code has been working with for 15 years. Finding and fixing all of that code takes time.

    No! I'm happy to say that all of the known problems have solutions in-progress now. So I think we are on the downhill side of the work. We've mostly stopped discovering new problems and tasks. There is plenty of work to do, but we have line of sight to complete all of it.
     
    Gustave, ThatDan123, NotaNaN and 16 others like this.
  15. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    1,015
    @JoshPeterson one question, what progress has been made on the whole sdk style projects / dotnet native tooling feature? @xoofx started the UnityNuGet project and I am eternally grateful, in fact, he knows that to this day it is a child that we share (I'm a mantainer too)

    But honestly, I can't wait to set that project on fire and get real NuGet support.
     
    Huszky and CaseyHofland like this.
  16. ProtoTerminator

    ProtoTerminator

    Joined:
    Nov 19, 2013
    Posts:
    566
    @JoshPeterson About that struct static constructor bug in IL2CPP, it was only half fixed. It still doesn't work for generic structs. I emailed back on the issue and got an automated response that it was re-opened, but haven't seen anything else in over a month. Could you check on it? (IN-43207)

    [Edit] I was wrong, it does work with generic structs. :oops:
     
    Last edited: Oct 3, 2023
  17. TangerineDev

    TangerineDev

    Joined:
    Sep 28, 2020
    Posts:
    122
    One reason why I *always* use a for loop with an upper limit instead of a while loop... ugh

    (Although this is kind of a best practice...)
     
    Last edited: Oct 3, 2023
  18. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,771
    There is work going on int his area, but I don't yet know what parts of it will be available when, to be honest. I suspect we will have more to say when we can announce shipment dates, but likely not before that.
     
    bdovaz likes this.
  19. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,771
    Sorry we missed this, somehow our bug tracking system did not re-open it. We will investigate this additional test case you provided. Thank you!
     
    ProtoTerminator likes this.
  20. TheOtherMonarch

    TheOtherMonarch

    Joined:
    Jul 28, 2012
    Posts:
    791
    That had to be a huge job. Good to hear it is getting closer to initial release.
     
    JoshPeterson likes this.
  21. Kabinet13

    Kabinet13

    Joined:
    Jun 13, 2019
    Posts:
    63
    I hate to ask again, but is there any news on that blogpost?
     
  22. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    1,049
    Seem like they would keep everything to release in Unite event
     
  23. vertxxyz

    vertxxyz

    Joined:
    Oct 29, 2014
    Posts:
    99
    Walter_Hulsebos likes this.
  24. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,771
    Not yet, we're still waiting to see when it will be published, sorry!
     
    CodeSmile, AliAlbarrak and Kabinet13 like this.
  25. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    1,049
    Nearly a week now
     
  26. Gamma_Snaplight

    Gamma_Snaplight

    Joined:
    Nov 23, 2022
    Posts:
    8
    We're all waiting...
     
  27. Tuncle

    Tuncle

    Joined:
    Oct 1, 2018
    Posts:
    12
    @JoshPeterson
    Hi, our team is doing preliminary research for future projects. The basic structure of future projects are: there will be a main project which works as a launcher. And there are several sub-projects which works as mini-games opened by launcher.

    All these projects wish to be made by Unity. For now, our approach is to build main project as an apk running on our device. And all sub-projects will export the asset as AB, and export all codes as dlls. The main project is responsible for loading dll and ABs. And we are using hot-reload solution HybridCLR to make the demo.

    The problem we are currently encountering is that the loaded dll cannot be unloaded, and all dlls can only be loaded into one app domain.

    Whether it's possible to run each dll in the separate app domain, and unload / reload it at anytime in the future Unity version with .net?

    Our core requirement is to safely isolate the running logic of multiple mini-games from the Launcher (the crashed mini-game will not lead to launcher crash and mini-game can not access launcher codes), but all content can be rendered together. If you have a better solution, please tell us
     
  28. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,771
    This will not be possible in a future version of Unity with .NET, since current .NET has removed app domain support entirely.

    I don't know of a great solution for this at the moment. Process isolation is likely the best option to avoid a crash in one mini-game from impacting the launcher. I not sure if rendering together is possible in that context though.
     
  29. Tuncle

    Tuncle

    Joined:
    Oct 1, 2018
    Posts:
    12
    Sad...
    Is there any alternative to app domain in the current .Net ?The requirements I am most concerned about are that the assembly can be load/unload/reload dynamically at runtime and the script in one assembly can not access the other via reflection (for safety)

    Are both of these requirements impossible in future version of Unity with .Net?
     
  30. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,771
    Yes, you might want to look at AssemblyLoadContext: https://learn.microsoft.com/en-us/dotnet/core/dependency-loading/understanding-assemblyloadcontext

    Unity is using this to implement code reload for enter/exit playmode (which was previously done via app domains). It is really nice, and it might meet your requirements.

    However, a crash in some loaded assembly will impact the entire process, so that might be a problem for your use case.
     
  31. Flavelius

    Flavelius

    Joined:
    Jul 8, 2012
    Posts:
    926
    I hope that with the .net upgrade, that sounds like i brings some breaking changes anyway, it will also be possible to finally get rid of those unnecessary magic properties, like .rigidbody, .camera, .light etc., that just generate undesired warnings ('use new to hide'..) because it's natural to use these names for your own members.
     
    NotaNaN, Kabinet13, Enderlook and 4 others like this.
  32. CaseyHofland

    CaseyHofland

    Joined:
    Mar 18, 2016
    Posts:
    548
    They shouldn’t bring too many breaking changes actually, apart from switching AppDomain with AssemblyLoadContext.

    Anything that it does break however is unrelated to API implementations like these.

    Personally, I’m with you, those properties are idiotic.
     
  33. RaventurnPatrick

    RaventurnPatrick

    Joined:
    Aug 9, 2011
    Posts:
    165
    I would propose a different naming convention for your private fields such as
    _rigidbody
    ; (underline in front) otherwise it is too easy to clash with your local scoped variables anyways.
    However I agree that all unity fields that are exposed should really behave like a field access (fast, no side effects) instead of calling
    GetComponent<>
    internally. Also please make sure the dispose pattern is properly implemented for Components (instead of needing to check the instance against a boolean)
     
  34. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,771
    This is a good discussion, thanks to all of you. We're talking about this internally now. I'll follow up with details about our decision. It looks like these properties are already marked as obsolete, so this might be a good time to remove them.
     
  35. Altale

    Altale

    Joined:
    Mar 13, 2017
    Posts:
    3
    Hi, thanks for the hard work, is there an ETA that we can have access to CoreCLR in alpha builds?
     
  36. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,771
    Nope, there is nothing we can share publicly yet. I'll let you know as soon as I can!
     
  37. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    1,049
    It's now three weeks since you say there was a blog to be published though
     
  38. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,771
    Yep! Unfortunately I don't control the publishing schedule. I'll refrain from making any more guesses about when it will be out though. It is still planned!
     
  39. CaseyHofland

    CaseyHofland

    Joined:
    Mar 18, 2016
    Posts:
    548
    Still can’t do things like
    renderTexture?.Release();


    Any hope you’ll take a second look at handling null in Unity? It’s only gonna get worse…
     
    DiacPaulAlexandru, Nad_B and bdovaz like this.
  40. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,771
    Yes, that is on the table as well! We will try to clean up all the things!
     
  41. oscarAbraham

    oscarAbraham

    Joined:
    Jan 7, 2013
    Posts:
    431
    I always thought that the improvement for Unity's null handling would be to replace it's equality operator overloads with some kind of
    IsAlive
    property or method. The thing is, it wouldn't allow that kind of null-conditional operator, because you'd still need to check if the Object's native part still exists.

    I'm curious about how that kind of null-conditional usage would be allowed. Would the Release method check if the Object is still alive before actually trying to execute?
     
  42. WinterboltGames

    WinterboltGames

    Joined:
    Jul 27, 2016
    Posts:
    257
    You can do this right now by using an extension method. Inside the extension method, do the null check and call the relevant method if the check is false.

    Code (CSharp):
    1. public static class RenderTextureExtensions
    2. {
    3.     public static void Free(this RenderTexture renderTexture)
    4.     {
    5.         if (renderTexture == null)
    6.         {
    7.             Debug.LogWarning("Attempted to free a null RenderTexture. Ignoring request.");
    8.         }
    9.         else
    10.         {
    11.             renderTexture.Release();
    12.         }
    13.     }
    14. }
    This is not the best solution, as you would need to do this for 100s of methods, but it's a good enough workaround for now.
     
  43. CaseyHofland

    CaseyHofland

    Joined:
    Mar 18, 2016
    Posts:
    548
    The problem is, why would I want to do that? The point is not so much about possibility as it is about convenience. I already make sure to do null checks. Extensions methods sound not like such a good practice here, because it gives you a lot of small code with little purpose to do upkeep on, and when or not you implement this pattern requires clear and distinct rules as to not confuse anyone using your codebase.

    To be fair, there is a larger issue about clarity and causality. If Unity won't tell you the difference between
    null
    or
    destroyed
    , then this can in special cases lead to unforeseen and hard-to-debug errors (mostly editor scripts). My point is more so about
    null
    not being properly represented that makes it hard to reason about some things here and there.

    EDIT:
    That being said, sorry for being a bit douchy, I appreciate you taking the time to help and think along <3
     
    WinterboltGames likes this.
  44. WinterboltGames

    WinterboltGames

    Joined:
    Jul 27, 2016
    Posts:
    257
    I agree. I never actually do the above unless I need to log that such an operation was attempted. I really despise how Unity handles
    null
    . I am DREAMING for the day I will be able to use my dear
    ?
    and
    ??
    operators :(
     
    CaseyHofland likes this.
  45. christianbay

    christianbay

    Unity Technologies

    Joined:
    Dec 27, 2017
    Posts:
    1
    We can confirm that we are actively exploring options to eliminate the explicit use of boolean operators, equal operators with null, and Equals(null). Instead, we aim to make the checks explicit. While this may result in slightly more verbose code, it will enhance the transparency of the code's behavior and reduce reliance on hidden mechanisms.

    We have conducted preliminary experiments using .NET analyzers to replace explicit boolean checks and equality operators with explicit methods such as IsDestroyed. We have also explored the use of extension methods like IsNullOrDestroyed and NullOrAlive to assess how they look and feel in practice. However, please note that this is still in the early stages, and we need to thoroughly examine other alternatives and weigh the pros and cons.
     
  46. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,903
    The problem with the ? and ?? that they aren't overridable, so we will never be able to use them while we also have fake nulls to keep tabs on the unmanaged objects. I really like the analyzer approach, if it can be made robust and replace these usages with isDestroyed calls (or whatever the method for checking ends up named) in case of Unity-specific objects.
    Although obviously it's a little bit error-prone since we need a robust analyzer otherwise we open up for very (I mean very) hard to find errors.
     
  47. CaseyHofland

    CaseyHofland

    Joined:
    Mar 18, 2016
    Posts:
    548
    A cool thing coming to C# 12 is that it will start introducing breaking changes. But the way they do it is as such:
    - They have determined a breaking change they want to introduce in the future.
    - The next version (in this case C# 12) will start to throw compiler warnings about the breaking change.
    - The version after that (C# 13) will include the breaking change.

    I've said something along these lines before, but Unity can start deprecating code, with a message stating in which version that deprecated code will be removed. That sounds like a perfect fit.



    For anyone interested, the breaking change in C# 13 is going to be field as a keyword. I'm pretty excited as it will allow for the following code:

    Code (CSharp):
    1. [field: SerializeField] public float myFloat
    2. {
    3.     get => field;
    4.     set => field = Mathf.Clamp01(value); // Instead of keeping a separate value.
    5. }
    6.  
    7. void OnValidate()
    8. {
    9.     myFloat = myFloat;
    10. }
     
  48. Igotlazy

    Igotlazy

    Joined:
    Jan 15, 2018
    Posts:
    51
    Hi Josh. The last update to the main post was back in February 2023. Would it be possible to update it based on current goings-on?
     
  49. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,771
    Good point, I'll make an update!
     
    Flavelius, jowseyy, NotaNaN and 2 others like this.
  50. MiTschMR

    MiTschMR

    Joined:
    Aug 28, 2018
    Posts:
    358