|
| 1 | +#!/usr/bin/env pwsh |
| 2 | +# Locate a Unity installation and emit the paths the build needs. |
| 3 | +# |
| 4 | +# Output (stdout, one per line): |
| 5 | +# UnityRoot=<dir containing the Unity executable, trailing slash> |
| 6 | +# UnityDataPath=<dir containing the Editor data, trailing slash> |
| 7 | +# UnityManagedPath=<dir containing UnityEngine.dll, trailing slash> |
| 8 | +# |
| 9 | +# Warnings + the "candidates tried" error go to stderr. Exits non-zero on failure. |
| 10 | + |
| 11 | +[CmdletBinding()] |
| 12 | +param ( |
| 13 | + [string] $UnityVersion = '', |
| 14 | + [string] $HubInstallDir = '', |
| 15 | + [string] $HubDefaultEditor = '' |
| 16 | +) |
| 17 | + |
| 18 | +$ErrorActionPreference = 'Stop' |
| 19 | + |
| 20 | +function Read-HubFile([string] $path) { |
| 21 | + if (-not (Test-Path $path)) { return '' } |
| 22 | + # Unity Hub writes a single quoted string per file (not strict JSON). |
| 23 | + return (Get-Content -Raw $path).Trim().Trim('"') |
| 24 | +} |
| 25 | + |
| 26 | +# --- Resolve Hub install dir + default editor ------------------------------- |
| 27 | + |
| 28 | +# AppData is Windows-only; on macOS/Linux we just rely on the platform default below. |
| 29 | +$hubConfigDir = if ($env:APPDATA) { Join-Path $env:APPDATA 'UnityHub' } else { '' } |
| 30 | + |
| 31 | +if (-not $HubInstallDir -and $hubConfigDir) { |
| 32 | + $HubInstallDir = Read-HubFile (Join-Path $hubConfigDir 'secondaryInstallPath.json') |
| 33 | +} |
| 34 | +if (-not $HubInstallDir) { |
| 35 | + $HubInstallDir = if ($IsWindows) { 'C:/Program Files/Unity/Hub/Editor' } |
| 36 | + elseif ($IsMacOS) { '/Applications/Unity/Hub/Editor' } |
| 37 | + elseif ($IsLinux) { Join-Path $env:HOME 'Unity/Hub/Editor' } |
| 38 | +} |
| 39 | +if (-not (Test-Path $HubInstallDir)) { $HubInstallDir = '' } |
| 40 | + |
| 41 | +if (-not $HubDefaultEditor -and $hubConfigDir) { |
| 42 | + $HubDefaultEditor = Read-HubFile (Join-Path $hubConfigDir 'defaultEditor.json') |
| 43 | + if ($HubDefaultEditor -and -not (Test-Path (Join-Path $HubInstallDir $HubDefaultEditor))) { |
| 44 | + $HubDefaultEditor = '' |
| 45 | + } |
| 46 | +} |
| 47 | +# Fallback: pick the highest version-named dir under the hub install root. |
| 48 | +if (-not $HubDefaultEditor -and $HubInstallDir) { |
| 49 | + $HubDefaultEditor = Get-ChildItem -Path $HubInstallDir -Directory -ErrorAction SilentlyContinue | |
| 50 | + Where-Object Name -Match '^\d{4}' | |
| 51 | + Sort-Object Name -Descending | |
| 52 | + Select-Object -First 1 -ExpandProperty Name |
| 53 | +} |
| 54 | + |
| 55 | +# --- Build the probe list --------------------------------------------------- |
| 56 | + |
| 57 | +# Layout pieces. macOS Unity ships as a .app bundle so the inner path differs. |
| 58 | +$inner = if ($IsMacOS) { 'Unity.app/Contents' } else { 'Editor/Data' } |
| 59 | +$rootName = if ($IsMacOS) { 'Unity.app' } else { 'Editor' } |
| 60 | + |
| 61 | +# Each "install" candidate is a directory like <hub>/<version>/ that contains |
| 62 | +# either Editor/ (win/linux) or Unity.app/ (mac). |
| 63 | +$installs = [System.Collections.Generic.List[string]]::new() |
| 64 | +if ($IsLinux -and $env:UNITY_PATH) { $installs.Add($env:UNITY_PATH) } |
| 65 | +if ($HubInstallDir -and $UnityVersion) { $installs.Add((Join-Path $HubInstallDir $UnityVersion)) } |
| 66 | +if ($HubInstallDir -and $HubDefaultEditor -and $UnityVersion -ne $HubDefaultEditor) { |
| 67 | + $installs.Add((Join-Path $HubInstallDir $HubDefaultEditor)) |
| 68 | +} |
| 69 | +if ($IsWindows) { $installs.Add('C:/Program Files/Unity') } |
| 70 | +if ($IsMacOS) { $installs.Add('/Applications/Unity') } |
| 71 | + |
| 72 | +# Unity 6000.3+ moved managed assemblies under Resources/Scripting/Managed. |
| 73 | +# Probe the new layout first within each install. |
| 74 | +$dllRelatives = @('Resources/Scripting/Managed/UnityEngine.dll', 'Managed/UnityEngine.dll') |
| 75 | + |
| 76 | +function Find-UnityDll([string] $install) { |
| 77 | + foreach ($rel in $dllRelatives) { |
| 78 | + $dll = Join-Path $install (Join-Path $inner $rel) |
| 79 | + if (Test-Path $dll) { return $dll } |
| 80 | + } |
| 81 | + return $null |
| 82 | +} |
| 83 | + |
| 84 | +# Did the requested-version install yield a DLL? Drives the fallback warning below. |
| 85 | +$expected = if ($UnityVersion -and $HubInstallDir) { Join-Path $HubInstallDir $UnityVersion } else { '' } |
| 86 | +$shouldWarnFallback = $expected -and -not (Find-UnityDll $expected) |
| 87 | + |
| 88 | +$tried = [System.Collections.Generic.List[string]]::new() |
| 89 | + |
| 90 | +foreach ($install in $installs) { |
| 91 | + $dll = Find-UnityDll $install |
| 92 | + if (-not $dll) { |
| 93 | + $dllRelatives | ForEach-Object { $tried.Add((Join-Path $install (Join-Path $inner $_))) } |
| 94 | + continue |
| 95 | + } |
| 96 | + |
| 97 | + if ($shouldWarnFallback) { |
| 98 | + Write-Warning "Unity version $UnityVersion is not installed. Falling back to default Unity installation." |
| 99 | + } |
| 100 | + |
| 101 | + $unityRoot = (Join-Path $install $rootName) + '/' |
| 102 | + $unityDataPath = (Join-Path $install $inner) + '/' |
| 103 | + $unityManagedPath = (Split-Path $dll -Parent) + '/' |
| 104 | + |
| 105 | + # Normalize to forward slashes — MSBuild accepts both, and it keeps the output diff-clean across platforms. |
| 106 | + Write-Output ("UnityRoot=" + ($unityRoot -replace '\\', '/')) |
| 107 | + Write-Output ("UnityDataPath=" + ($unityDataPath -replace '\\', '/')) |
| 108 | + Write-Output ("UnityManagedPath=" + ($unityManagedPath -replace '\\', '/')) |
| 109 | + exit 0 |
| 110 | +} |
| 111 | + |
| 112 | +$msg = @" |
| 113 | +Unity installation not found. See CONTRIBUTING.md. |
| 114 | +UnityVersion: '$UnityVersion' |
| 115 | +Expected one of: |
| 116 | + * $($tried -join "`n * ") |
| 117 | +"@ |
| 118 | +Write-Error $msg |
| 119 | +exit 1 |
0 commit comments