Skip to content

Instantly share code, notes, and snippets.

@instance-id
Last active October 3, 2022 18:32
Show Gist options
  • Save instance-id/3161cc2b5343db5bc3cef494d83a7449 to your computer and use it in GitHub Desktop.
Save instance-id/3161cc2b5343db5bc3cef494d83a7449 to your computer and use it in GitHub Desktop.
Launch unity project by name (automatically in correct editor version). Automatically fix Unity: "Failed to load layout window" issue. Create new project from CLI. Add packages / assets to project before launch
#!/usr/bin/env pwsh
<#
.NOTES
============================================================
Last Edit: 5/29/2021
Created by: instance.id (https://github.com/instance-id)
Platform: Windows/Linux
Filename: unitytool.ps1
PSVersion: Last tested with 7.2-preview5
Requirements: fd : https://github.com/sharkdp/fd
============================================================
.DESCRIPTION
Unity multi tool, launcher, version upgrade, package/asset installer, default layout fix
.EXAMPLE
.\unitytool.ps1 "MyProjectName" -Fix -Run
.\unitytool.ps1 "MyProjectName" -fr -s
.\unitytool.ps1 . -s
.PARAMETER Project
The name of the project/root folder
.PARAMETER Path
The Path to the project root folder
.PARAMETER Fix
Automatically fix the "Failed to load layout window" issue
.PARAMETER Run
Start the project up after fixing the issue if included, or will simply open the project if fix is not needed.
.PARAMETER FRun
Both -Fix and -Run combined for ease of use
.PARAMETER Open
Open the folder in the file explorer
.PARAMETER Setup
Add custom packages and assets to the project before starting it
.PARAMETER Create
Create a new Unity project, must include Project as name and Path for location
.PARAMETER Backup
Execute the specified backup script and pass it the project name and location
.PARAMETER SetVersion
Open project with a version that is different than the currently specified version
#>
Param (
[Parameter(Position = 0)]
[string]$Project,
[Alias('p')][string]$Path,
[Alias('f')][switch]$Fix,
[Alias('r')][switch]$Run,
[Alias('o')][switch]$Open,
[Alias('s')][switch]$Setup,
[Alias('fr')][switch]$FRun,
[Alias('b')][switch]$Backup,
[Alias('c')][switch]$Create,
[Alias('v')][string]$SetVersion
)
# Checking to make sure project name and Unity version are included
if ($Project) { Write-Host "Locating ${Project}..." }
else { Write-Host 'Unity project name required...'; exit }
# -- Possibly needed for starting batch mode, so I have heard.
# -- I didn't find that I needed it, though
$licenseFile = '/.local/share/unity3d/Unity/Unity_lic.ulf'
# -------------------------------------- Packages
# -- Either create an environmental variable named $PACKAGES
# -- which points to the location you store your custom packages
# -- or change $packages below to the hardcoded path
# $packages = "/path/to/custom/packages"
$assets = $env:ASSETS
$packages = $env:PACKAGES
<#
Example package setup:
$packageDict = [PSCustomObject]@{
'com.domain.githubpackage1' = "https://github.com/com.domain.package2/MyPackage1"
'com.domain.localpackage2' = "file:${packages}/com.domain.package2/Assets/instance.id/Package2"
'com.domain.package3' = "file:${packages}/com.domain.package2/Assets/instance.id/Package3"
}
#>
$packageDict = [PSCustomObject]@{ 'id.instance.projectinit' = "file:${packages}/id.instance.projectinit/Assets/instance.id/ProjectInit" }
# ------------------------------- System Specific
$projectBackupScript = ''
if ($IsWindows) {
$driveDir = 'E:'
$projectBackupScript = "$HOME/.backup/_projects/projects_backup.ps1"
$hubLocation = 'C:/Program Files/Unity/Hub/Editor'
$unity = 'Unity.exe'
}
if ($IsLinux) {
$driveDir = '/mnt/x'
$projectBackupScript = "$HOME/.backup/_projects/projects_backup.ps1"
$hubLocation = "$HOME/Unity/Hub/Editor"
$unity = 'Unity'
}
$projectFound = $false
$packageResult = $false
# ------------------------------------------------------------------------------------------
# Input the root folder location that contain your Unity projects --------------------------
# Ex: "C:/Unity/MyProjects" : If located in multiple folders,
# enter the parent which contains all of them. This now uses fd to search, and it is
# extremely fast.
# (Prior search, which took 3-4 minutes on a Sabrent 4.0 Rocket NVME, fd takes 4-5 seconds)
# -- Default Unnity project search root
$location = '/mnt/x/'
# -- Default is overridden by passing $Path from CLI
if (!([string]::IsNullOrEmpty($Path))) { $location = $Path }
# -- If you plan to also use my backup script, put it's path here
$projectBackupScript = "$HOME/.backup/_projects/projects_backup.ps1"
# -- Needed variables for file names and locations
# ------------------------------ Layout
$originalLayout = 'Default.wlt'
$destinationLayout = 'CurrentLayout-default.dwlt'
$destinationLayoutPath = "/Library/${destinationLayout}"
$editorLocation = ''
# ---------------------------- Location
$folderName = ''
$projectPath = ''
$destinationPath = ''
# --------------------- Project Version
$versionNumber = ''
$projectVersionFile = ''
$projectVersionTxt = 'ProjectVersion.txt'
$editorVersions = [System.Collections.ArrayList]::new()
# --------------------------------------------- Helper Functions
# --------------------------------------------------------------
# ----------------------------- Select project version
function SelectVersion {
$editorPaths = (get-item $hubLocation).FullName | gci -Directory
$editorPaths | % { $versionNum = (get-item $_).BaseName; $null = $editorVersions.Add($versionNum) }
$editorVersions.Reverse()
$index = 0
$indexVal = ''
write-host "Select Version of Unity for Project: ${Project}"
foreach ($item in $editorVersions) {
if ($index -lt 10) { $indexVal = "[0${index}]" }
else { $indexVal = "[${index}]" }
write-host "${indexVal} ${item}"
$index++
}
[int]$result = Read-Host 'Enter integer value from above'
return $result
}
# ------------------------ Get project current version
function GetVersion {
$versionSearch = ‘*m_EditorVersion: *’
$file_data = Get-Content $projectVersionFile | Where-Object { $_ -like "${versionSearch}"; }
$separator = ': '
$versionString = $file_data.split($separator);
$version = ${versionString}[1]
return $version
}
# ----------------------------- Add packages to project manifest
# --------------------------------------------------------------
function AddPackages {
# ----------------- Copy Standard Assets folder into project
$assetSource = [System.IO.Path]::Combine($assets, 'Standard Assets', 'Essentials')
$assetPath = [System.IO.Path]::Combine($projectPath, 'Assets')
if (Test-Path $assetSource) {
if (!(Test-Path $([System.IO.Path]::Combine($projectPath, 'Assets', 'Standard Assets', 'Essentials')))) {
Copy-item -Force -Recurse -Path $assetSource -Destination $assetPath
} else { write-host 'Essential assets already exist in project' }
}
# --------- Edit the project manifest to add custom packages
$manifest = [System.IO.Path]::Combine($projectPath, 'Packages', 'manifest.json')
if (!(Test-Path $manifest)) { Write-Host 'Could not locate project manifest.json. Skipping package injection' -ForegroundColor Red; return }
$json = Get-Content -Encoding UTF8 $manifest | ConvertFrom-Json
$packageDict.PSObject.Properties | % {
if (!($json.dependencies | Get-Member -Type NoteProperty -Name "$($_.Name)")) {
write-host "Adding Package $($_.Name) to manifest.json"
$json.dependencies | Add-Member -Type NoteProperty -Name "$($_.Name)" -Value "$($_.Value)" -ErrorAction SilentlyContinuepower
$packageResult = $true
} else { write-host "Package $($_.Name) already exists" }
}
if ($packageResult) { $json | ConvertTo-Json | Set-Content -Encoding UTF8 $manifest }
}
function WriteError($type, $path, $folder) { Write-Host "Error creating folder: ${folder} in ${path}"; exit }
# -------------------------------------------- Setup new project
# --------------------------------------------------------------
function CreateProjectStructure($projPath, $version) {
$as = 'Assets'
$ps = 'ProjectSettings'
$pv = $projectVersionTxt
$pSettingsTxt = @"
m_EditorVersion: ${version}
m_EditorVersionWithRevision: ${version}
"@
$projectPath = [System.IO.Path]::Combine($projPath, $Project)
$pPath = [System.IO.Path]::Combine($projectPath, 'ProjectSettings')
if (!(Test-Path $projectPath)) { New-Item -Path $projPath -ItemType Directory -Name $Project -Force }
$fPath = get-item $projectPath
if (Test-Path $fPath) { New-Item -Path $fPath -ItemType Directory -Name $as } else { WriteError 'Folder' $fPath $as }
if (Test-Path $fPath) { New-Item -Path $fPath -ItemType Directory -Name $ps } else { WriteError 'Folder' $fPath $ps }
if (Test-Path $pPath) { New-Item -Path $pPath -ItemType File -Name $pv -Value $pSettingsTxt } else { WriteError 'File' $pPath $pv }
write-host 'Project structure created..'
return $fPath
}
function CreateProject {
if ($([string]::IsNullOrEmpty($SetVersion))) {
$result = SelectVersion
$versionNumber = $editorVersions[$result]
} else { $versionNumber = $SetVersion }
write-host "Editor Version ${versionNumber}"
$editorLocation = "$hubLocation/$versionNumber/Editor/$unity"
if (!(Test-Path $editorLocation)) { Write-Host "Unity Editor version: ${versionNumber} not found. Please download or open in another version" -ForegroundColor Red; exit }
if ( [String]::IsNullOrEmpty($Path)) { $Path = Read-Host 'Path for new project?' }
$projectPath = CreateProjectStructure $Path $versionNumber
if ($Setup.IsPresent) {
write-host 'Creating initial project files'
& $editorLocation -projectPath $projectPath -Quit -BatchMode -manualLicenseFile $licenseFile
write-host "Adding custom packages to ${Project}"; AddPackages
& $editorLocation -projectPath $projectPath -Quit -BatchMode -manualLicenseFile $licenseFile
& $editorLocation -projectPath $projectPath
} else { & $editorLocation -projectPath $projectPath }
exit
}
# --------------------------------- Process the project requests
function ProcessProject {
Write-Host "Unity project $Project found: $projectPath"
if ($([string]::IsNullOrEmpty($SetVersion))) {
$versionNumber = GetVersion
$editorLocation = [System.IO.Path]::Combine($hubLocation, $versionNumber, 'Editor', $unity)
} else {
$versionNumber = $SetVersion
$editorLocation = [System.IO.Path]::Combine($hubLocation, $SetVersion, 'Editor', $unity)
Write-Host "Updating project editor version to ${setversion}. Using: ${editorLocation}"
}
# ------------------------------------------ Fix Layout file
if ($FRun -or $Fix) {
$destinationPath = [System.IO.Path]::Combine(${projectPath}, ${destinationLayoutPath})
$originalLayoutFile = "$hubLocation/$versionNumber/Editor/Data/Resources/Layouts/$originalLayout"
if (Test-Path $originalLayoutFile) {
Write-Host "Original layout file for Unity version $versionNumber found..."
if ( [System.IO.File]::Exists($destinationPath)) {
Write-Host "Probematic layout file found: $destinationPath"
# Backup original layout file
Write-Host "Creating backup of original layout file at: ${destinationPath}.bak"
Copy-item -Force -Verbose -Path ${destinationPath} -Destination "${destinationPath}.bak"
# Copy Default layout for editor version from install folder and rename to required filename
Write-Host "Copying default layout for version $versionNumber to $Project Library..."
Copy-item -Force -Verbose -Path $originalLayoutFile -Destination "${destinationPath}"
} else {
Write-Host "Could not locate probematic layout file at location: $destinationPath" -ForegroundColor Red
Write-Host "Please check that Library folder exists at path: ${projectPath}/Library" -ForegroundColor Red
}
} else {
Write-Host "Unity $versionNumber not found" -ForegroundColor Red; return;
}
Write-Host 'Layout fix completed' -ForegroundColor Green
}
# --------------------------- Install any packages or assets
if ($Setup.IsPresent) { write-host "Adding custom packages to ${Project}"; AddPackages }
# -------------------------------------------- Start Project
if ($FRun.IsPresent -or $Run.IsPresent) {
if (Test-Path $editorLocation) {
Write-Host "Starting Project: ${Project}" -ForegroundColor Green
if ($packageResult) {
# -- Quickly opens the project in batch mode and automatically close it so that the newly added packages
# -- can be added, then opens it again normally, so that it will be ready to use
& $editorLocation -projectPath $projectPath -Quit -BatchMode <# -manualLicenseFile $licenseFile #>
& $editorLocation -projectPath $projectPath <# -manualLicenseFile $licenseFile #>
} else {
& $editorLocation -projectPath $projectPath <# -manualLicenseFile $licenseFile #>
}
} else { Write-Host "Unity Editor version: ${versionNumber} not found. Please download or open in another version" -ForegroundColor Red }
}
# ------------------------------------ Open Project Location
if ($Open.IsPresent) {
Write-Host "Opening location: ${foundProjectLocation}"
if ($IsWindows) { & explorer.exe $projectPath } else { & nemo $projectPath }
}
# ------------------------------------------- Backup project
if ($Backup.IsPresent) {
Write-Host "Backing up project ${Project}: ${foundProjectLocation}"
& $projectBackupScript -Project $Project -Source $projectPath
} else { Write-Host "Project: ${Project} Editor: ${versionNumber} Location: ${foundProjectLocation}" }
}
# -------------------------------------------- Program Execution
# --------------------------------------------------------------
if ($Create) {
CreateProject
}
if ($Project -eq '.') {
$projectPath = [System.IO.Directory]::GetCurrentDirectory()
$projectName = (get-item $projectPath).Directory.BaseName
$projectVersionFile = [System.IO.Path]::Combine(${projectPath}, 'ProjectSettings', $projectVersionTxt)
if (!(Test-Path $projectVersionFile)) { Write-Host 'Could not find project version file' -ForegroundColor Red; exit }
if ($([string]::IsNullOrEmpty($SetVersion))) { $versionNumber = GetVersion } else { $versionNumber = $SetVersion }
$editorLocation = "$hubLocation/$versionNumber/Editor/$unity"
if (!(Test-Path $editorLocation)) {
Write-Host "Unity Editor version: ${versionNumber} not found. Please download or open in another version" -ForegroundColor Red; exit
}
if ($Setup.IsPresent) {
# -- Add custom packages and assets to project and run project
write-host "Adding custom packages to ${projectName}"; AddPackages
# -- Determine if Unity is already opened with this project
$Programs = 'Unity'
$Running = Get-Process -Name $Programs -ErrorAction SilentlyContinue
# -- If project is already running, exit script
$exists = $false
foreach ($item in $Running.CommandLine) {
write-host $item
if ( $item.ToString().EndsWith($Project)) {
$exists = $true
write-host "${Project} already running"
exit
}
}
}
if ($packageResult) {
# -- Quickly opens the project in batch mode and automatically close it so that the newly added packages
# -- can be added, then opens it again normally, so that it will be ready to use
& $editorLocation -projectPath $projectPath -Quit -BatchMode <# -manualLicenseFile $licenseFile #>
& $editorLocation -projectPath $projectPath <# -manualLicenseFile $licenseFile #>
} else {
& $editorLocation -projectPath $projectPath <# -manualLicenseFile $licenseFile #>
}
exit
} else {
# -- Using fd's extremely fast search, look for ProjectVersion.txt file in the location specified
[String[]]$results = & fd "${projectVersionTxt}" "${location}" --xdev --follow --hidden
# -- Iterate all found files and determine if they are Unity projects, and which is the one we requested
for ($i = 0; $i -lt $results.Count; $i++) {
$projectVersionFile = $results[$i]
$projectPath = (get-item $projectVersionFile).Directory.Parent
$folderName = (get-item $projectPath).BaseName
if ($folderName -eq $Project) { $projectFound = $true; } else { continue; }
# -- If the correct project has been found, pass it to the ProcessProject method
if ($projectFound) { ProcessProject } else { Write-Host "${Project} not found, or it is not a project folder." -ForegroundColor Red; exit; }
}
}
@Barina
Copy link

Barina commented Mar 25, 2021

On line 91 I had to change this '${versionString}[1]' to this '${versionString}[$versionString.Length - 1]'
for some reason the split function splits the string to 3 components where the second (index 1) is just an empty string
in that specific line the version portion is the "last element" so.... length - 1 :)
BTW using 2020.3.1
and also I needed to change "hubLocation" with the location of my editors in line 95

@instance-id
Copy link
Author

What OS did you happen to try this on? The hublocation should have been able to be set up near line 33 depending on your OS. I tried to make it pretty generic so that as long as your editors were all in the same folder it could easily be changed. I didn't get a chance to add MacOS yet, as I only have a first gen Mac Mini in a closet somewhere and I just have not dug it out in a long time.

@Barina
Copy link

Barina commented Mar 26, 2021

Well I'm on Windows but my hub path is different (can't remember if I changed it..) at "C:\Program Files\Unity Hub" and the $IsWindows condition didn't worked for me for some reason so I just removed the if block keeping the content :)
All works pretty nice now thanks for that script saves us a lot of time :D

@instance-id
Copy link
Author

Ah, I wrote and tested this using PowerShell 7.2, I suppose I should make note of that in the description. If you are not using PowerShell Core, or possibly just an earlier version, there is a chance that things might act a bit different (such as the string split, it sounds like). It's a shame I had to even write this script in the first place, but I was just getting that error so often I wanted to throw Unity out the window, lol. Glad it was able to help you as well. 👍

@bloodfor1
Copy link

fd
no info on how to run it

Line |
 374 |      [String[]]$results = & fd "${projectVersionTxt}" "${location}" -- …
     |                             ~~
     | The term 'fd' is not recognized as a name of a cmdlet, function, script file, or executable program.
     | Check the spelling of the name, or if a path was included, verify that the path is correct and try
     | again.

@instance-id
Copy link
Author

Ah, my apologies. fd is an extremely fast search tool written in rust that I was using to locate the Unity projectversion file to tell which folders were unity projects.

https://github.com/sharkdp/fd

@bloodfor1
Copy link

Ah, my apologies. fd is an extremely fast search tool written in rust that I was using to locate the Unity projectversion file to tell which folders were unity projects.

https://github.com/sharkdp/fd

i downloaded it but i don't know how to use it what should i do

@instance-id
Copy link
Author

If you are on Windows, you would have to either put the fd.exe file into a folder that is already added to your systems environment variables $PATH, or add the folder in which the fd.exe file is located to the $PATH environment variable list under the advanced system properties window. This is so that the script can locate the fd executable.

The last option would be to change that line like this, but changing the "C:\Path\To" part to the path in which you actually extracted the fd.exe program :

[String[]]$results = & C:\Path\To\fd.exe "${projectVersionTxt}" "${location}"

@bloodfor1
Copy link

bloodfor1 commented Oct 2, 2022

[ String []] $results  =  & C:\Path\To\ fd.exe  " ${projectVersionTxt} "  " ${konum} "

Could you send me this compiled? I don't have .exe

@instance-id
Copy link
Author

@bloodfor1
Copy link

This should be the compiled version - https://github.com/sharkdp/fd/releases/download/v8.4.0/fd-v8.4.0-x86_64-pc-windows-gnu.zip

thank you so much my friend

@bloodfor1
Copy link

bloodfor1 commented Oct 2, 2022

PS C:\> .\unitytool.ps1 "new" -Fix -Run
Locating new...
[fd error]: Search path '/mnt/x/' is not a directory.
[fd error]: No valid search paths given.
PS C:\>

` # -- Using fd's extremely fast search, look for ProjectVersion.txt file in the location specified
[String[]]$results = & C:\fd\fd.exe "${projectVersionTxt}" "${location}"

# -- Iterate all found files and determine if they are Unity projects, and which is the one we requested
for ($i = 0; $i -lt $results.Count; $i++) {
    $projectVersionFile = $results[$i]

`

I couldn't solve this problem my friend can't find it

@instance-id
Copy link
Author

If you look at lines 102 - 110, there are some notes about changing that variable to the path in which you keep all of your Unity projects. That path would be different for everyone, it just depends on where you store your projects. I don't use windows, which is why it was currently set to /mnt/x/ which is my second drive in my PC. You will have to change the $location = '/mnt/x/' to match where your projects are.

# ------------------------------------------------------------------------------------------
# Input the root folder location that contain your Unity projects --------------------------
# Ex: "C:/Unity/MyProjects" : If located in multiple folders,
# enter the parent which contains all of them. This now uses fd to search, and it is
# extremely fast.
# (Prior search, which took 3-4 minutes on a Sabrent 4.0 Rocket NVME, fd takes 4-5 seconds)

# -- Default Unnity project search root
$location = '/mnt/x/'

@bloodfor1
Copy link

`PS C:\> .\unitytool.ps1 "new" -Fix -Run
Locating new...
Unity project new found: C:\Users\xxxx\OneDrive\Desktop\new
Unity 2019.3.0f1 not found
PS C:\>`

Now I am faced with a very sad situation. I'm sorry, I gave you trouble

@instance-id
Copy link
Author

Hmm.. do you have your actual Unity editors installed in the default location?

The script attempts to launch the project using the editor version that it finds in the ProjectVersion.txt file, but by default it is set to look for the editor in (line 88) $hubLocation = 'C:/Program Files/Unity/Hub/Editor'. Based on that error message, it looks like it detects the project as using editor version 2019.3.0f1, but it is not able to find the actual Unity.exe for version 2019.3.0f1. Do you have 2019.3.0f1 currently installed? If so, is the folder for it located in "C:/Program Files/Unity/Hub/Editor"?

@bloodfor1
Copy link

bloodfor1 commented Oct 2, 2022

Currently installed but not under unity hub

Unity 2019.3.0f1 (64-bit)

if ($IsWindows) {
    $driveDir = 'C:'
    $projectBackupScript = "$HOME/.backup/_projects/projects_backup.ps1"
    $hubLocation = 'C:\Program Files\Unity\Editor'
    $unity = 'Unity.exe'

I made the settings but it still gives an error

PS C:> .\unitytool.ps1 "new" -Fix -Run
Locating new...
Unity project new found: C:\Users\xxxxx\OneDrive\Desktop\new
Unity 2019.3.0f1 not found

@instance-id
Copy link
Author

Unfortunately, I don't use Windows, so I don't currently have a way to try and reproduce this on my end.

Is the location of the actual 2019 Unity.exe C:\Program Files\Unity\Editor\2019.3.0f1\Editor\Unity.exe, or is the path different?

@bloodfor1
Copy link

bloodfor1 commented Oct 2, 2022

C:\Program Files\Unity\Editor\2019.3.0f1\Editor\Unity.exe

When I changed the folder location as I wrote, it worked, but it did not solve the problem.

`PS C:\> .\unitytool.ps1 "new" -Fix -Run
Locating new...
Unity project new found: C:\Users\xxx\OneDrive\Desktop\new
Original layout file for Unity version 2019.3.0f1 found...
Could not locate probematic layout file at location: /Library/CurrentLayout-default.dwlt
Please check that Library folder exists at path: C:\Users\xxxxx\OneDrive\Desktop\new/Library
Layout fix completed
Starting Project: new
Project: new Editor: 2019.3.0f1 Location:`


``` Solved
$originalLayout = 'Default.wlt'
$destinationLayout = 'CurrentLayout-default.dwlt'
$destinationLayoutPath = "C:\Users\xxx\OneDrive\Desktop\new\Library\${destinationLayout}"
$editorLocation = ''

@bloodfor1
Copy link

PS C:\> .\unitytool.ps1 "new" -Fix
Locating new...
Unity project new found: C:\Users\xxxx\OneDrive\Desktop\new
Original layout file for Unity version 2019.3.0f1 found...
Probematic layout file found: C:\Users\xxxx\OneDrive\Desktop\new\Library\CurrentLayout-default.dwlt
Creating backup of original layout file at: C:\Users\xxxx\OneDrive\Desktop\new\Library\CurrentLayout-default.dwlt.bak
VERBOSE: Performing the operation "Copy File" on target "Item: C:\Users\xxxx\OneDrive\Desktop\new\Library\CurrentLayout-default.dwlt Destination: C:\Users\xxxx\OneDrive\Desktop\new\Library\CurrentLayout-default.dwlt.bak".
Copying default layout for version 2019.3.0f1 to new Library...
VERBOSE: Performing the operation "Copy File" on target "Item: C:\Program Files\Unity\Hub\Editor\2019.3.0f1\Editor\Data\Resources\Layouts\Default.wlt Destination: C:\Users\xxxx\OneDrive\Desktop\new\Library\CurrentLayout-default.dwlt".
Layout fix completed
Project: new Editor: 2019.3.0f1 Location:

I don't know if it works this way. because Failed to load windows layout is in progress.

@instance-id
Copy link
Author

instance-id commented Oct 2, 2022

Are you using PowerShell v7.2.x or above? I have not tested this script with any version lower than that. If you are using the PowerShell that is built into Windows, it is only version 5.x, so I can't say for sure that things will work as intended.
Line 10 has notes on that:

<#
    ...
    PSVersion:    Last tested with 7.2-preview5
#>

That said, line 269 has the following text:

if ( [System.IO.File]::Exists($destinationPath)) {

You can try changing it to this:

if (Test-Path $destinationPath) {

I think that might be more 'friendly' to older PowerShell if you are using that.

Edit ----

You might also have to change line 264:

From:

$destinationPath = [System.IO.Path]::Combine(${projectPath}, ${destinationLayoutPath})

To:

$destinationPath = "${projectPath}${destinationLayoutPath}"

@bloodfor1
Copy link

my ps version is 7.2.0

@instance-id
Copy link
Author

My apologies, I did not see your updated posts.

Looking at the log where it says:

VERBOSE: Performing the operation "Copy File" on target "Item: C:\Program Files\Unity\Hub\Editor\2019.3.0f1\Editor\Data\Resources\Layouts\Default.wlt Destination: C:\Users\xxxx\OneDrive\Desktop\new\Library\CurrentLayout-default.dwlt".

That should be what fixes the issue. The problem I have ran into in the past is that even after fixing it, the next time I opened the editor, it was fixed, but then instantly became broken again, so I had to do it more than once, which was the whole reason I created this script in the first place. I was manually copying the file from the above "target item" to the "Destination" but it was happening so often that I made this script to make it faster when I needed to do that.

@bloodfor1
Copy link

bloodfor1 commented Oct 3, 2022

got it dude this works faster for me i guess. this does not currently fix the problem

@instance-id
Copy link
Author

Does it happen to fix it if you do the steps below manually, or do you still get the same issue?

Taking this file, and making a copy of it C:\Program Files\Unity\Hub\Editor\2019.3.0f1\Editor\Data\Resources\Layouts\Default.wlt
Then renaming the new copy to: CurrentLayout-default.dwlt
Then removing the file from C:\Users\xxxx\OneDrive\Desktop\new\Library\CurrentLayout-default.dwlt and then putting the new copy in it's place?

@bloodfor1
Copy link

no it doesn't work

@instance-id
Copy link
Author

You could try deleting everything within C:\Users\xxxx\OneDrive\Desktop\new\Library\ (or just move the folder to another location while Unity is closed) and then open Unity again so it can reimport everything. This can take a long time, though, if you have a lot of large files that it has to serialize, but unfortunately, even then it is not 100% guaranteed to work. I don't really know what causes this bug, but it drives me insane. I am assuming that you can't just update to a newer version of the editor? That would certainly be the ideal solution, but of course, that can't always be done with production projects.

@bloodfor1
Copy link

When I upgrade, I encounter many errors in the project.
I will try this now

@bloodfor1
Copy link

bro i need to update the version. We tried everything, it doesn't work, thank you very much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment