This documentation outlines instructions for the core scenarios that are enabled with MSB4U, and how to get started with them. Each scenario assumes the reader understand how to add MSB4U to the Unity project, if not, take a look at the instructions. If you have issues following scenarios, see troubleshooting.
GOAL: We have a Unity project where we want to add the Newtonsoft.Json
NuGet package for use in the project.
To make this happen, we will simply rely on MSB4U providing us with a top-level MSBuild project that builds itself plus other MSBuild projects in our Unity assets directory, and add our reference to it. As a result, this project will be built, and it's dependencies and references will be brought into it's output directory inside the Unity assets folder.
-
Find the generated dependencies project in your Assets folder. It is named after your Unity project; ie.
(UnityProjectName).Dependencies.msb4u.csproj
. -
Double-click the project, to open it in Visual Studio.
-
Right-click on the
(UnityProjectName).Dependencies.msb4u
, and pressManage NuGet Packages...
-
In the NuGet Manager window, ensure
Browse
is Selected, and selectNewtonsoft.Json
-
On the right side, press
Install
to install it to the project.NOTE: Ensure to save the project in Visual Studio.
-
Temporary Limitation: Open the project file in any text editor, and ensure that the
PackageReference
is above theImport Targets
, see example below:<!-- SDK.props is imported inside this props file --> <Import Project="$(MSBuildForUnityGeneratedProjectDirectory)\$(MSBuildProjectName).g.props" /> <ItemGroup> <PackageReference Include="Newtonsoft.Json" Version="12.0.3" /> </ItemGroup> <!-- SDK.targets is imported inside this props file --> <Import Project="$(MSBuildForUnityGeneratedProjectDirectory)\$(MSBuildProjectName).g.targets" />
-
Back in Unity, the build should now happen and your dependency will appear under
Dependencies\{Current TargetFramework}\
folder.
GOAL: We have two Unity projects, and want to share a C# library that contains common code.
This is similar to Scenario 1
, but we will rely on the editing the dependency project manually here.
-
Find the generated dependencies project in your Assets folder. It is named after your Unity project; ie.
(UnityProjectName).Dependencies.msb4u.csproj
. -
Double-click the project, to open it in Visual Studio.
-
Right-click on the
(UnityProjectName).Dependencies.msb4u
, and pressEdit (UnityProjectName).Dependencies.msb4u.csproj
-
In the text window, add your
ProjectReference
as follows:<!-- SDK.props is imported inside this props file --> <Import Project="$(MSBuildForUnityGeneratedProjectDirectory)\$(MSBuildProjectName).g.props" /> <ItemGroup> <!--Add NuGet or Project references here--> <ProjectReference Include="..\Relative\Path\To\Your.csproj" /> </ItemGroup> <!-- SDK.targets is imported inside this props file --> <Import Project="$(MSBuildForUnityGeneratedProjectDirectory)\$(MSBuildProjectName).g.targets" />
-
Back in Unity, the build should now happen and your compiled project binary will appear under
Dependencies\{Current TargetFramework}\
folder.
GOAL: We want to distribute a .unitypackage (or zip file) that has a dependency on a NuGet package.
In Scenario 1
and Scenario 2
we seen how we can modify the MSBuild project provided to us by MSB4U to add a reference to our dependencies. Here, we will actually craft a simple MSBuild project where we will specify our dependency, and package it up along with our source code. Then, we will bring this .unitypackage into the consuming Unity project that has MSB4U installed, which will result in our project being built and it's dependency brought into the consuming Unity project assets folder.
NOTE: For this scenario, it is assumed that the reader knows how to create a Unity package and import it into a project.
-
Open up a Unity project, and setup a folder in the Assets directory with the following files (and contents). The CSProject file is specially documented to explain each piece.
MyComponent.csproj
<Project> <!-- Import the MSB4U generated common project as we will rely on it. --> <Import Project="$([MSBuild]::GetPathOfFileAbove(MSBuildForUnity.Common.props))" Condition="Exists('$([MSBuild]::GetPathOfFileAbove(MSBuildForUnity.Common.props))')" /> <PropertyGroup> <!-- Specify both of these target frameworks, as we will need to support them for Unity 2018 case. --> <TargetFrameworks>netstandard2.0;net46</TargetFrameworks> </PropertyGroup> <PropertyGroup> <!-- Make sure Unity ignores the contents of the output path. --> <BaseIntermediateOutputPath>.obj\</BaseIntermediateOutputPath> <OutputPath>.bin\</OutputPath> </PropertyGroup> <!-- Note that this is the special "NoTarget" SDK to prevent this project from producing a dll. --> <Import Project="Sdk.props" Sdk="Microsoft.Build.NoTargets" Version="1.0.85" /> <ItemGroup> <!-- Specify your dependency here --> <PackageReference Include="Newtonsoft.Json" Version="12.0.2" /> </ItemGroup> <Import Project="Sdk.targets" Sdk="Microsoft.Build.NoTargets" Version="1.0.85" /> </Project>
.gitignore
/.bin/ /.obj/
MyComponent.cs
using Newtonsoft.Json.Linq; public class Test { public static JObject Property = JObject.Parse("{ Hello: \"World\" }"); }
-
Now, this folder could be packaged up and distributed.
-
Create a Unity project that will consume this component, and install MSBuildForUnity.
-
Bring this component in, and it will automatically build it.
GOAL: We want to distribute our source through UPM, but need a NuGet dependency.
In order to support this scenario, we simply follow Scenario 3
in terms of setting up the C# project, and then establish a UPM dependency onto the MSBuildForUnity UPM package. This would bring in the MSBuildForUnity package by default and activate your C# Project.
Instructions coming soon.