Skip to content

Commit 44a649c

Browse files
committed
Add support for finding Unity in the Hub alternate install path
Users can set a custom install path for Unity installations in the Unity Hub, and that information is recorded in a json file in the hub config folder. Older versions of the Hub also recorded a preferred version of Unity to be used, which can be used as a fallback if the requested version isn't available. This adds support for finding Unity installations in this custom location, and for picking the latest (or preferred, if available) version of Unity that's installed, if the requested one isn't found. The error is changed to output all of the calculated paths that were scanned, instead of hardcoding paths per-platform, as some of these paths can't be known until build time.
1 parent f45f5ea commit 44a649c

File tree

2 files changed

+83
-37
lines changed

2 files changed

+83
-37
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Add support for finding Unity in the Hub alternate install path ([#2015](https://github.com/getsentry/sentry-unity/pull/2015))
6+
57
### Features
68

79
- The `Ignore CLI Errors` checkbox in the Debug Symbols tab now applies to all supported platforms. ([#2008](https://github.com/getsentry/sentry-unity/pull/2008))

Directory.Build.targets

Lines changed: 81 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,73 +24,117 @@
2424
<SentryWindowsArtifactsDestination>$(SentryArtifactsDestination)Windows/Sentry/</SentryWindowsArtifactsDestination>
2525
</PropertyGroup>
2626

27+
<PropertyGroup>
28+
<HubSecondaryInstallPathFile>$(AppData)\UnityHub\secondaryInstallPath.json</HubSecondaryInstallPathFile>
29+
<HubDefaultEditorFile>$(AppData)\UnityHub\defaultEditor.json</HubDefaultEditorFile>
30+
<HubDefaultEditor Condition="'$(HubDefaultEditor)' == ''"></HubDefaultEditor>
31+
<HubInstallDir Condition="'$(HubInstallDir)' == '' AND $([MSBuild]::IsOSPlatform('Windows'))">C:\Program Files\Unity\Hub\Editor</HubInstallDir>
32+
<HubInstallDir Condition="'$(HubInstallDir)' == '' AND $([MSBuild]::IsOSPlatform('OSX'))">\Applications\Unity\Hub\Editor</HubInstallDir>
33+
<HubInstallDir Condition="'$(HubInstallDir)' == '' AND $([MSBuild]::IsOSPlatform('Linux'))">$(Home)\Unity\Hub\Editor</HubInstallDir>
34+
<HubInstallDir Condition="!Exists('$(HubInstallDir)')"></HubInstallDir>
35+
</PropertyGroup>
36+
37+
<Target Name="FindHub"
38+
Condition="'$(HubInstallDir)' == '' AND Exists('$(HubSecondaryInstallPathFile)')"
39+
Returns="$(HubInstallDir);$(HubDefaultEditor)">
40+
41+
<ReadLinesFromFile File="$(HubSecondaryInstallPathFile)">
42+
<Output TaskParameter="Lines" ItemName="item1" />
43+
</ReadLinesFromFile>
44+
45+
<ReadLinesFromFile File="$(HubDefaultEditorFile)" Condition="Exists('$(HubDefaultEditorFile)')">
46+
<Output TaskParameter="Lines" ItemName="item2" />
47+
</ReadLinesFromFile>
48+
49+
<PropertyGroup>
50+
<HubInstallDir>@(item1->Replace('"', ''))</HubInstallDir>
51+
<HubInstallDir Condition=" !Exists('$(HubInstallDir)') "></HubInstallDir>
52+
53+
<HubDefaultEditor>@(item2->Replace('"', ''))</HubDefaultEditor>
54+
<HubDefaultEditor Condition=" !Exists('$(HubInstallDir)\$(HubDefaultEditor)') "></HubDefaultEditor>
55+
</PropertyGroup>
56+
</Target>
57+
2758
<!-- Use the Unity Editor version set in the sample project of the repo -->
28-
<Target Name="FindUnity">
59+
<Target Name="FindUnity" DependsOnTargets="FindHub" AfterTargets="FindHub">
2960
<Message Text="Unity Version: $(UnityVersion)" Importance="Normal" />
3061

62+
<!-- Find all the installations of Unity done by the Unity Hub -->
63+
<ItemGroup Condition="'$(HubInstallDir)' != '' AND '$(HubDefaultEditor)' == ''">
64+
<_AllUnityInstallDirs Include="$([System.IO.Directory]::GetDirectories('$(HubInstallDir)'))" />
65+
<_UnityInstalls Condition="$([System.Text.RegularExpressions.Regex]::IsMatch('%(Filename)', '^[\d]{4}.*$'))" Include="@(_AllUnityInstallDirs->'%(Filename)%(Extension)')" />
66+
</ItemGroup>
67+
68+
<!-- Pick the latest one if this version of the Hub doesn't record a default version -->
69+
<PropertyGroup Condition="'$(HubInstallDir)' != '' AND '$(HubDefaultEditor)' == ''">
70+
<HubDefaultEditor>%(_UnityInstalls.Identity)</HubDefaultEditor>
71+
</PropertyGroup>
72+
73+
<PropertyGroup>
74+
<_UnityInnerPath Condition="!$([MSBuild]::IsOSPlatform('OSX'))">Editor\Data</_UnityInnerPath>
75+
<_UnityInnerPath Condition="$([MSBuild]::IsOSPlatform('OSX'))">Unity.App\Contents</_UnityInnerPath>
76+
</PropertyGroup>
77+
78+
<ItemGroup>
79+
<_PotentialUnityPaths Condition="'$(HubInstallDir)' != ''" Include="$(HubInstallDir)\$(UnityVersion)\$(_UnityInnerPath)\Managed\UnityEngine.dll" />
80+
<_PotentialUnityPaths Condition="'$(HubInstallDir)' != '' AND '$(HubDefaultEditor)' != '' AND '$(UnityVersion)' != '$(HubDefaultEditor)'" Include="$(HubInstallDir)\$(HubDefaultEditor)\$(_UnityInnerPath)\Managed\UnityEngine.dll" />
81+
<_PotentialUnityPaths Condition="$([MSBuild]::IsOSPlatform('Windows'))" Include="C:\Program Files\Unity\$(_UnityInnerPath)\Managed\UnityEngine.dll" />
82+
<_PotentialUnityPaths Condition="$([MSBuild]::IsOSPlatform('OSX'))" Include="\Applications\Unity\$(_UnityInnerPath)\Managed\UnityEngine.dll" />
83+
<_UnityPathsFound Include="@(_PotentialUnityPaths->Exists())" />
84+
<_UnityPathsFoundReversed Include="@(_UnityPathsFound->Reverse())" />
85+
</ItemGroup>
86+
87+
<PropertyGroup>
88+
<!--This is a little hack to grab the first item found on the list - properties are repeatedly set for each item on the list, so they end up with the last one (we reversed the list so we get the first one) -->
89+
<_UnityPathProp>%(_UnityPathsFoundReversed.Identity)</_UnityPathProp>
90+
</PropertyGroup>
91+
92+
<ItemGroup>
93+
<!-- Turn the property back into an item so we can use DirectoryName() below. -->
94+
<_UnityPath Include="$(_UnityPathProp)" />
95+
</ItemGroup>
96+
97+
<PropertyGroup Condition="'$(_UnityPathProp)' != ''">
98+
<UnityManagedPath>@(_UnityPath->DirectoryName())\</UnityManagedPath>
99+
<UnityDataPath>@(_UnityPath->DirectoryName()->DirectoryName())\</UnityDataPath>
100+
<UnityRoot>@(_UnityPath->DirectoryName()->DirectoryName()->DirectoryName())\</UnityRoot>
101+
<UnityLibcache>$(UnityDataPath)Resources\PackageManager\ProjectTemplates\libcache\</UnityLibcache>
102+
</PropertyGroup>
103+
104+
<Error Condition="'$(UnityRoot)' == ''" Text="UnityRoot not found. Ensure Unity is installed.
105+
See the CONTRIBUTING.md.
106+
UnityVersion: '$(UnityVersion)'
107+
Expected to exist:
108+
* @(_PotentialUnityPaths, '%0a or %0a * ')" />
109+
110+
31111
<!-- Unity paths on Windows -->
32112
<PropertyGroup Condition="$([MSBuild]::IsOSPlatform('Windows'))">
33-
<UnityRoot Condition="Exists('C:\Program Files\Unity\Hub\Editor\$(UnityVersion)\Editor\Data\Managed\UnityEngine.dll')">C:\Program Files\Unity\Hub\Editor\$(UnityVersion)\Editor\</UnityRoot>
34-
<!--If not using Unity Hub, tries to pick whatever Unity version is installed on the machine-->
35-
<UnityRoot Condition="$(UnityRoot) == '' AND Exists('C:\Program Files\Unity\Editor\Data\Managed\UnityEngine.dll')">C:\Program Files\Unity\Editor</UnityRoot>
36-
<UnityLibcache>$(UnityRoot)Data/Resources/PackageManager/ProjectTemplates/libcache/</UnityLibcache>
37-
<UnityManagedPath>$(UnityRoot)\Data\Managed</UnityManagedPath>
38113
<UnityExec>&quot;$(UnityRoot)\Unity.exe&quot;</UnityExec>
39114
<StandaloneBuildMethod>Builder.BuildWindowsIl2CPPPlayer</StandaloneBuildMethod>
40115
<StandaloneBuildPath>$(PlayerBuildPath)Windows/IL2CPP_Player.exe</StandaloneBuildPath>
41116
<StandaloneExecutablePath>$(StandaloneBuildPath)</StandaloneExecutablePath>
42117
<StandaloneDataPath>$(USERPROFILE)/AppData/LocalLow/DefaultCompany/unity-of-bugs/</StandaloneDataPath>
43118
</PropertyGroup>
44119

45-
<Error Condition="!Exists('$(UnityRoot)') AND $([MSBuild]::IsOSPlatform('Windows'))" Text="UnityRoot not found. Ensure Unity is installed.
46-
See the CONTRIBUTING.md.
47-
UnityVersion: '$(UnityVersion)'
48-
Resolved directory: '$(UnityRoot)'
49-
Expected to exist:
50-
* C:\Program Files\Unity\Hub\Editor\$(UnityVersion)\Editor\Data\Managed\UnityEngine.dll
51-
or
52-
* C:\Program Files\Unity\Editor\Data\Managed\UnityEngine.dll" />
53120

54121
<!-- Unity paths on macOS -->
55122
<PropertyGroup Condition="$([MSBuild]::IsOSPlatform('OSX'))">
56-
<UnityRoot Condition="Exists('/Applications/Unity/Hub/Editor/$(UnityVersion)/Unity.app/Contents/Managed/UnityEngine.dll')">/Applications/Unity/Hub/Editor/$(UnityVersion)/Unity.app/</UnityRoot>
57-
<!--If not using Unity Hub, tries to pick whatever Unity version is installed on the machine-->
58-
<UnityRoot Condition="$(UnityRoot) == '' AND Exists('/Applications/Unity/Unity.app/Contents/Managed/UnityEngine.dll')">/Applications/Unity/Unity.app/</UnityRoot>
59-
<UnityLibcache>$(UnityRoot)Contents/Resources/PackageManager/ProjectTemplates/libcache/</UnityLibcache>
60-
<UnityManagedPath>$(UnityRoot)Contents/Managed</UnityManagedPath>
61123
<UnityExec>&quot;$(UnityRoot)Contents/MacOS/Unity&quot;</UnityExec>
62124
<StandaloneBuildMethod>Builder.BuildMacIl2CPPPlayer</StandaloneBuildMethod>
63125
<StandaloneBuildPath>$(PlayerBuildPath)MacOS/IL2CPP_Player.app</StandaloneBuildPath>
64126
<StandaloneExecutablePath>$(StandaloneBuildPath)/Contents/MacOS/unity-of-bugs</StandaloneExecutablePath>
65127
</PropertyGroup>
66128

67-
<Error Condition="!Exists('$(UnityRoot)') AND $([MSBuild]::IsOSPlatform('OSX'))" Text="UnityRoot not found. Ensure Unity is installed.
68-
See the CONTRIBUTING.md.
69-
UnityVersion: '$(UnityVersion)'
70-
Resolved directory: '$(UnityRoot)'
71-
Expected to exist:
72-
* /Applications/Unity/Hub/Editor/$(UnityVersion)/Unity.app/Contents/Managed/UnityEngine.dll
73-
or
74-
* /Applications/Unity/Unity.app/Contents/Managed/UnityEngine.dll" />
75129

76130
<!-- Unity paths on Linux -->
77131
<PropertyGroup Condition="$([MSBuild]::IsOSPlatform('Linux'))">
78-
<UnityRoot Condition="Exists('$(Home)/Unity/Hub/Editor/$(UnityVersion)/Editor/Data/Managed/UnityEngine.dll')">$(Home)/Unity/Hub/Editor/$(UnityVersion)/Editor/</UnityRoot>
79-
<UnityRoot Condition="$(UNITY_PATH) != ''">$(UNITY_PATH)/Editor/</UnityRoot>
80-
<UnityLibcache>$(UnityRoot)Data/Resources/PackageManager/ProjectTemplates/libcache/</UnityLibcache>
81-
<UnityManagedPath>$(UnityRoot)Data/Managed</UnityManagedPath>
82132
<UnityExec>xvfb-run -ae /dev/stdout &quot;$(UnityRoot)Unity&quot;</UnityExec>
83133
<StandaloneBuildMethod>Builder.BuildLinuxIl2CPPPlayer</StandaloneBuildMethod>
84134
<StandaloneBuildPath>$(PlayerBuildPath)Linux/IL2CPP_Player</StandaloneBuildPath>
85135
<StandaloneExecutablePath>$(StandaloneBuildPath)</StandaloneExecutablePath>
86136
</PropertyGroup>
87137

88-
<Error Condition="!Exists('$(UnityRoot)') AND $([MSBuild]::IsOSPlatform('Linux'))" Text="UnityRoot not found. Ensure Unity is installed.
89-
See the CONTRIBUTING.md.
90-
UnityVersion: '$(UnityVersion)'
91-
Resolved directory: '$(UnityRoot)'
92-
Expected to exist:
93-
* $(Home)/Unity/Hub/Editor/$(UnityVersion)/Editor/Data/Managed/UnityEngine.dll" />
94138

95139
<LocateTestRunner UnityLibcache="$(UnityLibcache)">
96140
<Output PropertyName="TestRunnerPath" TaskParameter="TestRunnerPath" />

0 commit comments

Comments
 (0)