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.
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!
Am I getting my hopes up too high? Are there concrete plans to have Roslyn working in the upcoming release or is this just preparing the API for a distant future update?
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.
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.
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:
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.
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.
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.
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.
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 ?
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).
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.