Skip to content

Commit d1e3a15

Browse files
author
Andrei Borodin
authored
Fix Platform and TargetFramework switching (#70)
* Rework automatic generation logic, and ensure cleanning of build artifacts is done at appropriate times. * Removing debug logs. * A few minor fixes to strengthen regen for correct times. * Updated versions to 0.8.2 * Forcing NuGet packages to be copied out to bin folder
1 parent bf60529 commit d1e3a15

File tree

6 files changed

+82
-25
lines changed

6 files changed

+82
-25
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ This scenario leverages the MSBuildForUnity [Project Builder](#msbuild-project-b
6868
```
6969
- Add the following to the `dependencies` section of the file:
7070
```json
71-
"com.microsoft.msbuildforunity": "0.8.1"
71+
"com.microsoft.msbuildforunity": "0.8.2"
7272
```
7373
1. Create a "SDK style" MSBuild project (e.g. csproj) somewhere under your `Assets` directory of your Unity project that references the `MSBuildForUnity` NuGet package. Here is an example:
7474
```xml
@@ -77,7 +77,7 @@ This scenario leverages the MSBuildForUnity [Project Builder](#msbuild-project-b
7777
<TargetFramework>netstandard2.0</TargetFramework>
7878
</PropertyGroup>
7979
<ItemGroup>
80-
<PackageReference Include="MSBuildForUnity" Version="0.8.1">
80+
<PackageReference Include="MSBuildForUnity" Version="0.8.2">
8181
<PrivateAssets>all</PrivateAssets>
8282
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
8383
</PackageReference>

Samples/SimpleNuGetDependency.Unity/Assets/NewtonsoftDependency/NewtonsoftDependency.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
</PropertyGroup>
1818

1919
<ItemGroup>
20-
<PackageReference Include="MSBuildForUnity" Version="[0.8.1-*, 0.8.1]">
20+
<PackageReference Include="MSBuildForUnity" Version="[0.8.2-*, 0.8.2]">
2121
<PrivateAssets>all</PrivateAssets>
2222
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
2323
</PackageReference>

Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/MSBuildTemplates/DependenciesProjectTemplate.g.props.template

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
<BaseIntermediateOutputPath>$(MSBuildForUnityGeneratedOutputDirectory)\..\Output\obj\Dependencies</BaseIntermediateOutputPath>
1818
<OutputPath>Dependencies</OutputPath>
1919
<EnableDefaultItems>false</EnableDefaultItems>
20+
<!--Copy the NuGet package reference dlls as well.-->
21+
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
2022
</PropertyGroup>
2123

2224
<ItemGroup>
23-
<PackageReference Include="MSBuildForUnity" Version="[0.8.1-*, 0.8.1]">
25+
<PackageReference Include="MSBuildForUnity" Version="[0.8.2-*, 0.8.2]">
2426
<PrivateAssets>all</PrivateAssets>
2527
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
2628
</PackageReference>

Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/MSBuildTemplates/MSBuildForUnity.Common.props.template

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup>
3-
<MSBuildForUnityVersion>0.8.1</MSBuildForUnityVersion>
3+
<MSBuildForUnityVersion>0.8.2</MSBuildForUnityVersion>
44

55
<MSBuildForUnityGeneratedOutputDirectory><!--GENERATED_OUTPUT_DIRECTORY_TOKEN--></MSBuildForUnityGeneratedOutputDirectory>
66
<UnityProjectAssetsPath><!--UNITY_PROJECT_ASSETS_PATH_TOKEN--></UnityProjectAssetsPath>
@@ -12,4 +12,8 @@
1212
<UnityMajorVersion><!--UNITY_MAJOR_VERSION_TOKEN--></UnityMajorVersion>
1313
<UnityMinorVersion><!--UNITY_MINOR_VERSION_TOKEN--></UnityMinorVersion>
1414
</PropertyGroup>
15+
16+
<Target Name="_RemoveOutputDirectory" AfterTargets="Clean">
17+
<RemoveDir Directories="$(OutputPath)"/>
18+
</Target>
1519
</Project>

Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/Editor/ProjectGenerator/Scripts/MSBuildTools.cs

Lines changed: 70 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,10 @@ private class BuildTargetChanged : IActiveBuildTargetChanged
6262

6363
public void OnActiveBuildTargetChanged(BuildTarget previousTarget, BuildTarget newTarget)
6464
{
65-
MSBuildProjectBuilder.TryBuildAllProjects(MSBuildProjectBuilder.CleanProfileName);
66-
RunCoreAutoGenerate(true);
67-
MSBuildProjectBuilder.TryBuildAllProjects(MSBuildProjectBuilder.BuildProfileName);
65+
if (EditorAnalyticsSessionInfo.elapsedTime > 0)
66+
{
67+
RefreshGeneratedOutput(forceGenerateEverything: Config.AutoGenerateEnabled);
68+
}
6869
}
6970
}
7071

@@ -107,7 +108,8 @@ public static void ToggleAutoGenerate()
107108
{
108109
Config.AutoGenerateEnabled = !Config.AutoGenerateEnabled;
109110
Menu.SetChecked(AutoGenerate, Config.AutoGenerateEnabled);
110-
RunCoreAutoGenerate(false);
111+
// If we just toggled on, regenerate everything
112+
RefreshGeneratedOutput(forceGenerateEverything: Config.AutoGenerateEnabled);
111113
}
112114

113115
[MenuItem(AutoGenerate, true, priority = 101)]
@@ -123,7 +125,7 @@ public static void GenerateSDKProjects()
123125
{
124126
try
125127
{
126-
RegenerateEverything(true);
128+
RefreshGeneratedOutput(forceGenerateEverything: true);
127129
Debug.Log($"{nameof(GenerateSDKProjects)} Completed Succesfully.");
128130
}
129131
catch
@@ -135,28 +137,77 @@ public static void GenerateSDKProjects()
135137

136138
static MSBuildTools()
137139
{
138-
RunCoreAutoGenerate(false);
140+
if (EditorAnalyticsSessionInfo.elapsedTime == 0)
141+
{
142+
// The Unity asset database cannot be queried until the Editor is fully loaded. The first editor update tick seems to be a safe bet for this.
143+
144+
// Ensure a single invocation
145+
EditorApplication.update -= OnUpdate;
146+
EditorApplication.update += OnUpdate;
147+
void OnUpdate()
148+
{
149+
RefreshGeneratedOutput(forceGenerateEverything: false);
150+
EditorApplication.update -= OnUpdate;
151+
}
152+
}
153+
else
154+
{
155+
RefreshGeneratedOutput(forceGenerateEverything: false);
156+
}
139157
}
140158

141-
private static void RunCoreAutoGenerate(bool skipTokenFileCheck)
159+
private static void RefreshGeneratedOutput(bool forceGenerateEverything)
142160
{
143-
// Check if a file exists, if it does, we already generated this editor instance
144-
bool fileExists = File.Exists(TokenFilePath);
145-
if (!fileExists || skipTokenFileCheck)
161+
// In this method, the following must happen
162+
// - Clean up builds if necessary
163+
// - Generate the common props file if necessary
164+
// - Regenerate everything else if necessary
165+
// - Build if the clean was done
166+
167+
BuildTarget currentBuildTarget = EditorUserBuildSettings.activeBuildTarget;
168+
ApiCompatibilityLevel targetFramework = PlayerSettings.GetApiCompatibilityLevel(EditorUserBuildSettings.selectedBuildTargetGroup);
169+
170+
bool shouldClean = EditorPrefs.GetInt($"{nameof(MSBuildTools)}.{nameof(currentBuildTarget)}") != (int)currentBuildTarget
171+
|| EditorPrefs.GetInt($"{nameof(MSBuildTools)}.{nameof(targetFramework)}") != (int)targetFramework
172+
|| forceGenerateEverything;
173+
174+
if (shouldClean)
146175
{
147-
Exporter.GenerateDirectoryPropsFile(UnityProjectInfo);
176+
// We clean up previous build if the EditorPrefs currentBuildTarget or targetFramework is different from current ones.
177+
MSBuildProjectBuilder.TryBuildAllProjects(MSBuildProjectBuilder.CleanProfileName);
178+
}
148179

149-
if (!Config.AutoGenerateEnabled)
150-
{
151-
return;
152-
}
180+
bool doesTokenFileExist = File.Exists(TokenFilePath);
153181

182+
// We regenerate the common "directory" props file under the following conditions:
183+
// - Token file doesn't exist which means editor was just started
184+
// - EditorPrefs currentBuildTarget or targetFramework is different from current ones (same as for executing a clean)
185+
if (shouldClean || !doesTokenFileExist)
186+
{
187+
Exporter.GenerateDirectoryPropsFile(UnityProjectInfo);
188+
}
189+
190+
// We regenerate everything if:
191+
// - We are forced to
192+
// - AutoGenerateEnabled and token file doesn't exist or shouldClean is true
193+
if (forceGenerateEverything || (Config.AutoGenerateEnabled && (!doesTokenFileExist || shouldClean)))
194+
{
154195
RegenerateEverything(true);
196+
}
155197

156-
if (!fileExists)
157-
{
158-
File.Create(TokenFilePath).Dispose();
159-
}
198+
if (!doesTokenFileExist)
199+
{
200+
File.Create(TokenFilePath).Dispose();
201+
}
202+
203+
// Write the current targetframework and build target
204+
EditorPrefs.SetInt($"{nameof(MSBuildTools)}.{nameof(currentBuildTarget)}", (int)currentBuildTarget);
205+
EditorPrefs.SetInt($"{nameof(MSBuildTools)}.{nameof(targetFramework)}", (int)targetFramework);
206+
207+
// If we cleaned, now build
208+
if (shouldClean)
209+
{
210+
MSBuildProjectBuilder.TryBuildAllProjects(MSBuildProjectBuilder.BuildProfileName);
160211
}
161212
}
162213

Source/MSBuildTools.Unity/Packages/com.microsoft.msbuildforunity/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.microsoft.msbuildforunity",
3-
"version": "0.8.1",
3+
"version": "0.8.2",
44
"displayName": "MSBuild for Unity",
55
"description": "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.",
66
"unity": "2018.1",

0 commit comments

Comments
 (0)