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

Mono Upgrade Roslyn support in the Unity editor

Discussion in 'Experimental Scripting Previews' started by liortal, Dec 1, 2016.

  1. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    Hi,

    I'd like to use Roslyn inside the Unity editor (to create code analyzers).
    Is the editor using a different version of Mono than the runtime version ?

    Suppose that Roslyn is built using the latest available Mono version, should that work in the experimental build ?
     
    rakkarage likes this.
  2. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    rakkarage likes this.
  3. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    I watched this talk. Which is exactly the reason i brought up this topic.
    I'd like to execute that inside the editor, if possible. Just wondering if that will work with the new mono.
     
    Last edited: Dec 3, 2016
  4. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    The current experimental scripting builds we have released with the new Mono runtime are not using Roslyn, they still use mcs as the C# compiler.

    No the editor and the runtimes (players) use the same version of Mono.

    We do plan to support Roslyn though, Xamarin has just merged changes into its master branch to support Roslyn, so we should be able to get that support with an upgraded version of Mono in the future. We're unsure yet whether the first release of Unity with the Mono runtime upgrade will use Roslyn though - there is some associated tooling in Unity that needs to be modified to work with the debug file format (ppdb) that Roslyn uses.

    If you want to try this on your own, I think it will work, even with the released versions of Unity and the old Mono runtime. In that case you'll need restrict the C# features to C# 4 I think (although I've heard of people using C# 6 language-only features). None of this is something we can support yet, but it might be fun to try!
     
    rakkarage and MV10 like this.
  5. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
  6. JoshPeterson

    JoshPeterson

    Unity Technologies

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

    We're not shipping Roslyn in 5.6. I think this is here is the documentation for the Windows Store platform, which has been using the Microsoft C# compiler for some time.

    We are however planning to ship Roslyn in the future. Xamarin is working to replace their C# compiler with Roslyn, and Unity will get that as part of a future Mono upgrade. Note also that the Mono upgrade we have been shipping preview builds for on this forum section does not use the Roslyn compiler.
     
    Xarbrough likes this.
  7. dadude123

    dadude123

    Joined:
    Feb 26, 2014
    Posts:
    789
    Is there any way to currently (and by that I mean 5.5.0f3) use roslyn to compile some simple c# code at runtime and use it?
     
  8. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
  9. dadude123

    dadude123

    Joined:
    Feb 26, 2014
    Posts:
    789
    I was aware that I can just invoke the compiler (any compiler really) myself.
    But I would like to use roslyn as a dll reference! To analyze code before I compile it (also with roslyn).

    That would allow me to use C# as a scripting language in my game.
    Without roslyn and syntax tree analysis it would be too difficult to ensure that a script only contains what I want to be allowed (essntially I'd be creating my own DSL, a small subset of c#).

    I already did that with success in other projects but I'm not sure how to use roslyn in unity because of the different .net runtime requirements.
     
    Last edited: Feb 7, 2017
    Qbit86 and Chetic like this.
  10. Chetic

    Chetic

    Joined:
    Oct 28, 2016
    Posts:
    4
    Just want to add that I am also eagerly awaiting Roslyn support. What I want to see is VR-based C# IDEs to more intuitively represent code paths, dependencies etc and let you use all that unused spatial cognition power. Code inherently just isn't structured at all as we write it today (top to bottom like a flat piece of paper) and I think there's a lot to gain there in terms of productivity. Prototyping something like that in Unity seems to be the quickest way to get there at the moment.

    If it's any help, I've tried referencing Roslyn DLLs but got stuck at the following step:
    https://forum.unity3d.com/threads/v...microsoft-codeanalysis-workspaces-dll.438459/
     
    MattMaker and Qbit86 like this.
  11. dadude123

    dadude123

    Joined:
    Feb 26, 2014
    Posts:
    789
    The experimental release works ok-ish, but I can't really test it much more because I need to complete my project.
    What I did now was some wonky hybrid approach:
    I used the mono Evaluator class to execute code, and to check the code and transform it I made a .NET 4.6 executable that I just call from the commandline (Process.Start).
    That exe takes care of transforming the code a bit, checking its references, and producing output for .net 2.0.

    It works but its far from optimal :(

    Having C# as a "scripting" language for my UI, Monsters, Abilities, ... is just too convenient.
     
  12. MerryYellow

    MerryYellow

    Joined:
    Sep 6, 2018
    Posts:
    22
    I have achieved using Roslyn with MSBuild in the editor with CLR hosting. Since I host my own .NET runtime, it works in older versions of Unity as well. It was not a very smooth experience but still doable. Here is the tool that Roslyn to optimize C# script files.
     
  13. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    Note that Unity 2018.3 ships with Roslyn as the C# compiler when the .NET 4.x Equivalent scripting runtime is used.
     
    nxrighthere likes this.
  14. MerryYellow

    MerryYellow

    Joined:
    Sep 6, 2018
    Posts:
    22
    @JoshPeterson

    Will we be able to use Roslyn analyzers and code fixes in the editor (not in Visual Studio) in the future?

    Also getting the workspace from a helper class like VisualStudioWorkspace would be very useful for script tinkerers like me. Still having some problems when trying to create workspace on my own.
     
  15. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    I think that it is possible to load Roslyn assemblies in the Unity editor. I don't think that is impacted by the fact that Unity is using the Roslyn compiler though.
     
  16. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    i am actually a bit confused by the term Roslyn - is that the new (well, not so new anymore) compiler for C# / Mono or is it also a framework (set of libraries) that are used for creating code analyzers and other code tools ?
     
  17. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    In Unity 2018.3 we're using Roslyn, the C# compiler (really csc.exe), to compile all C# script code in each Unity project. We've not done anything to add support for the code analyzers or other tools (although they should work).
     
  18. MerryYellow

    MerryYellow

    Joined:
    Sep 6, 2018
    Posts:
    22
    Both. It's a collection APIs to compile .NET code, and is provided by Microsoft. It's official name is "The .NET Compiler Platform SDK", which gives you an idea of what it is for. Visual Studio 2015 & 2017 and Unity 2018.3 uses it for compiling in the background.

    What makes it special is that it is fully implemented in C# and is open source. So that you can access to syntax trees, semantic model and IL emitting, and this gives you a lot of freedom. You can implement static code analyzers, code generators etc without relying on Visual Studio. Burst compiler is a good example of what Roslyn made possible.
     
    liortal and rakkarage like this.
  19. MFKJ

    MFKJ

    Joined:
    May 13, 2015
    Posts:
    264
    From Unity2020.2, Roslyn analyzer is fully supported.
     
    MihaPro_CarX likes this.