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
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

C# Compiler Existing C# 6 users

Discussion in 'Experimental Scripting Previews' started by makeshiftwings, Jul 23, 2016.

  1. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Hello Unity, just wanted to make sure you all were aware of alexzzzz's existing wrapper for the Roslyn compiler:

    https://bitbucket.org/alexzzzz/unity-c-5.0-and-6.0-integration/src

    and SaladLab's Unity C# 6 incremental compiler:

    https://github.com/SaladLab/Unity3D.IncrementalCompiler

    I have not used the incremental one but I've been using alexzzzz's wrapper for a few months now and have had no issues with it. FYI, both Visual Studio and Resharper can currently be set to use C# 6 but target .NET 3 without any issues.

    I hope Unity gives Roslyn some serious consideration... I know you guys like to make custom versions of everything but it's good to leverage existing solutions where possible. I still don't really understand why you need to write your own .NET compiler when IL2CPP is supposed to handle IL (it's in the name!) and so should be compiler-agnostic.
     
    rakkarage likes this.
  2. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    @makeshiftwings

    Thanks for the information. We've actually looked those projects a bit - they look pretty nice.

    To clarify, we're not shipping a custom C# compiler. We ship the Mono C# compiler, and in fact we're upgrading to the latest Mono compiler (the one released with Mono 4.4) for the next version of Unity. You can try out that compiler with a 5.3.5p8-compatible build of Unity here:

    http://forum.unity3d.com/threads/upgraded-c-compiler-on-5-3-5p8.417363/

    Although that compiler has full C# 6 support, we've not enabled it yet since some other tools in the Unity ecosystem can't handle C# 6. We're working to update those tools, and we plan to support C# 6 when everything is ready.

    Indeed, IL2CPP is C#-compiler agnostic, so it really doesn't have any impact on the C# compiler upgrade.
     
  3. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    And I've just realized this thread was posted in the experimental scripting previews section already, so hopefully I did not just restart what Jonathan has already mentioned.

    @makeshiftwings Let me know if I've totally missed the point of your comments, sorry about that.
     
  4. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Ah ok, I got the impression you were making a branch of the Mono C# compiler specific to Unity. Though still, it sounds like the Mono C# Compiler is not going to be around for much longer, since Mono has been working to adopt Roslyn and will be dumping their own compiler eventually:

    http://tirania.org/blog/archive/2015/Jul-21.html

    Roslyn already works pretty well with the Mono runtime and it's going to be the main C# compiler for all platforms in the not too distant future, so it would seem wise to try to integrate Roslyn rather than the Mono compiler. Though I'm sure there are other factors that I might not be aware of.
     
    rakkarage likes this.
  5. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    @makeshiftwings

    Actually, we are planning to use the Roslyn compiler, via the Mono project! So as Mono moves to Roslyn for its C# compiler, at some point a stable version on Mono will be released that uses Roslyn. We plan to upgrade to that version of Mono (and future versions in general, not just that version), so Unity will get the Roslyn compiler.

    Internally, we've done some early testing with Roslyn, and the results are good. The various .NET runtimes Unity uses all seem to work with it (at least with our test cases).
     
  6. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Perfect! Thanks for the info. :) That's the direction I was hoping for.
     
  7. frank28

    frank28

    Joined:
    Dec 30, 2013
    Posts:
    13
    Really glad to hear the progress! Well done!
     
  8. 00christian00

    00christian00

    Joined:
    Jul 22, 2012
    Posts:
    1,035
    Just curious, why not go directly with Roslyn then?
     
    rakkarage likes this.
  9. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    There's at least one problem with Roslyn. It can't generate .mdb/.pdb debug information files on MacOS. .Mdb is not supported and .pdb is tied to Windows only.

    Roslyn can write "portable pdb" on Macs but it's a pretty new standard that is not compatible with the current tool chain. So, you can compile C# with Roslyn on MacOS if you want, but can not debug it.
     
  10. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    @00christian00

    Besides what @alexzzzz mentioned about portable pdbs, it was simply easier to integrate the new Mono compiler, as we already have an existing Mono compiler integration. That allowed us to focus on issues related to the actual compiler more so than the integration tasks, and get something shipped earlier.

    With that said, we're looking closely at C# 7 (and working with Microsoft on a few design issues). So depending on the time lines, we might see Roslyn integrated directly. But it is too early to talk about anything real now.
     
    rakkarage and 00christian00 like this.
  11. 00christian00

    00christian00

    Joined:
    Jul 22, 2012
    Posts:
    1,035
    While I wait for this to become license unrestricted I tried the one above with incremental build and it's saving a ton of time in compilation time, while there wasn't that much difference with normal roslyn.
    Can Mono also enable incremental builds?
    And Is this something that could be implemented also:
    https://www.infoq.com/news/2011/12/Mono-for-Android-4
    Uploading only a delta to the device would speed up testing A LOT!
     
    Last edited: Sep 6, 2016
  12. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    @00christian00

    The version of the Mono C# compiler we're shipping now doesn't have the same incremental capability that Roslyn does, so we can't ship that. But it is certainly something we're looking at. Likewise the partial package stuff for Android is pretty cool. It is not a current target for us, but I think as we move to the new Mono, this kind of thing will be much easier to integrate.
     
    00christian00 likes this.
  13. Lordinarius

    Lordinarius

    Joined:
    Sep 3, 2012
    Posts:
    94
    Is there any link that i can learn what benefits c# 6 will bring to us ?
     
  14. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
  15. Xtro

    Xtro

    Joined:
    Apr 17, 2013
    Posts:
    604
    Is there any update on C# 6+ from Unity Tech?

    Are we getting closer? This is 2017 now :)
     
  16. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    rakkarage likes this.
  17. Xtro

    Xtro

    Joined:
    Apr 17, 2013
    Posts:
    604
    I think you meant "you can use C# 6+".

    Well, that's great news. Thank you :)
     
  18. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    Yes, C#6 is what I meant, thanks!
     
    rakkarage likes this.
  19. Xtro

    Xtro

    Joined:
    Apr 17, 2013
    Posts:
    604
    OMG wait!! This is not just C# update. This is .NET upgrade!!!!! Can't belive this!!!
     
  20. Xtro

    Xtro

    Joined:
    Apr 17, 2013
    Posts:
    604
    I have been using C# 6 in Unity via pre-build dll's for a long time but now having the option of .NET 4.6!!! Can't belive it !!!!
     
    rakkarage likes this.
  21. Gru

    Gru

    Joined:
    Dec 23, 2012
    Posts:
    142
    Hi, any news on full integration of C# 6 in non-preview builds? Are there any plans to maybe ship this with Unity 2017?
     
  22. Ostwind

    Ostwind

    Joined:
    Mar 22, 2011
    Posts:
    2,804
    It's already included in the 2017.1 beta but the preview tag is probably going to stay for a while.
     
  23. Gru

    Gru

    Joined:
    Dec 23, 2012
    Posts:
    142
    OK, thanks @Ostwind.
     
  24. Sergey_Droba

    Sergey_Droba

    Joined:
    Nov 6, 2016
    Posts:
    52
    Hello,
    so i'm on Unity 2017.1 beta but it looks like c#6 features still not supported.
    How can i use them?
    Thank you!
     
  25. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    @Sergey_Droba

    Look at the Player Settings. There is an option named "Scripting Runtime Version". Change it to the value "Experimental (.NET 4.6 Equivalent)", then you should have C# support.
     
  26. Sergey_Droba

    Sergey_Droba

    Joined:
    Nov 6, 2016
    Posts:
    52
    Thank you for reply.
    Looks like it works now!
     
    JoshPeterson likes this.
  27. Pharan

    Pharan

    Joined:
    Oct 28, 2013
    Posts:
    102
    Is there a definitive list somewhere of C#6 syntax that currently works or doesn't work?

    I can use expression-bodied methods:
    Code (csharp):
    1. public string GetHello() => "Hello!"; // no problem
    but expression-bodied properties isn't working.
    Code (csharp):
    1. public string Hello { get => "Hello!"; } // compiler gets all sorts of confused.
    MonoDevelop shows a squiggly under the => operator.
    And the Unity console says:
    If I use just the shorthand getter...
    Code (csharp):
    1. public string Hello => "Hello!"; // confuses MonoDevelop but Unity is fine.
    MonoDevelop shows senseless squiggles all over the rest of the document, and the methods using that property, but it seems to compile and work correctly in Unity / at runtime.


    I checked if it was actually Mono thing. Wasn't terribly sure, but there was curiously no mention of expression-bodied properties here: https://developer.xamarin.com/guides/cross-platform/advanced/csharp_six/
     
  28. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    This is C# 7.0.
     
  29. Pharan

    Pharan

    Joined:
    Oct 28, 2013
    Posts:
    102
    Thanks! but what about the other syntax?
     
  30. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    @Pharan

    It should work. Maybe try Visual Studio for Mac, which is a newer version of MonoDevelop, and should have full C#6 support.