NLDClient-yudde/Tool/dll_tools/open_unity.sh

148 lines
4.4 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/bin/bash
# 打开unity并等待编译完成后自动退出
# 当前目录在C:/Work/Projects/NLDClient/Tool/dll_tools
# 找到同级别C:/Work/Projects/NLDClient/ProjectNLD
# 需要删除 C:/Work/Projects/NLDClient/ProjectNLD/Assets/Code/Scripts/GamePlay_Dll
# 打开unity
# 等待编译完成
# 退出unity
# 获取当前脚本所在目录的绝对路径
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# 基于当前脚本位置计算PROJECT_PATH
# 当前目录是C:/Work/Projects/NLDClient/Tool/dll_tools
# 我们需要向上两级再进入ProjectNLD目录
PROJECT_PATH="$(dirname "$(dirname "$SCRIPT_DIR")")/ProjectNLD"
echo "Calculated PROJECT_PATH: $PROJECT_PATH"
# 检查PROJECT_PATH是否存在
if [ ! -d "$PROJECT_PATH" ]; then
echo "PROJECT_PATH does not exist: $PROJECT_PATH"
exit 1
fi
echo "PROJECT_PATH exists: $PROJECT_PATH"
# 检查本地是否有local_env.sh文件, 如果有则使用其中的路径, 里面定义了UNITY_PATH
# 如果没有则使用默认的unity地址
if [ -f ./local_env.sh ]; then
source ./local_env.sh
echo "found the local_env file"
else
# 没有则使用默认的unity地址
# UNITY_PATH="/Applications/Unity/Hub/Editor/2022.3.50f1c1/Unity.app/Contents/MacOS/Unity"
echo "local_env.sh not found, using default UNITY_PATH"
UNITY_PATH="/Applications/Unity/Hub/Editor/2022.3.50f1c1/Unity.app/Contents/MacOS/Unity"
fi
# 输出UNITY_PATH
echo "UNITY_PATH: $UNITY_PATH"
# 删除指定文件夹
NEED_DELETE_PATH="$PROJECT_PATH/Assets/Code/Scripts/GamePlay_Dll"
if [ -d "$NEED_DELETE_PATH" ]; then
echo "Deleting $NEED_DELETE_PATH"
rm -rf "$NEED_DELETE_PATH"
else
echo "$NEED_DELETE_PATH does not exist, skipping deletion."
fi
# 创建临时日志文件
LOG_FILE="unity_build_$(date +%Y%m%d_%H%M%S).log"
echo "Unity build log will be saved to $LOG_FILE"
# 创建一个临时的编辑器脚本用于在编译完成后退出Unity
TEMP_SCRIPT="$PROJECT_PATH/Assets/Editor/TempQuitAfterBuild.cs"
mkdir -p "$(dirname "$TEMP_SCRIPT")"
cat > "$TEMP_SCRIPT" << 'EOL'
using UnityEditor;
using UnityEngine;
[InitializeOnLoad]
public class QuitAfterBuild
{
static QuitAfterBuild()
{
EditorApplication.update += WaitForCompilation;
}
static void WaitForCompilation()
{
if (!EditorApplication.isCompiling)
{
Debug.Log("Compilation finished. Quitting Unity...");
EditorApplication.update -= WaitForCompilation;
EditorApplication.Exit(0);
}
}
}
EOL
echo "Starting Unity in batchmode..."
# 使用Unity的batchmode命令行参数打开项目
"$UNITY_PATH" -projectPath "$PROJECT_PATH" -logFile "$LOG_FILE" &
# 保存Unity进程ID
UNITY_PID=$!
echo "Unity started with PID: $UNITY_PID"
echo "Waiting for Unity to complete compilation and exit..."
# 设置最大等待时间(以秒为单位)
MAX_WAIT_TIME=3000 # 50分钟
START_TIME=$(date +%s)
# 等待Unity进程结束或超时
while kill -0 $UNITY_PID 2>/dev/null; do
# 检查是否超时
CURRENT_TIME=$(date +%s)
ELAPSED_TIME=$((CURRENT_TIME - START_TIME))
if [ $ELAPSED_TIME -gt $MAX_WAIT_TIME ]; then
echo "Timeout reached after $MAX_WAIT_TIME seconds. Unity might be stuck."
echo "Killing Unity process..."
kill $UNITY_PID
sleep 2
# 如果进程仍然存在,强制终止
if kill -0 $UNITY_PID 2>/dev/null; then
kill -9 $UNITY_PID
fi
echo "Unity process was terminated due to timeout."
rm -f "$TEMP_SCRIPT"
exit 1
fi
# 检查日志文件中是否有编译错误
if [ -f "$LOG_FILE" ] && grep -q "CompilerErrorException" "$LOG_FILE"; then
echo "Compilation errors detected in log file."
echo "Killing Unity process..."
kill $UNITY_PID
sleep 2
if kill -0 $UNITY_PID 2>/dev/null; then
kill -9 $UNITY_PID
fi
echo "Unity process was terminated due to compilation errors."
rm -f "$TEMP_SCRIPT"
exit 1
fi
# 每秒检查一次
sleep 1
done
# 获取Unity进程的退出状态
wait $UNITY_PID
EXIT_CODE=$?
# 删除临时编辑器脚本
rm -f "$TEMP_SCRIPT"
if [ $EXIT_CODE -ne 0 ]; then
echo "Unity process exited with error code: $EXIT_CODE"
echo "Check the build log for errors: $LOG_FILE"
exit $EXIT_CODE
else
echo "Unity process has exited successfully."
echo "Build log available at: $LOG_FILE"
fi