Skip to content

Instantly share code, notes, and snippets.

@viitahenri
Last active February 8, 2024 17:03
Show Gist options
  • Save viitahenri/bf86d48075c9a138020b037a4732ce63 to your computer and use it in GitHub Desktop.
Save viitahenri/bf86d48075c9a138020b037a4732ce63 to your computer and use it in GitHub Desktop.
Smarter Unity build script for MacOS.
#!/bin/zsh
# Define build targets and descriptions in an associative array
typeset -A build_targets
build_targets=(
["StandaloneOSX"]="Build a macOS standalone (Intel 64-bit)."
["StandaloneWindows"]="Build a Windows standalone."
["iOS"]="Build an iOS player."
["Android"]="Build an Android .apk standalone app."
["StandaloneWindows64"]="Build a Windows 64-bit standalone."
["WebGL"]="Build to WebGL platform."
["WSAPlayer"]="Build an Windows Store Apps player."
["StandaloneLinux64"]="Build a Linux 64-bit standalone."
["PS4"]="Build a PS4 Standalone."
["XboxOne"]="Build a Xbox One Standalone."
["tvOS"]="Build to Apple's tvOS platform."
["Switch"]="Build a Nintendo Switch player."
["Stadia"]="Build a Stadia standalone."
["LinuxHeadlessSimulation"]="Build a LinuxHeadlessSimulation standalone."
["PS5"]="Build to PlayStation 5 platform."
["VisionOS"]="Build a visionOS player."
)
# Usage information
usage() {
local show_targets=$1
echo " Usage: \n\t unity-build [-m Build Method] [-v Unity Version] [-p Project Path] [-t Target Platform]\n"
echo " -h, --help Display this help message"
echo " -m, --method Method Name for building (e.g., Builder.BuildMyClient). Required."
echo " -v, --version Unity Version (e.g., 2023.1.14f1). Defaults to version in ProjectSettings/ProjectVersion.txt"
echo " -p, --path Project Path. Defaults to current directory"
echo " -t, --target Target Platform (e.g., WebGL). Defaults to StandaloneOSX"
if [[ $show_targets == true ]]; then
# Find the maximum length of target names
local max_length=0
for target description in ${(kv)build_targets}; do
(( ${#target} > max_length )) && max_length=${#target}
done
echo "\n Build Targets:"
for target description in ${(kv)build_targets}; do
printf " %-${max_length}s %s\n" "$target" "$description"
done
fi
exit 1
}
# Function to extract Unity version from ProjectSettings/ProjectVersion.txt
get_unity_version() {
local version_file="ProjectSettings/ProjectVersion.txt"
if [ -f "$version_file" ]; then
grep "m_EditorVersion:" "$version_file" | cut -d ' ' -f2
else
echo "Error: Unable to find Unity version file ($version_file)."
exit 1
fi
}
# Default values
UNITY_VERSION=$(get_unity_version)
PROJECT_PATH=$(pwd)
METHOD_NAME=""
TARGET_PLATFORM="StandaloneOSX"
# Parse command-line options
while getopts ":v:p:t:m:h-:" opt; do
case "${opt}" in
v)
UNITY_VERSION=${OPTARG}
;;
p)
PROJECT_PATH=${OPTARG}
;;
t)
TARGET_PLATFORM=${OPTARG}
;;
m)
METHOD_NAME=${OPTARG}
;;
h)
usage true
;;
-)
case "${OPTARG}" in
version)
UNITY_VERSION=$argv[$(( OPTIND + 1 ))]; OPTIND=$(( OPTIND + 2 ))
;;
path)
PROJECT_PATH=$argv[$(( OPTIND + 1 ))]; OPTIND=$(( OPTIND + 2 ))
;;
target)
TARGET_PLATFORM=$argv[$(( OPTIND + 1 ))]; OPTIND=$(( OPTIND + 2 ))
;;
method)
METHOD_NAME=$argv[$(( OPTIND + 1 ))]; OPTIND=$(( OPTIND + 2 ))
;;
help)
usage true
;;
*)
echo "Invalid option: --${OPTARG}" >&2
usage
;;
esac
;;
\?)
echo "Invalid option: -$OPTARG" >&2
usage
;;
:)
echo "Option -$OPTARG requires an argument." >&2
usage
;;
esac
done
# Check if the method name is provided
if [ -z "$METHOD_NAME" ]; then
echo "Error: Method Name not provided."
usage
fi
# Construct the path to the Unity executable
UNITY_EDITOR_PATH="/Applications/Unity/Hub/Editor/$UNITY_VERSION/Unity.app/Contents/MacOS/Unity"
# Call Unity with the specified parameters and redirect log to a file
"$UNITY_EDITOR_PATH" -quit -batchmode -projectPath "$PROJECT_PATH" -executeMethod "$METHOD_NAME" -buildTarget "$TARGET_PLATFORM" -logFile -
# Check if Unity build was successful
if [ $? -eq 0 ]; then
echo "Unity build completed successfully."
else
echo "Unity build failed."
fi
#!/bin/zsh
# Define build targets and descriptions in an associative array
typeset -A build_targets
build_targets=(
["StandaloneOSX"]="Target a macOS standalone (Intel 64-bit)."
["StandaloneWindows"]="Target a Windows standalone."
["iOS"]="Target an iOS player."
["Android"]="Target an Android .apk standalone app."
["StandaloneWindows64"]="Target a Windows 64-bit standalone."
["WebGL"]="Target to WebGL platform."
["WSAPlayer"]="Target an Windows Store Apps player."
["StandaloneLinux64"]="Target a Linux 64-bit standalone."
["PS4"]="Target a PS4 Standalone."
["XboxOne"]="Target a Xbox One Standalone."
["tvOS"]="Target to Apple's tvOS platform."
["Switch"]="Target a Nintendo Switch player."
["Stadia"]="Target a Stadia standalone."
["LinuxHeadlessSimulation"]="Target a LinuxHeadlessSimulation standalone."
["PS5"]="Target to PlayStation 5 platform."
["VisionOS"]="Target a visionOS player."
)
# Usage information
usage() {
local show_targets=$1
echo " Usage: \n\t unity-open [-v Unity Version] [-p Project Path] [-t Target Platform]\n"
echo " -h, --help Display this help message"
echo " -v, --version Unity Version (e.g., 2023.1.14f1). Defaults to version in ProjectSettings/ProjectVersion.txt"
echo " -p, --path Project Path. Defaults to current directory"
echo " -t, --target Target Platform (e.g., WebGL). Defaults to StandaloneOSX"
if [[ $show_targets == true ]]; then
# Find the maximum length of target names
local max_length=0
for target description in ${(kv)build_targets}; do
(( ${#target} > max_length )) && max_length=${#target}
done
echo "\n Build Targets:"
for target description in ${(kv)build_targets}; do
printf " %-${max_length}s %s\n" "$target" "$description"
done
fi
exit 1
}
# Function to extract Unity version from ProjectSettings/ProjectVersion.txt
get_unity_version() {
local version_file="ProjectSettings/ProjectVersion.txt"
if [ -f "$version_file" ]; then
grep "m_EditorVersion:" "$version_file" | cut -d ' ' -f2
else
echo "Error: Unable to find Unity version file ($version_file)."
exit 1
fi
}
# Default values
UNITY_VERSION=$(get_unity_version)
PROJECT_PATH=$(pwd)
TARGET_PLATFORM="StandaloneOSX"
# Parse command-line options
while getopts ":v:p:t:h-:" opt; do
case "${opt}" in
v)
UNITY_VERSION=${OPTARG}
;;
p)
PROJECT_PATH=${OPTARG}
;;
t)
TARGET_PLATFORM=${OPTARG}
;;
h)
usage true
;;
-)
case "${OPTARG}" in
version)
UNITY_VERSION=$argv[$(( OPTIND + 1 ))]; OPTIND=$(( OPTIND + 2 ))
;;
path)
PROJECT_PATH=$argv[$(( OPTIND + 1 ))]; OPTIND=$(( OPTIND + 2 ))
;;
target)
TARGET_PLATFORM=$argv[$(( OPTIND + 1 ))]; OPTIND=$(( OPTIND + 2 ))
;;
help)
usage true
;;
*)
echo "Invalid option: --${OPTARG}" >&2
usage
;;
esac
;;
\?)
echo "Invalid option: -$OPTARG" >&2
usage
;;
:)
echo "Option -$OPTARG requires an argument." >&2
usage
;;
esac
done
# Construct the path to the Unity executable
UNITY_EDITOR_PATH="/Applications/Unity/Hub/Editor/$UNITY_VERSION/Unity.app/Contents/MacOS/Unity"
# Call Unity with the specified parameters and redirect log to a file
nohup "$UNITY_EDITOR_PATH" -projectPath "$PROJECT_PATH" -buildTarget "$TARGET_PLATFORM" > "$HOME/Library/Logs/Unity/Editor.log" 2>&1 &
@viitahenri
Copy link
Author

viitahenri commented Jan 25, 2024

chmod +x ~/unity-build.sh
alias unity-build="~/unity-build.sh"

@viitahenri
Copy link
Author

viitahenri commented Jan 30, 2024

chmod +x ~/unity-open.sh
alias unity-open="~/unity-open.sh"

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