Skip to content

Latest commit

 

History

History
44 lines (24 loc) · 3.49 KB

MSBuildProjectBuilder.md

File metadata and controls

44 lines (24 loc) · 3.49 KB

MSBuild Project Builder

The MSBuild Project Builder provides a means of building MSBuild projects from within Unity, where the output is generally consumed by the Unity project. This is particularly useful when sharing code between a Unity project and a non-Unity project (e.g. UWP XAML, Xamarin, etc.) via a NuGet package or an MSBuild project.

Project and Solution Importer

The simplest way to use the MSBuild Project Builder is to simply drop a .csproj or .sln into a Unity project (somewhere under Assets). A custom scripted importer will create a custom asset that allows the MSBuild project to be built from within Unity. The project/solution will immediately be built, and will also be built whenever the project/solution changes, or when the Unity project is reloaded. Note there are some considerations in how the project and directory structure are organized to ensure the source is compiled by MSBuild and not by Unity.

MSBuild Importer

Manual Asset Creation

It's also possible to manually create a reference to an MSBuild project/solution, which may even live outside of the Unity project. Simply create an MSBuild Project Reference asset and point it to an MSBuild project/solution.

MSBuild Importer

Manual Build

Referenced MSBuild projects/solutions are automatically built when the Unity project is loaded. They can also be built manually from within the Unity Editor. All referenced projects/solutions can be built from the top level MSBuild menu.

MSBuild Importer

Alternatively, individual projects/solutions can be built from the inspector.

MSBuild Importer

Considerations

When dropping an MSBuild project/solution into a Unity project, some additional configuration is typically required to get the right behavior:

  • If the project compiles source code, Unity must not also compile that source code. The simplest way to achieve this is to ensure the source code lives in a directory starting with a '.' (which Unity will ignore).
  • If both an MSBuild project and a containing solution are dropped into the Unity project, only the solution should be built (to prevent double building the project). The simplest way to achieve this is to ensure the project lives in a directory starting with a '.' (which Unity will ignore).
  • The build output typically should not be checked in to source control. If git is being used, the simplest way to ensure this is to simply add a .gitignore to the output directory.

See the Source/MSBuildProjectBuilder/Assets/Samples directory for an example.

Programmatic Builds

MSBuild projects/solutions can also be built programmatically via MSBuildProjectBuilder.

MSBuildProjectBuilder.BuildAllProjectsAsync - use one of these overloads to build all MSBuild projects/solutions referenced by MSBuildProjectReferences in the Unity project.

MSBuildProjectBuilder.BuildProjectsAsync - use one of these overloads to build a specified collection of projects/solutions referenced by MSBuildProjectReferences.

MSBuildProjectBuilder.BuildProjectAsync - use one of these overloads to build a specified project/solution referenced by a MSBuildProjectReference.