Skip to content

Commit 237017c

Browse files
authored
Merge pull request #1 from Microsoft/dev/unityProjects
Add functionality to find unity projects, leverage.
2 parents 18d1c95 + 12929b4 commit 237017c

File tree

2 files changed

+68
-10
lines changed

2 files changed

+68
-10
lines changed

UnitySetup/UnitySetup.psd1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
RootModule = 'UnitySetup'
1515

1616
# Version number of this module.
17-
ModuleVersion = '1.3'
17+
ModuleVersion = '1.4'
1818

1919
# Supported PSEditions
2020
# CompatiblePSEditions = @()
@@ -73,10 +73,10 @@ RequiredModules = @(
7373
# NestedModules = @()
7474

7575
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
76-
FunctionsToExport = 'Get-UnitySetupInstance', 'Select-UnitySetupInstance', 'Start-UnityEditor'
76+
FunctionsToExport = 'Get-UnitySetupInstance', 'Select-UnitySetupInstance', 'Start-UnityEditor', 'Get-UnityProjectInstance'
7777

7878
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
79-
CmdletsToExport = 'Get-UnitySetupInstance', 'Select-UnitySetupInstance', 'Start-UnityEditor'
79+
CmdletsToExport = 'Get-UnitySetupInstance', 'Select-UnitySetupInstance', 'Start-UnityEditor', 'Get-UnityProjectInstance'
8080

8181
# Variables to export from this module
8282
# VariablesToExport = @()

UnitySetup/UnitySetup.psm1

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,62 @@ class UnityInstance
77
[string]$InstallationPath
88
}
99

10+
class UnityProjectInstance
11+
{
12+
[string]$ProjectPath
13+
[string]$UnityInstanceVersion
14+
}
15+
16+
<#
17+
.Synopsis
18+
Get the Unity Projects under a specfied folder
19+
.DESCRIPTION
20+
Recursively discovers Unity projects and their UnityInstance version
21+
.PARAMETER BasePath
22+
Under what base pattern should we look for Unity projects? Defaults to '$PWD'.
23+
.EXAMPLE
24+
Get-UnityProjectInstance
25+
.EXAMPLE
26+
Get-UnityProjectInstance -BasePath .\MyUnityProjects -Recurse
27+
#>
28+
function Get-UnityProjectInstance
29+
{
30+
[CmdletBinding()]
31+
param(
32+
[parameter(Mandatory=$false)]
33+
[string] $BasePath = $PWD,
34+
35+
[parameter(Mandatory=$false)]
36+
[switch] $Recurse
37+
)
38+
Import-Module powershell-yaml -Force -ErrorAction Stop
39+
40+
$args = @{
41+
'Path' = $BasePath;
42+
'Filter' = 'ProjectSettings';
43+
'ErrorAction' = 'Ignore';
44+
'Directory' = $true;
45+
}
46+
47+
if( $Recurse )
48+
{
49+
$args['Recurse'] = $true;
50+
}
51+
52+
Get-ChildItem @args |
53+
ForEach-Object {
54+
$path = [io.path]::Combine($_.FullName, "ProjectVersion.txt")
55+
if( Test-Path $path )
56+
{
57+
$projectVersion = Get-Content $path -ErrorAction Stop | ConvertFrom-Yaml -ErrorAction Stop
58+
New-Object UnityProjectInstance -Property @{
59+
ProjectPath = Join-Path $_.FullName "..\" | Convert-Path
60+
UnityInstanceVersion = $projectVersion.m_EditorVersion
61+
}
62+
}
63+
}
64+
}
65+
1066
<#
1167
.Synopsis
1268
Get the Unity versions installed
@@ -82,11 +138,8 @@ function Select-UnitySetupInstance
82138
{
83139
if( $Project )
84140
{
85-
Import-Module powershell-yaml -Force -ErrorAction Stop
86-
$projectVersionPath = [io.path]::combine("$Project","ProjectSettings\ProjectVersion.txt" );
87-
$projectVersion = Get-Content $projectVersionPath -ErrorAction Stop | ConvertFrom-Yaml -ErrorAction Stop
88-
89-
$Version = $projectVersion.m_EditorVersion
141+
$Version = Get-UnityProjectInstance -BasePath $Project |
142+
Select-Object -First 1 -ExpandProperty UnityInstanceVersion
90143
}
91144
}
92145
process
@@ -203,13 +256,18 @@ function Start-UnityEditor
203256

204257
if( $Instance -eq $null )
205258
{
206-
$Instance = Get-UnitySetupInstance | Select-UnitySetupInstance -Project $Project
259+
$version = Get-UnityProjectInstance -BasePath $Project | Select-Object -First 1 -ExpandProperty UnityInstanceVersion
260+
$Instance = Get-UnitySetupInstance | Select-UnitySetupInstance -Version $version
261+
}
262+
else
263+
{
264+
$version = $Instance.InstallationVersion
207265
}
208266

209267
$unityPath = $Instance.InstallationPath
210268

211269
if ( !$unityPath -or $unityPath -eq "" ) {
212-
throw "Could not find Unity editor for $($Instance.InstallationVersion)"
270+
throw "Could not find Unity Editor for version $version"
213271
}
214272

215273
$editor = Get-ChildItem "$unityPath" -Filter Unity.exe -Recurse | Select-Object -First 1 -ExpandProperty FullName

0 commit comments

Comments
 (0)