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

    MiTschMR

    Joined:
    Aug 28, 2018
    Posts:
    358
    TangerineDev and Nad_B like this.
  2. Mindstyler

    Mindstyler

    Joined:
    Aug 29, 2017
    Posts:
    224
    I think you're massively misinformed. .net 7 is just as much supported as a .net lts version would be, just not for as long. So it wouldn't make sense not to use upgrade to .net 7, or any other current (non-lts) version. And as Unity have said already, they are looking to always update to the new .net version shortly after a new one's been released each year.
    Also see: https://youtube.com/watch?v=55UdBNRuQ7k
     
  3. thetho

    thetho

    Joined:
    Oct 26, 2018
    Posts:
    1
    @JoshPeterson
    With the migration to MSBuild or at some other point during the .NET migration, will it also be possible to write, compile, run & debug games completely without the editor?
     
    Qbit86, TangerineDev and Nad_B like this.
  4. TangerineDev

    TangerineDev

    Joined:
    Sep 28, 2020
    Posts:
    122
    Well without the source ( or the enterprise license from Unity ) you're probably as an Indie not going to be able to do that. ( Maybe circumvent with an engine dll? )

    Otherwise... That might work...
     
  5. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    1,084
    Will il2cpp benefit somehow from CoreCLR in the future or is it completely unrelated as il2cpp has it's own runtime?
     
    AliAlbarrak and Anthiese like this.
  6. Huszky

    Huszky

    Joined:
    Mar 25, 2018
    Posts:
    106
    They stated in the past that they want to bring IL2CPP and NativeAOT closer together
     
  7. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,771
    Yes, our goal is to be able to upgrade Unity to a new version of .NET when it is available. We're still not sure how to sync up the Unity LTS support cycle with the .NET one. We probably don't want to "back port" a new .NET version to a Unity LTS version, but that process does wait to be seen.

    Once we get to the point where can release Unity with .NET we should have a better idea about how the release cadence will line up.
     
    Trigve, NotaNaN, thetho and 5 others like this.
  8. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,771
    We're going to use the same BCL as CoreCLR (the one from the dotnet/runtime repository) for IL2CPP. In addition, we've started to find some performance improvements we can make to IL2CPP based on the way that CoreCLR works.

    We touched on one of those cases in this blog post: https://blog.unity.com/technology/making-animationevent-safe-for-the-coreclr-garbage-collector
     
    Trigve, NotaNaN, VolodymyrBS and 7 others like this.
  9. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,771
    We don't have that one our roadmap at the moment - the plan is still to have Unity as an editor-driven workflow. With that said, I don't think this is out of the realm of possibility in the future. One of the reasons go move to MSBuild is to give us flexibility to consider things like this, which we can't do currently.
     
    Trigve, NotaNaN, goldbug and 6 others like this.
  10. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,771
    The .NET Conf talk is now available on YouTube:
     
  11. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,194
    If the above is a big fat image going "ohno cookies", the link is GAH UNITY FORUMS

    Click here

    If I actually write the url in cleartext, the forums puts a MEDIA tag around it, and then the forum software replaces it with a big box saying that things inside MEDIA boxes won't work because of cookies. Thanks, forums!

    But uh like great work on .NET!
     
    mklasson, JoshPeterson and cxode like this.
  12. cxode

    cxode

    Joined:
    Jun 7, 2017
    Posts:
    267
    Great presentation Josh! I am very excited about all of the fun new .NET things coming!!
     
    JesOb, zhuxianzhi, NotaNaN and 2 others like this.
  13. CaseyHofland

    CaseyHofland

    Joined:
    Mar 18, 2016
    Posts:
    548
    I had no idea you could use Burst in an unsafe context and get the same performance benefits! That’s really interesting!
     
    Nad_B and JoshPeterson like this.
  14. Yupmoh

    Yupmoh

    Joined:
    Dec 9, 2021
    Posts:
    1
    About time we got .NET in Unity!
     
  15. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    760
    I've been playing around with the alpha and Awaitable a bit, and I've noticed that the job system might need an Awaitable sopport. I wrote my own extension:

    Code (CSharp):
    1. public static class JonHandleExtensions
    2. {
    3.     public static async Awaitable CompleteAsync(this JobHandle handle, int maxFrames, CancellationToken cancellationToken = default)
    4.     {
    5.         for (int i = 0; i < maxFrames; i++)
    6.         {
    7.             if(handle.IsCompleted) break;
    8.             await Awaitable.NextFrameAsync(cancellationToken);
    9.         }
    10.         handle.Complete();
    11.     }
    12. }
    And in a MonoBehaviour:

    Code (CSharp):
    1. async Awaitable Start()
    2. {
    3.     var myArray= new NativeArray<int>(10, Allocator.TempJob);
    4.     var handle = new MyJob
    5.     {
    6.         Data= myArray
    7.     }.Schedule();
    8.  
    9.     await handle.CompleteAsync(4, destroyCancellationToken);
    10.  
    11.     // to something with the data
    12.  
    13.     myArray.Dispose();
    14. }
     
    JesOb likes this.
  16. simon-ferquel-unity

    simon-ferquel-unity

    Unity Technologies

    Joined:
    Apr 1, 2021
    Posts:
    65
    @runner78 I had a prototype initially with JobSystem integration, but it goes against some of the job system principles:
    The job system is aimed at running short-lived job (jobs that would start and finish within the same Update call) concurrently. Its scheduler is skewed toward achieving the less overhead as we can, at the cost of lack of features. For example, having long running jobs may lead to job system starvation very quickly as the number of worker threads is static.
    This could lead to whole game subsystems to collapse (e.g. Physics runs many concurrent jobs on every frames, and if you start having jobs spanning multiple frames - which would probably be the main interest of adding await support - , you're going to starve it).

    So for long running tasks offloading, you should prefer using:

    Code (CSharp):
    1. async Awaitable Start()
    2. {
    3.     using(var myArray= new NativeArray<int>(10, Allocator.TempJob)){
    4.        await Awaitable.BackgroundThreadAsync();
    5.     // fill up myArray with some data in a long running background operation
    6.        await Awaitable.MainThreadAsync();
    7.        // do something with the data.
    8.      }
    9. }
    Note: BackgroundThreadAsync / MainThreadAsync are coming in an upcoming alpha alongside an AwaitableCompletionSource class.
     
  17. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    760
    I have a case where I'm instating a few hundred of MonoBehavors, even if the individual jobs don't take long, the total number of jobs takes several seconds. However, not much would change with the 4 frame limit.

    Another thing that makes me wonder, is there a struct based Awaitable too and will async await be bust compatible someday? E.g. for ECS ISystem.

    if Awaitable.BackgroundThreadAsync() and Awaitable.MainThreadAsync(); new? Is not in the current Alpha 18
     
  18. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    283
     
  19. simon-ferquel-unity

    simon-ferquel-unity

    Unity Technologies

    Joined:
    Apr 1, 2021
    Posts:
    65
    Awaitable is a class, but it is pooled, to avoid allocating. It has some limitations though:
    - an Awaitable must be awaited only once (after awaiting it, it gets back to the Awaitable object pool)
    - a non awaited Awaitable will get ultimately garbage collected (not really an issue, but that means it can't be reused)
    - awaitable continuations are run synchronously, and any non catched exception will be raised up to the trigger of the completion (can be tricky with the upcoming AwaitableCompletionSource)

    Most of the time, after a bit of warmup, you should not see any allocation and it won't create garbage then.
     
  20. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    1,084
    May be an issue especially when calling these methods from sync code. Not very difficult to fix with a custom runner that awaits them but I'd expect it be implemented in the engine instead. I guess Forget() (or FireForget()) extension method would be a good place as it's a standard way to get exceptions from non awaited Task, UniTask also uses that name to get rid of compiler warnings about missing await.

    Code (CSharp):
    1. void MySyncMethod()
    2. {
    3.   MyAwaitableMethod().Forget();
    4. }
    5.  
    6. ...
    7.  
    8. static void Forget(this Awaitable a)
    9. {
    10.   AwaitableRunner.I.Add(a);
    11. }
    12.  
    13. ...
    14.  
    15. class AwaitableRunner : MonoBehaviour //singleton
    16. {
    17.   //list or something thread-safe?
    18.   List<Awaitable> list = new();
    19.  
    20.   public void Add(Awaitable a) => list.Add(a);
    21.  
    22.   async Awaitable Update()
    23.   {
    24.     foreach(var a in list)
    25.       await a;
    26.     list.Clear();
    27.   }
    28. }
     
    Last edited: Nov 19, 2022
    JesOb and Saniell like this.
  21. JesOb

    JesOb

    Joined:
    Sep 3, 2012
    Posts:
    1,081
    May be more clear name like:
    SwitchToBackgroundThreadAsync
    SwitchToMainThreadAsync
    or
    ContinueOnBackgroundThreadAsync
    ContinueOnMainThreadAsync
     
    Last edited: Nov 19, 2022
  22. TieSKey

    TieSKey

    Joined:
    Apr 14, 2011
    Posts:
    219
    Add a "to" so it reads as a more or less proper sentence? :p

    await Awaitable.ToContinueOnBackgroundThread();
    ...
    await Awaitable.ToContinueOnMainThread();
     
    JesOb likes this.
  23. Mindstyler

    Mindstyler

    Joined:
    Aug 29, 2017
    Posts:
    224
    no
     
  24. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    283
    These sound nice. The current names kind of go against typical convention (start with a verb). Still, after a using static directive, "await BackgroundThreadAsync()" kind of has a ring to it as well.
     
    TangerineDev likes this.
  25. Saniell

    Saniell

    Joined:
    Oct 24, 2015
    Posts:
    167
    I've been reading thread about future of text in unity and I see that Unity will move TextMeshPro directly to engine source instead of being a package. This will also happen to GraphToolsFoundation and something tells me, same will happen to many other packages.

    So I have a bit of a naive question maybe, but would it be possible to distibute Unity using source csharp files instead of compiled dlls? I mean, you already provide reference source on github. So maybe Unity could compile engine libraries on startup only (because I'm not sure if it's possible to recompile them on fly?)

    Or at least provide a way to compile your own dlls from reference code. I know it might be too much to ask considering it'll also involve licensing questions, but I would like to know if it is a possibility, considering that people will only get more annoyed as number of closed-source packages will, probably, grow
     
  26. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,771
    I would recommend you start a new thread for this question. I don't think it is directly related to the future of .NET in Unity, and unfortunately the people who might be able to answer this are not likely watching this thread.
     
    Saniell likes this.
  27. SonicBloomEric

    SonicBloomEric

    Joined:
    Sep 11, 2014
    Posts:
    1,081
    Please make sure that this makes its way into the documentation for the Awaitable class. This information is crucial.
     
    NotaNaN, MasonWheeler, OCASM and 4 others like this.
  28. Kinggrass

    Kinggrass

    Joined:
    Jun 7, 2016
    Posts:
    66
    Hi @JoshPeterson,

    I am currently working on a source generator.

    Is there currently a way to use Source Generators in a package?
    Unfortunatly Assets in a package can not be labeled ... :/
     
  29. Hertzole

    Hertzole

    Joined:
    Jul 27, 2013
    Posts:
    416
    This is a bit of a dumb workaround, but if you have your source generator as a dll file, move it into your assets folder, assign the labels there, and then move it back to the package.
     
    Kinggrass likes this.
  30. Huszky

    Huszky

    Joined:
    Mar 25, 2018
    Posts:
    106
    Or build it outside of unity, publish on nuget and distribute using xoofx's unity nuget https://github.com/xoofx/UnityNuGet
     
    Kinggrass likes this.
  31. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,051
    It's only the UI that's missing. For write-enabled packages (local or embedded ones), you can use AssetDatabase.SetLabels to add labels anyway.

    This is probably an oversight and the UI should be visible for write-enabled packages (and only disable editing for read-only packages). If you're affected by this, I'd try submitting a bug report.
     
    Kinggrass likes this.
  32. Kinggrass

    Kinggrass

    Joined:
    Jun 7, 2016
    Posts:
    66
    Okay thanks to @Adrian , @Huszky , @RealMTG :)
    That works :)

    But now I am stumbling upon the issue that no files are generated.
    My usecase is that I want to iterate through another C# Library and to generate a Unity integration like MonoBehaviours and Menu entries. The Library is a subject to change and therefore it has to generate it automatically again if changed.
    Is there a way to archive this
     
  33. Mindstyler

    Mindstyler

    Joined:
    Aug 29, 2017
    Posts:
    224
    If nothing is generated, the most likely cause is that you're using a yet unsupported (in unity) SourceGenerator api version.
     
  34. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    1,084
    Being able to mark assembly definition as Source Generator would be a great workflow improvement. I'd hope it could be added if asmdef-to-csproj won't land in Unity 2023
     
    Nirlah likes this.
  35. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    1,049
    How is C# 11 progress so far. Especially about generic math, have you include that into unity type like Vector3 and float3 ?

    ps generic math also imply many feature like static interface member
    ps2. Generic Attribute also great for unity to adopt in C# 11
     
  36. Kinggrass

    Kinggrass

    Joined:
    Jun 7, 2016
    Posts:
    66
    I only said that no files are generated. It works but Source Generator by default dont generate files but let them be part of the compilation and the end product.
    This is a problem if you want to generate code for Unity because Unity does generate *.meta files and does referencing by those. So Scripts as a Asset file on your disk are necessary and not only during compilation.
     
  37. zhuxianzhi

    zhuxianzhi

    Joined:
    Mar 30, 2015
    Posts:
    120
    Classes can be marked as partial, which can be created in Unity to generate .meta files. Source Generator works fine.
     
  38. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    1,049
    Are there any plan to bridge dotnet native for mobile and wasm or move the compiler to use dotnet build for mobile and wasm instead? I wish I could use dotnet interop system directly in unity
     
  39. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    1,049
    Maybe if we have unity that moved to csproj build system. Unity might not need .meta for cs files that not a monobehavior in the project anymore. And might need only one meta for csproj instead (there was mention about making cs file to not be an asset, I don't know how much would it imply)
     
  40. CaseyHofland

    CaseyHofland

    Joined:
    Mar 18, 2016
    Posts:
    548
    Hold the phone stop the presses:
    Bleh.png

    I opened the cupboard and accidentally found our Christmas gift. @JoshPeterson will we need to do some setup or will source generators just work? And which Unity versions does this include?

    Now that I'm here anyway, kudos on Burst 1.8.1, great stuff to the whole team!
     
  41. Mindstyler

    Mindstyler

    Joined:
    Aug 29, 2017
    Posts:
    224
    You were able to use SourceGenerators (at least v1 ones) and c# 9 for months already.
     
  42. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    836
    That's pretty neat, but I was always disappointed that source generators only allow adding new source files, rather than modifying existing code. Makes IL weaving the only way to actually modify the outputted code, and it's a pretty in depth topic to get into if you just want to do some simple things (transforming properties a bit etc).
     
  43. Mindstyler

    Mindstyler

    Joined:
    Aug 29, 2017
    Posts:
    224
    That was a design choice and not a limitation.
    Although you could always use direct roslyn api's to unsafely modify existing code and not deal with IL weaving.

    But almost any use case can be solved with source generators. It just requires a bit of tinkering and thinking outside the box for some niche advanced use cases.
     
  44. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    283
    Relax, kid. That's extremely far from news by this point. com.unity.roslyn added support by way of external compiler toolchain for 2020.1. 2021.2 and above have built-in support. There's been documentation on this for a while as well.
     
    Luxxuor likes this.
  45. kdserra

    kdserra

    Joined:
    May 16, 2018
    Posts:
    7
    Please consider adding
    Awaitable.WaitUntil
    .
     
  46. Mindstyler

    Mindstyler

    Joined:
    Aug 29, 2017
    Posts:
    224
    Doesn't make sense? What would it even do?
    There's already Awaitable.WaitForSecondsAsync()
     
  47. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,771
    We won't have C# 10 or C# 11 support until we get full support for .NET 7/8 in Unity. Once we complete the runtime support we will then have full C# support and the ability to track new versions of C# when they are released. We don't have an ETA ready to discuss publicly yet though.
     
    NotaNaN, TangerineDev, cxode and 3 others like this.
  48. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,771
    We don't have specific plans now to support dotnet build at the first release of .NET 7/8 with Unity. However that might be something that will come later, I don't think we have put enough time into investigating it yet to know if it would integrate with Unity.

    We're still planning to use IL2CPP as the AOT solution for mobile and web targets in Unity (although IL2CPP will work with the BCL from the dotnet/runtime repo, the same one CoreCLR uses). Microsoft seems to be moving fast with improvements to .NET Native and Blazor though, so we are watching those developments, and may adopt them in the future.
     
    Arlorean, SpencerGR, NotaNaN and 3 others like this.
  49. Laicasaane

    Laicasaane

    Joined:
    Apr 15, 2015
    Posts:
    288
    It waits until something becomes true and we don't know when that will happen.
     
    kdserra likes this.
  50. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    1,049
    I assume
    WaitUntil
    is the function that take predicate callback

    Awaitable.WaitUntil(() => somethingIsTrue)