MSBuild integration in Unity
Перейти к файлу
Josh Wittner 21a1f248a3
Merge pull request #166 from lynzrand/fix-165
Add support for .NET Framwork 4.7.1 with Unity 2021.2 or newer
2023-01-23 08:23:38 -08:00
AzurePipelines Update Publish.UPM.yml for Azure Pipelines 2020-01-31 14:08:37 -08:00
Documentation replaced %20 with - 2020-01-31 11:32:28 -08:00
Samples Merge pull request #158 from microsoft/dependabot/nuget/Samples/IntegratedDependencies.Unity/Assets/WSASpecific/Newtonsoft.Json-13.0.1 2022-08-29 10:13:28 -07:00
Source Add support for .NET Framwork 4.7.1 with Unity 2021.2 or newer 2023-01-20 16:36:31 +08:00
Tests/ProjectGeneration Additional IL2CPP vs. .NET and NET46 vs NETStandard20 fixes (+ other) (#27) 2019-09-09 12:19:21 -07:00
.gitattributes Add .gitattributes and renormalize line endings 2019-07-30 15:48:23 -07:00
.gitmodules Sign third party components (#47) 2019-11-07 20:44:21 -08:00
CODE_OF_CONDUCT.md Add .gitattributes and renormalize line endings 2019-07-30 15:48:23 -07:00
Directory.Build.props Upgrading to 0.9.1 2020-01-31 14:02:16 -08:00
LICENSE Add .gitattributes and renormalize line endings 2019-07-30 15:48:23 -07:00
README.md Upm package ref in readme (#152) 2021-11-08 18:54:07 -08:00
SECURITY.md Microsoft mandatory file 2022-08-15 21:14:17 +00:00

README.md

MSBuildForUnity

MSBuildForUnity solves the problem of establishing clear dependency relationships between Unity project and other .NET components such as external (to Unity) C# projects, or NuGet packages. It creates a familiar to .NET developers project structure and ensures that the dependencies are resolved and brought into the Unity project as appropriate. With this component, you can:

  • Share code between Unity and other .NET projects (such as UWP XAML apps, Xamarin apps, etc.).
  • Consuming existing .NET components (e.g. NuGet packages).

The samples included in this repository best convey the simplicity and value of this component:

Builds and Packages

Build Build Status Package Feed
UPM UPM Build Status Azure DevOps
NuGet NuGet Build Status NuGet Package

Quick Start

The following are basic instructions for taking advantage of MSBuildForUnity for some common scenarios.

Bring NuGet packages and MSBuild projects into a Unity project

This scenario leverages the MSBuildForUnity Project Builder and the MSBuildForUnity NuGet Package.

  1. Add the com.microsoft.msbuildforunity UPM (Unity Package Manager) package.

    • Edit the Packages/manifest.json file in your Unity project.

    • Add the following near the top of the file:

      "scopedRegistries": [
          {
              "name": "Microsoft",
              "url": "https://pkgs.dev.azure.com/UnityDeveloperTools/MSBuildForUnity/_packaging/UnityDeveloperTools/npm/registry/",
              "scopes": [
                  "com.microsoft"
              ]
          }
      ],
      
    • Add the following to the dependencies section of the file:

        "com.microsoft.msbuildforunity": "<version>"
      

      NOTE: Replace <version> with the latest package version from the MSBuildForUnity UPM feed.

  2. MSBuildForUnity will create a top-level project in your Assets folder named after your Unity project name: {UnityProjectName}.Dependencies.msb4u.csproj, edit this project file to add additional references to any NuGet packages or C# projects you want to use in your Unity project.

    <Project ToolsVersion="15.0">
    <!--GENERATED FILE-->
    <!--
        This file can be modified and checked in.
    
        It is different from the other generated C# Projects in that it will be the one gathering all dependencies and placing them into the Unity asset folder.
    
        You can add project level dependencies to this file, by placing them below:
        - <Import Project="$(MSBuildForUnityGeneratedProjectDirectory)\$(MSBuildProjectName).g.props" />
        and before:
        - <Import Project="$(MSBuildForUnityGeneratedProjectDirectory)\$(MSBuildProjectName).g.targets" />
    
        Do not add any source or compilation items.
    
        Examples of how you can modify this file:
        - Add NuGet package references:
            <ItemGroup>
                <PackageReference Include="Newtonsoft.Json" Version="11.0.1" />
            </ItemGroup>
        - Add external C# project references:
        <ItemGroup>
            <ProjectReference Include="..\..\..\ExternalLib\ExternalLib.csproj" />
        </ItemGroup>
    -->
    
    <Import Project="$([MSBuild]::GetPathOfFileAbove(MSBuildForUnity.Common.props))" Condition="Exists('$([MSBuild]::GetPathOfFileAbove(MSBuildForUnity.Common.props))')" />
    
    <PropertyGroup>
        <TargetFramework>$(UnityCurrentTargetFramework)</TargetFramework>
    </PropertyGroup>
    
    <!-- SDK.props is imported inside this props file -->
    <Import Project="$(MSBuildForUnityGeneratedProjectDirectory)\$(MSBuildProjectName).g.props" />
    
    <ItemGroup>
        <!--Add NuGet or Project references here-->
    </ItemGroup>
    
    <!-- SDK.targets is imported inside this props file -->
    <Import Project="$(MSBuildForUnityGeneratedProjectDirectory)\$(MSBuildProjectName).g.targets" />
    </Project>
    
  3. For additional instructions, see Core Scenarios.

Extended Instructions

Here you can find additional instructions for various things you may want to accomplish:

Features

MSBuildForUnity has several features that can be used independently or in conjunction.

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.

MSBuild Project Builder Progress Bar

For details, see the documentation, source, and samples.

MSBuild Project Generator

The MSBuild Project Generator will generate a Visual Studio solution configured for building the Unity project into DLLs outside of Unity. This solution is configured for each of the platforms installed with Unity and the InEditor/Player variants of the assemblies.

MSBuildForUnity NuGet Package

The MSBuildForUnity NuGet package augments the default MSBuild build logic to ensure the build output is suitable for Unity consumption. This package can be referenced from MSBuild projects that are built by the MSBuild Project Builder to add these features:

  • Meta file generation - generates .meta files for build output such as .dlls.
  • Dependency resolution - all dependencies (through PackageReferences or ProjectReferences) are resolved and sent to the output directory (which is typically under the Unity project's Assets directory).
  • Debug symbol patching - enables debugging pre-built dlls (e.g. from NuGet packages) while running in the Unity Editor.

For details, see the documentation, source, and samples.

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.

When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Prerequisites

The following tools are required to contribute to this project:

To get started, clone the repo, and then run git submodule update --init to initialize submodules.