Runtime C# Scripting - Embedding the .NET Roslyn Compiler in Unity @WorldOfZeroDevelopment
Runtime C# Scripting - Embedding the .NET Roslyn Compiler in Unity  @WorldOfZeroDevelopment
Uploaded February 2020 | Updated November 2026, 1 hour ago
I've been experimenting with embedding Roslyn (the .NET compiler) into Unity. Here is what I've built so far.

Roslyn is a compiler for .NET that includes a package for scripting and expressions. This allows you to write C# code into standard strings and then compile and execute the code in memory. We can extend this further by integrating it with Unity and exposing the default types from our game. The end result of all of this is the ability to write basic strings with C# code in them that we can evaluate later to interact with any part of our system we choose. There are a number of potential use cases for this from debugging your compiled game with an embedded terminal to creating a game that teaches C#.

This solution does open up some potential for running code that should not be run. If you're trying this, please use caution when considering allowing code from the internet or other users to be run. This can have severe consequences if abused. Someone once said great power demanded great responsibility and that's the case here.

## Some Examples

Calculate 2³ power using `System.Math`:

```csharp
result = CSharpScript.EvaluateAsync[double]("System.Math.Pow(2,3)").GetAwaiter().GetResult();
```

Find a `Camera` object in the current scene:

```csharp
cameraObject = await CSharpScript.EvaluateAsync[GameObject]("GameObject.FindObjectsOfType[Camera]().First().gameObject",
ScriptOptions.Default.WithImports( // Import namespaces to simplify scripts
"System",
"System.Collections.Generic",
"System.Linq",
"UnityEngine"
).AddReferences( // Include additional assemblies
typeof(System.Linq.Enumerable).Assembly,
typeof(UnityEngine.Transform).Assembly
));
```

The Roslyn Scripting API is documented on GitHub: github.com/dotnet/roslyn/wiki/Scripting-API-Samples

You can find the Nuget package here: nuget.org/packages/Microsoft.CodeAnalysis.CSharp.Scripting


Join the World of Zero Discord Server: discord.gg/hU5Kq2u
Runtime C# Scripting - Embedding the .NET Roslyn Compiler in UnityPlatforming and Other Adventures from Live StreamTrie, Trie Again - A String Search Data StructureRing to Ripples - Ripple in Shader Graph - Part 3Creating a Stepped Gradient Shader in UnityLearning Unitys XR Input SystemUsing PowerShell to Create a Symbolic Link on WindowsBuilding Boids in Unity 3DWriting a Snow Covered ShaderUsing C#s Yield KeywordUsing Python and Argparse to Build Quick ScriptsRedesigning our C# Scripting in Unity Tool
World of Zero |

Runtime C# Scripting - Embedding the .NET Roslyn Compiler in Unity

SHARE TO X SHARE TO REDDIT SHARE TO FACEBOOK WALLPAPER