Script compilation with MSBuild (Experimental)
Compile scripts with MSBuild as an experimental alternative to the default Unity script compilation pipeline.
Read time 9 minutesLast updated 13 days ago
By default Unity compiles scripts with its own script compilation pipeline, which determines what gets compiled automatically based on folder names, file locations, and assembly definition (.asmdef) files. The default pipeline generates C# project (.csproj) files but they aren't authoritative. It also doesn't support NuGet natively, and references are resolved through references, plug-ins in the folder, and Unity packages.
.asmdefAssetsMSBuild (Microsoft Build Engine) is the build platform for .NET and Visual Studio and is available experimentally as an alternative script compilation pipeline. MSBuild differs from Unity's default script compilation pipeline in the following ways:
- MSBuild is an explicit system where what's compiled is determined by authoritative project (.csproj) and solution (.slnx) files that list files, references, and build targets.
- MSBuild supports NuGet natively and handles dependency resolution via explicit and
<PackageReference>entries.<ProjectReference>
There are some Unity-specific limitations to the support for MSBuild features. For more informaton, refer to MSBuild feature support in Unity
Enable script compilation with MSBuild
You can enable script compilation with MSBuild in one of the following ways:
-
Through the Editor Settings menu in the Unity Editor. Perform the following steps to activate MSBuild compilation through the Editor Settings menu:
- Go to Edit > .Project Settings
?
- Click on the Editor tab to open the Editor settings.
- Navigate to the Script Compilation section heading.
- Select the Enable MSBuild Compilation (Experimental) checkbox.
- Go to Edit >
-
By supplying the Editor command-line argument.
-enable-msbuild
In either case the Editor must be restarted for the change to take effect.
MSBuild files reference
When MSBuild compilation is enabled, the following files appear at the project root. In some cases you can edit these files and your changes are preserved. In other cases Unity controls the file contents and changes you make are overwritten. For further details, refer to the table:
File | Editable | Description |
|---|---|---|
| Yes (write-once) | Unity creates this file if missing and never overwrites it. You can add elements such as PropertyGroups, targets, and imports to this file and your changes are preserved. |
| Partially. | User keys and extra |
| Partially. | Unity forces a single entry: |
| Partially (if Unity-generated) | Filename controlled by |
| No. | Regenerated from assembly definition on every signature change. Don't attempt to manually edit. |
| Yes. | Manually-authored .csproj files are supported. Unity's pattern-matching for file types detects and includes them in compilation automatically. |
MSBuild feature support in Unity
All MSBuild features are supported in Unity projects unless documented otherwise in this section. For the authoritative reference on MSBuild features, refer to the Microsoft documentation.
Unsupported features
The following MSBuild features are not supported for Unity projects:
Unsupported feature | Description |
|---|---|
| Web / WPF / WinForms SDKs | |
| Non-C# compilers (Vbc / Fsc) | Unity's assembly definition model and C# compilation pipeline assume C#. VB.NET and F# projects aren't supported. |
COM Interop ( | If you need COM, build a separate C# helper assembly with standard |
| Target frameworks above Net Standard 2.1 | Manually authored .csproj files can technically set any |
| Multiple target frameworks | |
Limitations and best practices
The following section details MSBuild features that Unity supports to a limited extent and where particular workarounds or best practices apply.
Restrictions on use of NuGet
Unity doesn't support the use of NuGet as an alternative to the Unity Package Manager (UPM) for developing and distributing Unity Editor or Engine functionality. As such, Unity applies the following restrictions to the use of NuGet:
- Any NuGet packages that use Unity APIs are blocked by the compilation pipeline.
- Any UPM or Asset Store packages that import a NuGet package are blocked.
- Any package that causes functional conflicts with Engine behavior or poses a security risk to users can be blocked at Unity's discretion.
Base class libraries (BCLs)
Unity bundles specific versions of several .NET libraries with an Editor installation. These versions are dependencies for certain Unity features and are chosen to align with Unity's internal .NET runtime version. The currently bundled packages are:
System.Text.JsonSystem.Collections.ImmutableSystem.Reflection.MetadataSystem.Runtime.CompilerServices.Unsafe
In the .NET assembly model it's not possible to have two versions of the same package present at once. If you add your own copy of one of the listed packages, Unity discards it and use its own bundled version instead.
A package you depend on that depends on a different version of one of these libraries will be resolved against Unity's version. In most cases this is transparent, but if your dependency requires an API surface that is not present in Unity's version, you might encounter incompatibilities.
MSBuild Restore
MSBuild in Unity supports both MSBuild restore and a faster NuGet restore engine. NuGet restore is the default and it bypasses MSBuild targets entirely. Any or hooks don't run under the default configuration. To switch to the MSBuild restore engine and make target hooks run normally, launch the Editor with the command-line argument.
Restore<Target Name="…" BeforeTargets="Restore">AfterTargets="Restore"Restore-disable-nuget-restoreCustom property settings and targets
If you create and files, they're disabled by default and setting alone doesn't enable them. To enable them, you must also create the file . Even when enabled, is never applied to package projects or to .
Directory.Build.propsDirectory.Build.targets<ImportDirectoryBuildProps>true</ImportDirectoryBuildProps>ProjectSettings/enableDirectoryBuild.configDirectory.Build.*Main.EntryPoint.csprojPrefer for project-wide extensions. It has the same reach as an enabled for user projects, without requiring the config file.
UnityProject.Build.propsDirectory.Build.propsCustom build configurations
<Configurations>UnityProject.Build.propsCustom+BetaYou can define custom configurations for direct invocations from the command line, but the Editor never builds with them. The C# preprocessor defines added under a custom configuration are not active in Editor-driven builds.
dotnet build -p:Configuration=Custom+BetaOutput paths for Unity assemblies
Unity uses the properties , , , and to define intermediate and output paths to and copies final DLLs to . These are critical for the Editor's assembly discovery. Overriding them on a .csproj breaks the Editor's ability to find the compiled assembly.
OutputPathIntermediateOutputPathPublishDirUnityOutputDirLibrary/MSBuild/artifacts/{obj,bin,Publish}Library/ScriptAssemblies/Unity.SdkFramework reference not supported with .NET Standard 2.1
Any item, such as , fails with with Unity's default .
FrameworkReference<FrameworkReference Include="Microsoft.NETCore.App"/>NETSDK1073: The FrameworkReference '…' was not recognizedTargetFramework=netstandard2.1FrameworkReferencenet5+netcorenet10.0enablelastdotnet.configBuild events
The property form of build events works. For example, fires an command correctly. Some Visual Studio specific macros inside the string (such as might work depending on evaluation context, but others such as that assume the legacy .csproj format might not always expand. Prefer the target-based form ().
<PreBuildEvent>echo hello</PreBuildEvent>echo$(SolutionDir)$(ProjectDir)<Target Name="X" BeforeTargets="PreBuildEvent">Disabling Unity's package source
Never disable the package source unless you have an alternative way to find Unity SDK packages. Adding to disables Unity's own SDK feed and Unity's doesn't re-enable it. Restore continues to succeed as long as the Unity SDK packages are already in the local NuGet cache, but on a fresh cache, restore of (and the other Unity MSBuild SDKs) fails.
Unity Source<disabledPackageSources><add key="Unity Source" value="true"/></disabledPackageSources>NuGet.configMaintainNugetConfigUnity.SdkThird-party SDKs via Nuget SDK resolution
Third-party SDKs, such as Microsoft.Build.NoTargets, referenced via (NuGet-based SDK resolution) don't inherit Unity's output-path overrides, so and land inside and clutter the Asset Database.
<Project Sdk="Name/version">obj/bin/Assets/…/Place third-party SDK .csproj files inside a folder suffixed with , for example . The folder is then excluded from the auto-include, so you must explicitly reference it with from .
~Assets/OrchLib~/OrchLib.csproj<ProjectReference Include="OrchLib~/OrchLib.csproj"/>Main.EntryPoint.csprojAssemblies referenced via package reference
Due to a known issue, analyzers and other assemblies referenced via in manually-authored .csproj files might be restored but never run. Working alternatives are as follows:
<PackageReference>- Asset-labeled analyzers: Place the analyzer DLL under , set the asset's label to
Assets/. For more information, refer to Create and use a Roslyn analyzer.RoslynAnalyzer - Scoped analyzers via : Manually declare
{AssemblyName}.Build.propsin a<Analyzer Include="path/to/analyzer.dll"/>next to an .asmdef-generated .csproj. Applies to direct referencers of that assembly.{name}.Build.props
Solution file maintenance
When you add and remove projects, for example by adding a new assembly definition or installing a package, their task correctly updates the solution (.slnx) file. Project membership within the solution correctly reflects the reference graph defined by entries. Unity doesn't overwrite custom solution folders and non-project sections.
<ProjectReference>Embedding resources
Default items are disabled. You must must explicitly , but the pipeline itself works.
EmbeddedResource<EmbeddedResource Include="…"/>.resx.resourcesStrings.fr.resxfr/{Name}.resources.dllLibrary/ScriptAssemblies/