Skip to content

Instantly share code, notes, and snippets.

@hsandt
Last active September 14, 2023 19:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hsandt/be252d3d294a77f55617c31364d457d1 to your computer and use it in GitHub Desktop.
Save hsandt/be252d3d294a77f55617c31364d457d1 to your computer and use it in GitHub Desktop.
Build and run a Unity project on a connected Android device from command-line (requires Unix-like shell on Windows)
# Platform: Windows with Unix-like shell (Git Bash recommended)
#
# Role: pull last repository changes, then build and run on a connected Android device
#
# Usage: ./buildandrun-android.sh [version] [--run-only]
#
# Parameters:
# version version number (e.g. "v0.2", "v4rc3", etc.) for your Unity build name
# --run-only set this if the Unity project was already build and you don't want to rebuild it
#
# Prerequisites:
# - create a repository at your Unity project root and cd there
# - define void CommandLineBuild.BuildAndRunAndroid() editor script in your project to build for Android
# with BuildPipeline.BuildPlayer with BuildOptions.AutoRunPlayer
# see example at https://gist.github.com/hsandt/2696289b21c1f83c8119bd7ee6101420
# - define the PROJECT_NAME below
# Name of your project
PROJECT_NAME="My Project"
# Retrieve version argument
if [[ -z "$1" || $1 == --* ]] ; then
VERSION_FLAGS=""
else
VERSION_FLAGS="-version \"$1\""
fi
# Retrieve --run-only option
if [[ $1 == "--run-only" || $2 == "--run-only" ]] ; then
UNITY_BUILD=no
else
UNITY_BUILD=yes
fi
echo "Pulling last changes..."
git pull && git submodule init && git submodule update --recursive
if [ ! -d "Logs" ] ; then
mkdir "Logs"
fi
if [ ! -d "Build" ] ; then
mkdir "Build"
fi
if [ ! -d "Build/Android" ] ; then
mkdir "Build/Android"
fi
if [[ $UNITY_BUILD == yes ]] ; then
echo "Building Android player..."
if [ ! -z "$VERSION_FLAGS" ] ; then
echo "Parameters: $VERSION_FLAGS"
fi
# use -logFile "Logs/log-build-android.txt" if your prefer outputting log in a file (don't forget to git ignore the Logs folder)
"C:\Program Files\Unity\Editor\Unity.exe" -quit -batchmode -logFile -projectPath "$PWD" -executeMethod CommandLineBuild.BuildAndRunAndroid $VERSION_FLAGS
else
echo "(Run-only)"
fi
echo "Done at $(date '+%Y-%m-%d %H:%M:%S')"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment