Skip to content

Instantly share code, notes, and snippets.

@StephenHodgson
Created January 5, 2023 23:26
Show Gist options
  • Save StephenHodgson/dc76cf70483cde8aa9327d0e2322185b to your computer and use it in GitHub Desktop.
Save StephenHodgson/dc76cf70483cde8aa9327d0e2322185b to your computer and use it in GitHub Desktop.
Opens project from command line so long as Unity Hub and editor is installed
#!/bin/bash
# Set the directory
directory=$1
# If no directory was specified, use the current working directory
if [ -z "$directory" ]
then
directory=$(pwd)
fi
#echo "Directory: $directory"
# Change to the specified directory
cd "$directory"
# Find all child directories named "ProjectSettings"
project_settings_dirs=$(find . -type d -name "ProjectSettings")
# Print the "project_settings_dirs" directory paths
#echo "ProjectSettings directory paths: $project_settings_dirs"
# Iterate through the directories
for dir in $project_settings_dirs; do
# Check if the ProjectVersion.txt file exists in the directory
if [ -f "$dir/ProjectVersion.txt" ]; then
# Set the directory as the project_settings variable and exit the loop
project_settings=$dir
break
fi
done
# Check if the project_settings variable was set
if [ -z "$project_settings" ]; then
echo "Error: ProjectSettings directory with ProjectVersion.txt file not found"
exit 1
fi
# Set the root folder path to the parent directory of the "ProjectSettings" directory
root=$(dirname "$project_settings")
# Print the root folder path
#echo "Root folder path: $root"
# Read the version from the ProjectVersion.txt file
version=$(cat "$root/ProjectSettings/ProjectVersion.txt" | grep -Eo "[0-9]+\.[0-9]+\.[0-9]+[a-z]*[0-9]*")
# Print the version
#echo "Unity version: $version"
# Find the path of the Unity editor using the Unity Hub CLI
editor=$(Unity\ Hub.exe -- --headless editors -i | grep "$version" | cut -d "," -f2 | sed 's/ installed at //')
# Print the path of the Unity editor
#echo "Unity editor path: $editor"
# Launch the Unity editor using the root folder path as the -projectPath parameter
start "" "$editor" -projectPath "$root"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment