Last active
July 5, 2024 18:34
-
-
Save AAulicino/be021bd644c0d66acd791bac5b3d34bc to your computer and use it in GitHub Desktop.
Open a Unity3D project in MacOS terminal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function unity { | |
forceOpen=false | |
rootDir="." | |
targetVersion="" | |
while [[ $# -gt 0 ]]; do | |
case $1 in | |
-f | --force) | |
forceOpen=true | |
shift | |
;; | |
-v | --version) | |
targetVersion="$2" | |
shift 2 | |
;; | |
*) | |
rootDir=$1 | |
shift | |
;; | |
esac | |
done | |
# If version is not specified, detect from project settings | |
if [ -z "$targetVersion" ]; then | |
if [ ! -d "$projectSettingsDir" ]; then | |
echo "No Unity ProjectSettings folder found at directory: \"$( | |
cd ${rootDir} | |
pwd | |
)\"\nMake sure to target a Unity project root directory." | |
return 1 | |
fi | |
targetVersion=$(grep "m_EditorVersion:" ${rootDir}/ProjectSettings/ProjectVersion.txt | awk '{ print $2}') | |
echo | |
echo "Detected project version: $targetVersion" | |
echo | |
fi | |
editor_paths=$(/Applications/Unity\ Hub.app/Contents/MacOS/Unity\ Hub -- --headless editors --installed | grep -o '/Applications.*') | |
matching_editor=$(echo "$editor_paths" | grep $targetVersion) | |
if [ -z "$matching_editor" ]; then | |
echo "\nNo Unity install found matching version $targetVersion." | |
if [ "$forceOpen" = true ]; then | |
echo "-f or --force was specified. Searching for closest installed version." | |
versions=$(echo "$editor_paths" | grep -oE '[0-9]+\.[0-9]+\.[0-9]+[a-z0-9]*') | |
# First try to find the closest higher version | |
closest_version=$(find_closest_version "up" $versions $targetVersion) | |
if [ -z "$closest_version" ]; then | |
# If no higher version is found, find the closest lower version | |
closest_version=$(find_closest_version "down" $versions $targetVersion) | |
fi | |
if [ -z "$closest_version" ]; then | |
echo "No compatible Unity install found" | |
return 1 | |
fi | |
echo "\nClosest compatible version: $closest_version" | |
matching_editor=$(echo "$editor_paths" | grep $closest_version) | |
else | |
return 1 | |
fi | |
fi | |
$matching_editor/Contents/MacOS/Unity -projectPath $rootDir & | |
disown | |
} | |
function find_closest_version() { | |
local direction=$1 | |
local versions=$2 | |
local targetVersion=$3 | |
if [ "$direction" = "up" ]; then | |
closest_version=$(echo "$versions" | sort -V | awk -v target="$targetVersion" ' | |
BEGIN { | |
split(target, tparts, /[.a-z]/) | |
t_major = tparts[1] | |
t_minor = tparts[2] | |
t_patch = tparts[3] | |
closest = "" | |
} | |
{ | |
split($1, parts, /[.a-z]/) | |
major = parts[1] | |
minor = parts[2] | |
patch = parts[3] | |
if (closest == "" && major >= t_major && minor >= t_minor && patch >= t_patch) | |
{ | |
closest = $1 | |
} | |
} | |
END { | |
print closest | |
}') | |
else | |
closest_version=$(echo "$versions" | sort -rV | awk -v target="$targetVersion" ' | |
BEGIN { | |
split(target, tparts, /[.a-z]/) | |
t_major = tparts[1] | |
t_minor = tparts[2] | |
t_patch = tparts[3] | |
closest = "" | |
} | |
{ | |
split($1, parts, /[.a-z]/) | |
major = parts[1] | |
minor = parts[2] | |
patch = parts[3] | |
if (closest == "" && major <= t_major && minor <= t_minor && patch <= t_patch) | |
{ | |
closest = $1 | |
} | |
} | |
END { | |
print closest | |
}') | |
fi | |
echo $closest_version | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment