Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Unity C# 8 support

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

  1. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,936
    I agree - let's try to push additional discussion to that thread. Thanks!
     
    Kokowolo likes this.
  2. TextusGames

    TextusGames

    Joined:
    Dec 8, 2016
    Posts:
    429
    Default interfaces are working in 2021.b.6

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class NewBehaviourScript : MonoBehaviour
    4. {
    5.     private void Start()
    6.     {
    7.       var someInterface = (ISomeInterface) new SomeInterfaceInheritor();
    8.       someInterface.DefaultMethod();
    9.     }
    10. }
    11.  
    12. public interface ISomeInterface
    13. {
    14.     public sealed void DefaultMethod()
    15.     {
    16.         Debug.Log("Calling public not overridable default method");
    17.         OnDefaultMethod();
    18.     }
    19.  
    20.     protected void OnDefaultMethod();
    21. }
    22.  
    23. public class SomeInterfaceInheritor : ISomeInterface
    24. {
    25.     // public void DefaultMethod() // This is not used because default method is sealed if it is not sealed this will be called instead
    26.     // {
    27.     //     Debug.Log("Try to override default method");
    28.     // }
    29.  
    30.     void ISomeInterface.OnDefaultMethod()
    31.     {
    32.        Debug.Log("Calling protected inherited method");
    33.     }
    34. }
     
  3. TomPo

    TomPo

    Joined:
    Nov 30, 2013
    Posts:
    86
    How did you manage to do this?
    I downloaded 2021.2.0b10 and in Visual Studio still got the same errors about Interfaces and methods.
    What steps did you take to make it work and what version of Visual Studio you are using?
     
  4. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,936
    Do you see the C# compilation error in Visual Studio, the Unity Editor, or both?
     
  5. TomPo

    TomPo

    Joined:
    Nov 30, 2013
    Posts:
    86
    In Both.

    Visual Studio is showing the error that Interface can't implement methods.
    And Unity showing error that methods are not implemented even when they are.

    Code (CSharp):
    1. public interface IMyInterface {
    2.  
    3.     public sealed void DefaultMethod() {
    4.         Debug.Log("Calling public not overridable default method");
    5.         OnDefaultMethod();
    6.     }
    7.  
    8.     protected void OnDefaultMethod();
    9.  
    10. }
    Code (CSharp):
    1. public class MyClass : MonoBehaviour, IMyInterface {
    2.     public void DefaultMethod() {
    3.         Debug.Log("");
    4.     }
    5.  
    6.     public void OnDefaultMethod() {
    7.         Debug.Log("");
    8.     }
    9. }
    Assets\MyClass.cs(5,39): error CS8704: 'MyClass' does not implement interface member 'IMyInterface.OnDefaultMethod()'. 'MyClass.OnDefaultMethod()' cannot implicitly implement a non-public member.
     
    Last edited: Sep 9, 2021
  6. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,936
    Thanks for the details. I think there are two different issues going on here.

    In Visual Studio, I suspect that the Visual Studio code editor package is generating the .csproj with an older language version. Can you look in the generated .csproj file for the <LangVersion> tag?

    Also, note that you will need Visual Studio 2019 - earlier versions won't work with default interface methods.

    In Unity, the default interface method is working, but I think this is a valid C# compiler error. You can address it by making OnDefaultMethod public in IMyInterface, for example.
     
    NotaNaN likes this.
  7. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    792
    Protected Interface Members Must be Implemented Explicitly:
    Code (CSharp):
    1. public class MyClass : MonoBehaviour, IMyInterface {
    2.     public void DefaultMethod() {
    3.         Debug.Log("");
    4.     }
    5.     public void IMyInterface.OnDefaultMethod() {
    6.         Debug.Log("");
    7.     }
    8. }
     
    JoshPeterson likes this.
  8. TomPo

    TomPo

    Joined:
    Nov 30, 2013
    Posts:
    86
    Thanks! Problem solved, Visual Studio 2019 did the trick.
    Finally, defined methods inside Interfaces ! :)
     
    TextusGames likes this.
  9. TextusGames

    TextusGames

    Joined:
    Dec 8, 2016
    Posts:
    429
    I was using Rider :)
     
    Anthiese likes this.
  10. sand_lantern

    sand_lantern

    Joined:
    Sep 15, 2017
    Posts:
    210
    So, some people are suggesting that at least some C# 8/9 features are supported, but it appears that Tuple Switches are not, unless I'm doing something wrong.

    I have a little function that looks like this:
    Code (CSharp):
    1. private (int index, Side nodeDirection) NearbyNode (in Vector2 position)
    2. {
    3.     (int index, Side nodeDirection) = ClosestNode (position);
    4.     switch (index, nodeDirection)
    5.     {
    6.     case (< 2, Side.Right):
    7.     case (< 2, Side.Left):
    8.     case (2, Side.Right):
    9.         return (index + 2, Side.Left);
    10.     case (2, Side.Left):
    11.     case (> 2, Side.Right):
    12.     case (> 2, Side.Left):
    13.         return (index - 2, Side.Left);
    14.     case ( _, _):
    15.         throw new InvalidEnumArgumentException ("Side must be left or right. Unexpected value of " + nodeDirection);
    16.     }
    17. }
    Visual Studio thinks it all looks good, but I'm getting an error in Unity 2020.3.16f1
    Assets\Scripts\Actors\AIBehaviorTree\BossBehaviorComponent.cs(125,13): error CS1002: ; expected


    Is this not a supported feature? It's sometimes hard to figure out which language features are available and which aren't.
     
  11. VolodymyrBS

    VolodymyrBS

    Joined:
    May 15, 2019
    Posts:
    150
    This code is using C# 9 feature (comparison operator in pattern matching). C# 9 is supported in Unity 2021.2 and higher
     
    Walter_Hulsebos and sand_lantern like this.
  12. watsonsong

    watsonsong

    Joined:
    May 13, 2015
    Posts:
    555
    How about this? It still warning on 2021.2.18f1
     
  13. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,936
    Sorry, I'm having trouble finding which feature you are referring to. Can you mention it explicitly?
     
  14. watsonsong

    watsonsong

    Joined:
    May 13, 2015
    Posts:
    555
    It about the nullable support and this warning:
    warning CS8669: The annotation for nullable reference types should only be used in code within a ‘#nullable’ annotations context

    I can add '#nullable enable' and '#nullable disable' in the source code to disable this warning. I think it may support open nullable support in asmdef file.
     
  15. rarepop99

    rarepop99

    Unity Technologies

    Joined:
    Sep 5, 2019
    Posts:
    54
    We don't plan on adding new features to asmdef files, as we plan to move away from them in the future

    You can either create a csc.rsp file with /nowarn:8669 or -nowarn:8669 or have the same message in the Additional Compiler Arguments list in the Project Settings-> Player, which are project-wide to ignore it in a faster way. :)
     
    Last edited: Apr 6, 2022
    Qbit86 and Kamyker like this.
  16. watsonsong

    watsonsong

    Joined:
    May 13, 2015
    Posts:
    555
    What I want is to build a package and it could put into any unity project. So the csc.rsp or player settings not work? Is the 'csc.rsp' work in a package or just for a single assembly?
    BTW, you mean the 'asmdef' will be deprecated? How about the Assembly and AssemblyBuilder, What the substitution?
     
  17. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    1,166
    This new to me. What was happen to asmdef and what would be alternative or are there any problem lead to this decision?
     
    Petr777 and Qbit86 like this.
  18. elcionap

    elcionap

    Joined:
    Jan 11, 2016
    Posts:
    138
    They are changing to SDK style csproj. There are more info in the 'Unity Future .NET Development Status' topic and here
    , sorry to not be able to pinpoint the exacly response link and timestamps.

    []'s
     
  19. ScottKane

    ScottKane

    Joined:
    Aug 24, 2019
    Posts:
    81
    A csc.rsp file is there to tell the compiler specific instructions related to what it should and shouldn't care about. As far as I know, a single csc.rsp file will cover ANY code compiled in your project. (3rd party and Unity packages come in source code form from UPM, so the compiler will apply the rules you set to that code too).
     
  20. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,476
    c# 10.0?
     
  21. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    1,166
    Thank you very much. This video this private so I cannot see it however I think I understand a glimpse of what you describe
     
  22. SugoiDev

    SugoiDev

    Joined:
    Mar 27, 2013
    Posts:
    395
    It was not private yesterday. I wonder what happened.

    Edit: the title was "The future of .NET and Unity | Unity at GDC 2022"
     
    Thaina likes this.
  23. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    792
    Was it the video with the bad audio? maybe it was re-uploaded
     
    SugoiDev likes this.
  24. SugoiDev

    SugoiDev

    Joined:
    Mar 27, 2013
    Posts:
    395
    Ah, now that you mention, it was!
    Audio was out of sync on left/right channel, so it had a weird echo.

    That's probably the reason.
     
  25. Cathei

    Cathei

    Joined:
    Dec 18, 2012
    Posts:
    7
    About interface default implementation, I encountered crash if I call 'new Test()' with below code.
    Seems like it only happens when number of generic argument of ITestBase is less than ITest.

    It is silent crash, editor log says "VAR 1 (T2) cannot be expanded in this context with 1 instantiations"

    Code (CSharp):
    1.  
    2. public interface ITestBase<T>
    3. {
    4.     internal void Process(T x);
    5. }
    6.  
    7. public interface ITest<T1, T2> : ITestBase<T1>
    8. {
    9.     void ITestBase<T1>.Process(T1 x) { }
    10. }
    11.  
    12. public class Test : ITest<int, int> { }
    13.  
    It happens in 2021.2.19f1.
     
    NotaNaN likes this.
  26. NotaNaN

    NotaNaN

    Joined:
    Dec 14, 2018
    Posts:
    325
    I just tested it myself and I can confirm it happens in 2022.1.0b12 as well. (You might want to test it on the latest beta version too, but I betcha it will break there as well).

    Have you made a bug report for this issue? (If you have not, you can be that here)

    I have had similar problems with using Default Interface Implementation within generic interfaces in Unity, but I have not been able to reproduce my issue across multiple machines yet...

    Here's me hoping your problem fixes my problem. ;)
     
  27. rarepop99

    rarepop99

    Unity Technologies

    Joined:
    Sep 5, 2019
    Posts:
    54
    csc.rsp files apply to the ASMDEF they exist next to. If your code doesn't have any asmdefs, then yes it will be applied to any code under "Assets"
     
    OndrejP likes this.
  28. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    1,166
    Sorry to bring this up on this old post but since C# 8 is fully support since 2021.2

    Is it include IAsyncEnumerable and await foreach/async stream ? And are there limitation to use it in any platform?
     
  29. simon-ferquel-unity

    simon-ferquel-unity

    Unity Technologies

    Joined:
    Apr 1, 2021
    Posts:
    68
    As user code is compiled with .net standard 2.1 support, IAsyncEnumerable and `async foreach` constructs are available.
    IIRC, WebGL is the only platform where Unity does not support threading. With WebGL you will still be able to using async/await and IAsyncEnumerable, but only for async IO (not for leveraging background threads).
     
    Thaina likes this.
  30. borkom_unity

    borkom_unity

    Joined:
    Jul 22, 2021
    Posts:
    13
    Sorry for necro post i am on 2021.3.21f its an non beta i think, should it work on this version too or only beta ones
     
  31. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,936
    It will work one any versions after the beta, so it should work for you.
     
  32. borkom_unity

    borkom_unity

    Joined:
    Jul 22, 2021
    Posts:
    13
    ye it works, my bad :(
     
  33. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    838
    @JoshPeterson hope you dont mind me asking in this thread;

    Are there plans to support the UnsafeAccessor attribute thats part of .net 8? I just tried in 2023.2 and it couldnt be found, i think it would be tremendously useful for some of the hacks we have to do.
     
  34. TheZombieKiller

    TheZombieKiller

    Joined:
    Feb 8, 2013
    Posts:
    266
    It's possible to implement UnsafeAccessor manually without runtime support if you write an IL post processor for it, you just need to make sure to add a
    SecurityPermissionAttribute
    to the assembly with
    SkipVerification
    set to true, which gives you roughly the same effect as
    IgnoresAccessChecksToAttribute
    does in CoreCLR, but in Mono instead (and therefore Unity).
     
  35. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,936
    We do plan to support with with .NET 8 in Unity, but you are correct, this is not in 2023.2. I don't have an ETA to share yet about .NET 8 support, so I can't provide more information right now.
     
  36. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    838
    Thanks for your prompt reply!