Skip to content

Instantly share code, notes, and snippets.

@aholkner
Last active February 8, 2023 09:06
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save aholkner/41929510d6b980b41903b2c612c11eea to your computer and use it in GitHub Desktop.
PowerShell script to install correct version of Unity for a project
@echo off
title Install Unity
echo Just a moment...
powershell -NoProfile -NoLogo -ExecutionPolicy Unrestricted -File PowershellScripts\InstallUnity.ps1 -Project . -Components Windows
pause
<#
.Synopsis
Install the appropriate Unity editor and components for the current project.
.PARAMETER Project
Path to Unity project
.PARAMETER Components
List of components to install; must include at least the current platform (e.g., Windows, Mac)
.EXAMPLE
& InstallUnity.ps1 -Project . -Components Mac,iOS
#>
param([string]$Project, [string[]]$Components)
$ErrorActionPreference = "Stop"
# Install UnitySetup module
If(!(Get-InstalledModule UnitySetup -ErrorAction SilentlyContinue))
{
Write-Host "Installing latest UnitySetup tools..."
Install-PackageProvider -Scope CurrentUser -Name NuGet -MinimumVersion 2.8.5.201 -Force
Install-Module UnitySetup -Scope CurrentUser -Confirm:$False -Force
}
# Get Unity version required by project
Write-Host "Checking for installed version of Unity..."
$RequiredVersion = (Get-UnityProjectInstance $Project).Version
# Check if there's an existing installed version of Unity that matches
if (!(Get-UnitySetupInstance | Select-UnitySetupInstance -Version $RequiredVersion))
{
# Download and install
Write-Host "Downloading and installing Unity $RequiredVersion..."
$Installers = Find-UnitySetupInstaller -Version $RequiredVersion -Components $Components
$Installers | Format-Table
Install-UnitySetupInstance -Installers ($Installers)
# Restart Unity Hub if it's running, since it won't pick up the new
# editor by itself
$UnityHub = Get-Process "Unity Hub" -ErrorAction SilentlyContinue
if ($UnityHub)
{
Write-Host "Restarting Unity Hub"
$UnityHubPath = ($UnityHub | Select-Object -First 1).Path
$UnityHub | Stop-Process
Start-Process $UnityHubPath
}
}
Write-Host "Good to go! (Unity $RequiredVersion)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment