Skip to content

Instantly share code, notes, and snippets.

@metaphore
Created January 9, 2023 15:42
Show Gist options
  • Save metaphore/a10f1862fa657a4e036f7553dfc2c881 to your computer and use it in GitHub Desktop.
Save metaphore/a10f1862fa657a4e036f7553dfc2c881 to your computer and use it in GitHub Desktop.
Linux script to launch a Unity Editor (installed from Unity Hub) for the specific project.
#!/bin/bash
# Note that the shell option `nullglob` needs to be set. **It is not set by default.**
# It prevents an error in case the glob (or one of multiple globs) does not match any name.
shopt -s nullglob
PROJECT_DIR=$(realpath "$1")
if [ ! -d "$PROJECT_DIR" ]; then
echo "Project directory is not valid: $1" >&2 &&\
exit 1
fi
# Locked on to the speific Unity version.
UNITY_VERSION_FILTER="2020.3.*"
UNITY_EDITOR_DIRS=("$HOME"/Unity/Hub/Editor/${UNITY_VERSION_FILTER})
if [ ${#UNITY_EDITOR_DIRS} = 0 ]; then
echo "Cannot find any Unity Editor version matching: \"${UNITY_VERSION_FILTER}\""
exit 1
fi
# Select the latest version.
UNITY_EDITOR_DIR="${UNITY_EDITOR_DIRS[-1]}"
UNITY_EDITOR_VER=$(basename "$UNITY_EDITOR_DIR")
UNITY_EDITOR="$UNITY_EDITOR_DIR/Editor/Unity"
echo "Unity Editor $UNITY_EDITOR_VER"
echo "Opening project: $PROJECT_DIR"
# POSIX way of running a command detached.
# https://stackoverflow.com/a/10408906/3802890
#nohup "$UNITY_EDITOR" -projectPath "$PROJECT_DIR" </dev/null >/dev/null 2>&1 &
# Bash way of running a command detached.
"$UNITY_EDITOR" -projectPath "$PROJECT_DIR" &
disown
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment