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. Improve your project's performance with our new guide on profiling in Unity.
    Dismiss Notice

Unity C# 8 support

Discussion in 'Experimental Scripting Previews' started by Jes28, Apr 18, 2019.

  1. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    706
    You can try above code to see that struct will not be boxed

    If it boxed, the value would not be changed. It is not boxed with generic and ref
     
  2. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    706
  3. TheZombieKiller

    TheZombieKiller

    Joined:
    Feb 8, 2013
    Posts:
    199
    You can copy it from a Visual Studio installation (located in (Visual Studio Dir)/MSBuild/Current/Bin/Roslyn) and replace the one in Unity (Editor/Data/Tools/Roslyn). Then you can put -langversion:9 in a csc.rsp file to use C# 9.

    The latest 2021 builds of the engine have already updated the compiler so you can use C# 9 without upgrading, but annoyingly there's no way to customize csproj generation. Unity places an explicit <LangVersion>8</LangVersion> in project files, so you need to keep modifying them to stop getting errors in Visual Studio.
     
    l33t_P4j33t likes this.
  4. SugoiDev

    SugoiDev

    Joined:
    Mar 27, 2013
    Posts:
    391
    You can also try using a csc.rsp file in the Assets/ folder containing

    Code (CSharp):
    1. -langversion:9.0
    Edit: in newer Unity versions, it seems you can add compiler options using the editor.
    Use Edit -> Project Settings -> Player, then scroll and add any options you need:
    upload_2021-3-23_21-21-58.png
     
    Last edited: Mar 24, 2021
  5. ScottKane

    ScottKane

    Joined:
    Aug 24, 2019
    Posts:
    78
    Awesome dude, thanks
     
  6. massivebacon

    massivebacon

    Joined:
    Apr 24, 2014
    Posts:
    27
    Is there anywhere we can follow this and know if it's in or not?
     
  7. l33t_P4j33t

    l33t_P4j33t

    Joined:
    Jul 29, 2019
    Posts:
    232
    how do you know so much forbidden unity knowledge
    im too noob for any of this
     
    Last edited: Mar 29, 2021
  8. xoofx

    xoofx

    Unity Technologies

    Joined:
    Nov 5, 2016
    Posts:
    363
    I believe when 2021.2 will be available as a beta later this year.
     
  9. jasonboukheir

    jasonboukheir

    Joined:
    May 3, 2017
    Posts:
    73
    SugoiDev likes this.
  10. Lhawika

    Lhawika

    Joined:
    May 27, 2015
    Posts:
    52
    This looks nice !
    What about the support for code generators ?
     
  11. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    809
    What about default interfaces?
     
  12. TheZombieKiller

    TheZombieKiller

    Joined:
    Feb 8, 2013
    Posts:
    199
    Those require runtime support, so you won't be able to use them with just a Roslyn upgrade (which has just shipped in the latest 2021 alpha).
     
  13. Lhawika

    Lhawika

    Joined:
    May 27, 2015
    Posts:
    52
    Well I'm sad...
    I wanted to use the new "function pointers", but it appears that just only adding such a function pointer makes the editor silently crash:
    "public delegate*<int, bool> functionPointer;"

    Do I have to do something special in order to make them work ? did you test that feature before releasing ?

    Bug repported: (Case 1326082)
     
    TheZombieKiller likes this.
  14. TheZombieKiller

    TheZombieKiller

    Joined:
    Feb 8, 2013
    Posts:
    199
    I'm only able to reproduce this crash if the field is serialized (meaning it's either a public field or a private/protected/internal field with a [SerializeField] attribute). Adding [NonSerialized] to the field causes the crash to go away. Serializing a function pointer doesn't make much sense so I assume this is just a case of Unity not filtering them out properly.
     
  15. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,036
    @exkise Thanks for the bug report.

    @TheZombieKiller Thanks for the clarification.

    We expect FunctionPointer to be fully supported, so we will correct this issue.
     
  16. Lhawika

    Lhawika

    Joined:
    May 27, 2015
    Posts:
    52
    I tried that, and sadly it doesn't work for me.
    I show you here a context where I wish to use function pointers.
    I changed the code so that it would compile without my whole code (there's many more parameters to the function pointer in the real code, but it doesn't look like it affects the bug anyway).
    The bug reproduces with the following code: a struct which is neither serializable nor intented to be serialized (in fact there's no reference to it in the code I use to test the bug).

    I tried adding [NonSerialized] on the function pointer field, with no effect.

    Code (CSharp):
    1. public unsafe struct DataMatcher : IEquatable<DataMatcher>
    2. {
    3.     public int tokenId;
    4.     public delegate*<int, bool> function;
    5.  
    6.     public DataMatcher(in int tokenId, in delegate*<int, bool> function)
    7.     {
    8.         this.tokenId = tokenId;
    9.         this.function = function;
    10.     }
    11.  
    12.     public bool Match(int i)
    13.     {
    14.         return function(i);
    15.     }
    16.  
    17.     public bool Equals(DataMatcher other)
    18.     {
    19.         return tokenId.Equals(other.tokenId);
    20.     }
    21. }
     
    TheZombieKiller likes this.
  17. TheZombieKiller

    TheZombieKiller

    Joined:
    Feb 8, 2013
    Posts:
    199
    I did a bit more testing, and your example no longer crashes if the type is changed into a class. So it appears that the crash-causing cases are:
    • Serializing a function pointer (eg, by storing it in a MonoBehaviour, ScriptableObject or [Serializable] class)
    • Defining a struct containing a function pointer
    Other than changing the type into a class, I found that you can also use a property to work around the crash:
    Code (CSharp):
    1. public unsafe struct DataMatcher : IEquatable<DataMatcher>
    2. {
    3.     public int tokenId;
    4.  
    5.     void* pFunction;
    6.  
    7.     public delegate*<int, bool> function
    8.     {
    9.         get => (delegate*<int, bool>)pFunction;
    10.         set => pFunction = value;
    11.     }
    12.  
    13.     public DataMatcher(in int tokenId, in delegate*<int, bool> function)
    14.     {
    15.         this.tokenId = tokenId;
    16.         pFunction    = function;
    17.     }
    18.  
    19.     public bool Match(int i)
    20.     {
    21.         return function(i);
    22.     }
    23.  
    24.     public bool Equals(DataMatcher other)
    25.     {
    26.         return tokenId.Equals(other.tokenId);
    27.     }
    28. }
     
    jasonboukheir and NotaNaN like this.
  18. Lhawika

    Lhawika

    Joined:
    May 27, 2015
    Posts:
    52
    Thank you for researching this !
    Changing for a class will not do in my case as I want the type to be unmanaged, but using a property seems to be a valid workaround until this is fixed.
     
  19. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    809
    Ye, uh, so what's the status on that?
    Or NET Core libraries?
     
    Last edited: Apr 1, 2021
  20. jasonboukheir

    jasonboukheir

    Joined:
    May 3, 2017
    Posts:
    73
    just noting that I've tried the C# 9 function pointer in my Burst Job and I'm getting a
    "Burst error BC1054: Unable to resolve type `method System.Int32 *(System.Int32). Reason: Unknown.". Filed a bug (Case 1326456)

    I'm pretty much just casting an
    IntPtr
    ->
    delegate*<int, int>
    and the error is appearing.

    Here's the failing job:
    Code (CSharp):
    1. [BurstCompile]
    2. public unsafe struct CallFuncJob : IJob
    3. {
    4.     [ReadOnly]
    5.     [NativeDisableUnsafePtrRestriction]
    6.     public IntPtr FuncPtr;
    7.  
    8.     [ReadOnly]
    9.     public NativeArray<int> Input;
    10.  
    11.     [WriteOnly]
    12.     public NativeArray<int> Output;
    13.  
    14.     public void Execute()
    15.     {
    16.         Output[0] = ((delegate*<int, int>)FuncPtr)(Input[0]);
    17.     }
    18. }

     
  21. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,455
    how likely is at the moment that this is going to happen @xoofx ?
     
    SamOld, MilenaRocha, Qbit86 and 3 others like this.
  22. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,036
    Hey all, I'm happy to mention that support for default interface methods is available in Mono and IL2CPP in Unity version 2021.2.0a21.
     
  23. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    833
    Would it be backported to 2020? Can't use 2021 because we're using DOTS.
     
  24. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,624
    wow
     
  25. CaseyHofland

    CaseyHofland

    Joined:
    Mar 18, 2016
    Posts:
    251
    Default Interfaces relies on all kinds of new compiler stuff that @JoshPeterson and his team are adding in 2021.2, so no.

    Speaking of, can't wait for .NET 5 / .NET 6!
     
    MilenaRocha and Qbit86 like this.
  26. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,036
    No, we won't be back porting this.
     
    cyriaca and jGate99 like this.
  27. Jorhoto

    Jorhoto

    Joined:
    May 7, 2019
    Posts:
    98
    Great! Thanks! Super! :):)
     
  28. Foriero

    Foriero

    Joined:
    Jan 24, 2012
    Posts:
    572
    I'm a bit late into this thread. Is C#8 supported in 2021.2.21> ? Thanks, Marek.
     
  29. TheZombieKiller

    TheZombieKiller

    Joined:
    Feb 8, 2013
    Posts:
    199
    Yes, C# 8 has been supported since 2020.2.0a12, and newer versions now have partial support for C# 9.
     
    Last edited: Jun 16, 2021
  30. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    591
    C# 8 is available since 2020.2
     
    TheZombieKiller, cyriaca and Qbit86 like this.
  31. TheZombieKiller

    TheZombieKiller

    Joined:
    Feb 8, 2013
    Posts:
    199
    You're right, I didn't even notice I made that typo.
     
  32. Jorhoto

    Jorhoto

    Joined:
    May 7, 2019
    Posts:
    98
    C# 8 is not really supported in 2020.2, since it is missing a core feature: "Default Interfaces" which is available (as stated above) in 2021.2.0a21 version.
     
  33. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    591
    I installed 2021.2a21, but have the error: Target runtime doesn't support default interface implementation.
     
  34. VolodymyrBS

    VolodymyrBS

    Joined:
    May 15, 2019
    Posts:
    148
    based on this answer https://forum.unity.com/threads/unity-future-net-development-status.1092205/page-3#post-7225384 I assume that BCL not updated to NET Standard 2.1 yet. So compiler does not know that runtime support default interface implementation.
    Compiler detect that runtime support this feature by checking if
     System.Runtime.CompilerServices.RuntimeFeature.DefaultImplementationsOfInterfaces
    exists.
     
    Jorhoto and Qbit86 like this.
  35. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,036
    What is the C# compiler command line? It should be present in the editor log file.
     
  36. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,036
    You are correct that the reference assemblies don't get support .NET Standard 2.1, but the BCL implementations for IL2CPP and Mono both support default interface methods, and the C# compiler should be able to detect that.
     
    VolodymyrBS likes this.
  37. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    8,688
    I see the same (Target runtime doesn't support default interface implementation).

    You mean this?
    ##### CommandLine
    "C:\Program Files\Unity\Hub\Editor\2021.2.0a21\Editor\Data\NetCoreRuntime\dotnet.exe" exec "C:/Program Files/Unity/Hub/Editor/2021.2.0a21/Editor/Data/DotNetSdkRoslyn/csc.dll" /nostdlib /noconfig /shared "@ Library/Bee/artifacts/1900b0aEDbg.dag/com.lurkingninja.eclypsia.rsp"

    Note: There is no space between the @ and the Library, but the forum tries to link to a forum user so added.

    ##### Contents of Library\Bee\artifacts\1900b0aEDbg.dag\com.lurkingninja.eclypsia.rsp
    -target:library
    -out:"Library/Bee/artifacts/1900b0aEDbg.dag/com.lurkingninja.eclypsia.dll"
    -refout:"Library/Bee/artifacts/1900b0aEDbg.dag/com.lurkingninja.eclypsia.ref.dll"
    -define:UNITY_2021_2_0

    [...]

    -langversion:9.0
    /deterministic
    /optimize-
    /debug: portable
    /nologo
    /RuntimeMetadataVersion:v4.0.30319

    [...]

    ##### Output
    Assets\_Eclypsia\Code\Interfaces\ILoopInterface.cs(5,14): error CS8701: Target runtime doesn't support default interface implementation.
    *** Tundra build failed (1.19 seconds), 1 items updated, 845 evaluated
    Assets\_Eclypsia\Code\Interfaces\ILoopInterface.cs(5,14): error CS8701: Target runtime doesn't support default interface implementation.
     
    Last edited: Jun 17, 2021
  38. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,036
    Thanks - everything looks fine there. Can you submit a bug report with a project that reproduces this issue? I'm not sure what is happening.
     
  39. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    8,688
    I made a minimal reproduction project.

    Case number: 1343996
     
    TextusGames, Qbit86 and JoshPeterson like this.
  40. broots

    broots

    Joined:
    Dec 20, 2019
    Posts:
    54
    I am getting the same issue as the others,

    Code (CSharp):
    1. ##### CommandLine
    2. "C:\Program Files\Unity\Hub\Editor\2021.2.0a21\Editor\Data\NetCoreRuntime\dotnet.exe" exec "C:/Program Files/Unity/Hub/Editor/2021.2.0a21/Editor/Data/DotNetSdkRoslyn/csc.dll" /nostdlib /noconfig /shared "@Library/Bee/artifacts/1900b0aEDbg.dag/Assembly-CSharp.rsp"
    3. ##### Contents of Library\Bee\artifacts\1900b0aEDbg.dag\Assembly-CSharp.rsp
    4. -target:library
    5.  
    6. -out:"Library/Bee/artifacts/1900b0aEDbg.dag/Assembly-CSharp.dll"
    7.  
    8. -refout:"Library/Bee/artifacts/1900b0aEDbg.dag/Assembly-CSharp.ref.dll"
    9.  
    All of my coworkers are having the same problem as well.

    Cheers,
    Z.
     
  41. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,036
    Thanks for the details - we will investigate this.
     
  42. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    8,688
  43. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,036
    Hey all, so it seems that I spoke too soon. While we do have all of the internal support for default interface methods in 2021.2.0a21 and later, the C# code is Unity is still compiling against the .NET Standard 2.0 or .NET Framework 4.7.1 references assemblies.

    Neither set of reference assemblies supports default interface methods, so Roslyn (correctly) produces the error shown in this bug report:

    https://issuetracker.unity3d.com/issues/default-interface-implementation-error

    I apologize that I mislead everyone, I should have checked the end-to-end behavior before posting here.

    The good news is that changes to make this work properly are on the way. I'll update here when default interface methods really are available in a Unity version.
     
  44. SamOld

    SamOld

    Joined:
    Aug 17, 2018
    Posts:
    302
    This is how all of the Unity technical communication should be. It's clear and explains the setback in a way that devs understand and can sympathise with. Thanks for the update @JoshPeterson.
     
  45. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,036
    Default interface methods support (and full support for C#8) will be available in Unity 2022.1.0a3. It will also be available in a 2021.2 beta release. I'll update here when I know the version.
     
  46. OndrejP

    OndrejP

    Joined:
    Jul 19, 2017
    Posts:
    219
    @JoshPeterson thank you for communicating like this to us, I wish all other teams did it like this as well.
    It's good to know what's going on behind the scene and what we can expect.
     
  47. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,036
    Default interface methods and C# 8 support will be available in 2021.2b6 as well.
     
  48. TechnoPriest

    TechnoPriest

    Joined:
    Dec 9, 2017
    Posts:
    1
    Thanks for the update! Do you happen to have a estimated release data for b6?
     
  49. CaseyHofland

    CaseyHofland

    Joined:
    Mar 18, 2016
    Posts:
    251
    I suppose then that this thread is coming to its end, while the Unity Future .NET Development Status thread has only just begun.

    Unless you have even more threads that I don't know about :D
     
    Wollbobaggins likes this.
  50. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,036
    I don't know sorry - I would expect within a week or so. But I'm not directly involved in the release process.
     
    Walter_Hulsebos likes this.
unityunity