diff --git a/Assets/Editor/BuildScript.cs b/Assets/Editor/BuildScript.cs new file mode 100644 index 0000000..da612b6 --- /dev/null +++ b/Assets/Editor/BuildScript.cs @@ -0,0 +1,258 @@ +using UnityEditor; +using UnityEditor.SceneManagement; +using System; +using System.IO; +using System.Collections.Generic; + +/// +/// Build script for Sui Unity SDK +/// Supports headless builds for multiple platforms +/// +/// Usage examples: +/// unity -projectPath . -buildTarget WebGL -executeMethod BuildScript.BuildWebGL +/// unity -projectPath . -buildTarget StandaloneWindows64 -executeMethod BuildScript.BuildWindows +/// unity -projectPath . -buildTarget StandaloneOSX -executeMethod BuildScript.BuildMac +/// +public class BuildScript +{ + private static readonly string BUILD_OUTPUT_DIR = "Build"; + private static readonly string LOG_FILE = "Build/build_log.txt"; + + /// + /// Build for WebGL platform + /// + public static void BuildWebGL() + { + BuildTarget(BuildTarget.WebGL, "WebGL", "Build/WebGL"); + } + + /// + /// Build for Windows 64-bit standalone + /// + public static void BuildWindows() + { + BuildTarget(BuildTarget.StandaloneWindows64, "Windows", "Build/Windows/SuiUnity.exe"); + } + + /// + /// Build for macOS standalone + /// + public static void BuildMac() + { + BuildTarget(BuildTarget.StandaloneOSX, "macOS", "Build/macOS"); + } + + /// + /// Build for Linux standalone + /// + public static void BuildLinux() + { + BuildTarget(BuildTarget.StandaloneLinux64, "Linux", "Build/Linux/SuiUnity"); + } + + /// + /// Build for Android + /// + public static void BuildAndroid() + { + BuildTarget(BuildTarget.Android, "Android", "Build/Android"); + } + + /// + /// Build for iOS + /// + public static void BuildiOS() + { + BuildTarget(BuildTarget.iOS, "iOS", "Build/iOS"); + } + + /// + /// Generic build method + /// + private static void BuildTarget(BuildTarget target, string targetName, string outputPath) + { + try + { + CreateBuildDirectory(); + LogMessage($"\n========== Starting {targetName} Build ==========\n"); + LogMessage($"Target: {target}"); + LogMessage($"Output: {outputPath}"); + LogMessage($"Time: {DateTime.Now:yyyy-MM-dd HH:mm:ss}"); + LogMessage("---"); + + // Get all scenes in the project + string[] scenes = GetScenePaths(); + + if (scenes.Length == 0) + { + LogMessage("WARNING: No scenes found in project. Build may fail."); + } + else + { + LogMessage($"Scenes to include ({scenes.Length}):"); + foreach (var scene in scenes) + { + LogMessage($" - {scene}"); + } + } + + LogMessage("\n---\n"); + + // Configure build options + BuildPlayerOptions buildOptions = new BuildPlayerOptions + { + scenes = scenes, + locationPathName = outputPath, + target = target, + options = BuildOptions.None + }; + + // Add development build flag if specified via environment variable + if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("DEVELOPMENT_BUILD"))) + { + buildOptions.options |= BuildOptions.Development; + LogMessage("Development build enabled"); + } + + // Perform the build + BuildReport report = BuildPipeline.BuildPlayer(buildOptions); + HandleBuildResults(report, targetName); + } + catch (Exception ex) + { + string errorMsg = $"Build failed with exception: {ex.Message}\n{ex.StackTrace}"; + LogMessage(errorMsg); + EditorApplication.Exit(1); + } + } + + /// + /// Get all scenes from the project + /// + private static string[] GetScenePaths() + { + List scenePaths = new List(); + string samplesSceneDir = "Assets/SuiUnitySDK/Samples/Scenes"; + + // Add sample scenes if they exist + if (Directory.Exists(samplesSceneDir)) + { + string[] sceneFiles = Directory.GetFiles(samplesSceneDir, "*.unity"); + Array.Sort(sceneFiles); + scenePaths.AddRange(sceneFiles); + } + + // If no scenes found, return empty array (Unity will build with empty scene list) + if (scenePaths.Count == 0) + { + LogMessage("Note: Using empty scene list - no scenes will be included in build"); + } + + return scenePaths.ToArray(); + } + + /// + /// Handle build results and exit with appropriate code + /// + private static void HandleBuildResults(BuildReport report, string targetName) + { + if (report.summary.result == BuildResult.Succeeded) + { + LogMessage($"✓ {targetName} build succeeded!"); + LogMessage($" Output: {report.summary.outputPath}"); + LogMessage($" Build size: {report.summary.totalSize / (1024 * 1024)} MB"); + LogMessage($" Build time: {report.summary.totalTime.TotalSeconds:F2} seconds"); + LogMessage($"\n========== Build Complete ==========\n"); + EditorApplication.Exit(0); + } + else if (report.summary.result == BuildResult.Failed) + { + string errorMsg = $"✗ {targetName} build failed!"; + LogMessage(errorMsg); + + if (report.summary.totalErrors > 0) + { + LogMessage($" Errors: {report.summary.totalErrors}"); + } + + LogMessage($"\n========== Build Failed ==========\n"); + EditorApplication.Exit(1); + } + else if (report.summary.result == BuildResult.Cancelled) + { + LogMessage($"! {targetName} build was cancelled"); + LogMessage($"\n========== Build Cancelled ==========\n"); + EditorApplication.Exit(2); + } + } + + /// + /// Create build directory if it doesn't exist + /// + private static void CreateBuildDirectory() + { + if (!Directory.Exists(BUILD_OUTPUT_DIR)) + { + Directory.CreateDirectory(BUILD_OUTPUT_DIR); + } + } + + /// + /// Log message to both console and log file + /// + private static void LogMessage(string message) + { + Console.WriteLine(message); + + try + { + File.AppendAllText(LOG_FILE, message + Environment.NewLine); + } + catch + { + // Silently fail if log file can't be written + } + } + + /// + /// Print help information + /// + public static void PrintHelp() + { + string help = @" +========== Sui Unity SDK Build Script ========== + +Available build targets: + + BuildWebGL() - Build for WebGL platform + BuildWindows() - Build for Windows 64-bit + BuildMac() - Build for macOS + BuildLinux() - Build for Linux 64-bit + BuildAndroid() - Build for Android + BuildiOS() - Build for iOS + +Usage examples: + + # WebGL Build + unity -projectPath . -buildTarget WebGL -executeMethod BuildScript.BuildWebGL + + # Windows Build (Release) + unity -projectPath . -buildTarget StandaloneWindows64 -executeMethod BuildScript.BuildWindows + + # Windows Build (Development) + DEVELOPMENT_BUILD=1 unity -projectPath . -buildTarget StandaloneWindows64 -executeMethod BuildScript.BuildWindows + + # macOS Build + unity -projectPath . -buildTarget StandaloneOSX -executeMethod BuildScript.BuildMac + + # Audio help + unity -projectPath . -executeMethod BuildScript.PrintHelp + +Output: Builds are created in the 'Build' directory +Log file: Build/build_log.txt + +================================================ +"; + Console.WriteLine(help); + } +} diff --git a/BUILD_SYSTEM.md b/BUILD_SYSTEM.md new file mode 100644 index 0000000..30b31ea --- /dev/null +++ b/BUILD_SYSTEM.md @@ -0,0 +1,237 @@ +# Sui Unity SDK - Build System + +This directory contains build scripts for the Sui Unity SDK project. The build system supports headless builds for multiple platforms using Unity's command-line interface. + +## Quick Start + +### Unix/Linux/macOS +```bash +chmod +x build.sh +./build.sh webgl +``` + +### Windows +```batch +build.bat webgl +``` + +## Build Targets + +| Target | Platform | Best For | +|--------|----------|----------| +| `webgl` | Web Browser (WebGL 2.0) | Testing in browser, web deployment | +| `windows` | Windows 64-bit | Standalone Windows executable | +| `macos` | macOS Universal | Standalone macOS application | +| `linux` | Linux 64-bit | Standalone Linux executable | +| `android` | Android | Mobile Android app | +| `ios` | iOS | Mobile iOS app | +| `all` | All platforms | Build everything (long process) | + +## Usage Examples + +### Basic Builds + +**WebGL (simplest, recommended for testing)** +```bash +./build.sh webgl +``` + +**Windows Standalone** +```bash +./build.sh windows +``` + +**Development Build (with debug symbols)** +```bash +./build.sh webgl -d +./build.sh windows --dev +``` + +### Advanced Usage + +**Using Unity directly** +```bash +unity -projectPath . \ + -buildTarget WebGL \ + -executeMethod BuildScript.BuildWebGL \ + -quit -batchmode -nographics +``` + +**For development/debug builds** +```bash +export DEVELOPMENT_BUILD=1 +unity -projectPath . \ + -buildTarget WebGL \ + -executeMethod BuildScript.BuildWebGL \ + -quit -batchmode -nographics +``` + +**Multiple builds (one at a time)** +```bash +./build.sh windows +./build.sh macos +./build.sh linux +``` + +## Build Configuration + +### C# Build Script +**Location:** `Assets/Editor/BuildScript.cs` + +Key features: +- Automatic scene discovery from `Assets/SuiUnitySDK/Samples/Scenes` +- Build logging to `Build/build_log.txt` +- Support for development builds (with `-d` flag) +- Clear error reporting and exit codes +- Build statistics (size, time, output location) + +### Build Methods + +The BuildScript provides these methods callable via `-executeMethod`: + +- `BuildScript.BuildWebGL` - WebGL build +- `BuildScript.BuildWindows` - Windows 64-bit build +- `BuildScript.BuildMac` - macOS build +- `BuildScript.BuildLinux` - Linux build +- `BuildScript.BuildAndroid` - Android build +- `BuildScript.BuildiOS` - iOS build +- `BuildScript.PrintHelp` - Show help information + +## Build Output + +All builds are created in the `Build/` directory: + +``` +Build/ +├── WebGL/ # WebGL build output +├── Windows/ # Windows executable +├── macOS/ # macOS app bundle +├── Linux/ # Linux executable +├── Android/ # Android APK/AAB +├── iOS/ # iOS Xcode project +└── build_log.txt # Build log with timestamps +``` + +## System Requirements + +- **Unity:** 2022.3.16f1 LTS or later +- **Platform SDKs:** + - Windows: Visual Studio or Build Tools + - macOS: Xcode + - Linux: Build Tools + - Android: Android SDK/NDK + - iOS: Xcode + +- **Disk Space:** Min 10 GB for full builds +- **Time:** Varies by platform and machine (5-30 minutes per build) + +## Scenes + +The build system automatically includes all scenes found in: +``` +Assets/SuiUnitySDK/Samples/Scenes/ +``` + +To include additional scenes: +1. Add them to the project +2. Ensure they're in the Build Settings (File > Build Settings) +3. The script will automatically discover them + +## Troubleshooting + +### Unity not found +``` +Error: Unity not found in PATH +``` +**Solution:** Install Unity 2022.3.16f1+ or add its path to your PATH environment variable. + +### Build fails with script errors +1. Check `Build/build_log.txt` for details +2. Ensure all assets are properly imported +3. Fix any compilation errors in the project + +### WebGL build requires specific settings +The script automatically handles WebGL configuration. Ensure: +- WebGL templates are present (they are) +- Supported formats are enabled in Player Settings + +### Out of disk space +WebGL builds are smallest (~50-150 MB), try building WebGL first to verify setup. + +## Development Workflow + +### During Development +1. Open the project in Unity Editor +2. Make changes and test in Editor +3. Use `./build.sh webgl` for quick web builds +4. Use `./build.sh windows` for testing on desktop + +### Before Release +```bash +./build.sh all +# Or build specific targets: +./build.sh webgl +./build.sh windows +./build.sh linux +``` + +## CI/CD Integration + +### GitHub Actions Example +```yaml +- name: Build WebGL + run: ./build.sh webgl + +- name: Build Windows + run: ./build.sh windows +``` + +### Environment Variables +- `DEVELOPMENT_BUILD=1` - Builds with debug symbols +- `CI=true` - Can be used for CI-specific logic + +## Advanced: Custom Build Scripts + +To create custom build methods, add to `BuildScript.cs`: + +```csharp +public static void CustomBuild() +{ + BuildTarget(BuildTarget.StandaloneWindows64, "Custom", "Build/Custom/output.exe"); +} +``` + +Then call: +```bash +unity -projectPath . -buildTarget StandaloneWindows64 -executeMethod BuildScript.CustomBuild +``` + +## Debugging Build Issues + +### View full build output +```bash +./build.sh webgl 2>&1 | tee build_output.txt +``` + +### Run build in Editor mode (non-headless) +```bash +unity -projectPath . -buildTarget WebGL +# Then use File > Build & Run in the editor +``` + +### Check build log +```bash +cat Build/build_log.txt +tail -f Build/build_log.txt # Watch in real-time +``` + +## Support + +For issues with: +- **Build Script:** Check `Assets/Editor/BuildScript.cs` +- **Project Setup:** Refer to main `README.md` +- **Sui Integration:** See `Assets/SuiUnitySDK/` documentation + +## License + +Part of the Sui Unity SDK - see LICENSE file in project root. diff --git a/build.bat b/build.bat new file mode 100644 index 0000000..da167ce --- /dev/null +++ b/build.bat @@ -0,0 +1,145 @@ +@echo off +REM Sui Unity SDK Build Script for Windows +REM Batch file wrapper to simplify building for different platforms + +setlocal enabledelayedexpansion + +set "PROJECT_PATH=%~dp0" +set "UNITY_VERSION=2022.3.16f1" +set "BUILD_DIR=%PROJECT_PATH%Build" + +:main +if "%1"=="" ( + call :print_usage + exit /b 0 +) + +if "%1"=="webgl" ( + call :check_unity + call :create_build_dir + call :run_build "WebGL" "BuildWebGL" "WebGL" + exit /b !ERRORLEVEL! +) + +if "%1"=="windows" ( + call :check_unity + call :create_build_dir + call :run_build "Windows" "BuildWindows" "StandaloneWindows64" + exit /b !ERRORLEVEL! +) + +if "%1"=="macos" ( + call :check_unity + call :create_build_dir + call :run_build "macOS" "BuildMac" "StandaloneOSX" + exit /b !ERRORLEVEL! +) + +if "%1"=="linux" ( + call :check_unity + call :create_build_dir + call :run_build "Linux" "BuildLinux" "StandaloneLinux64" + exit /b !ERRORLEVEL! +) + +if "%1"=="android" ( + call :check_unity + call :create_build_dir + call :run_build "Android" "BuildAndroid" "Android" + exit /b !ERRORLEVEL! +) + +if "%1"=="ios" ( + call :check_unity + call :create_build_dir + call :run_build "iOS" "BuildiOS" "iOS" + exit /b !ERRORLEVEL! +) + +if "%1"=="help" ( + call :print_usage + exit /b 0 +) + +echo Error: Unknown target: %1 +call :print_usage +exit /b 1 + +:print_usage +echo. +echo ========== Sui Unity SDK Build Script ========== +echo. +echo Usage: build.bat [target] +echo. +echo Targets: +echo webgl - Build for WebGL (recommended for testing) +echo windows - Build for Windows 64-bit standalone +echo macos - Build for macOS standalone +echo linux - Build for Linux 64-bit standalone +echo android - Build for Android +echo ios - Build for iOS +echo help - Show this help message +echo. +echo Examples: +echo build.bat webgl +echo build.bat windows +echo build.bat android +echo. +echo Requirements: +echo - Unity %UNITY_VERSION%+ installed and in PATH +echo - Project path: %PROJECT_PATH% +echo - Output: %BUILD_DIR% +echo. +echo ================================================ +exit /b 0 + +:check_unity +where unity >nul 2>&1 +if errorlevel 1 ( + echo Error: Unity not found in PATH + echo Please install Unity %UNITY_VERSION% or add it to your PATH + exit /b 1 +) +echo [OK] Unity found +exit /b 0 + +:create_build_dir +if not exist "%BUILD_DIR%" ( + mkdir "%BUILD_DIR%" +) +echo [OK] Build directory ready: %BUILD_DIR% +exit /b 0 + +:run_build +setlocal +set "TARGET_NAME=%~1" +set "METHOD=%~2" +set "BUILD_TARGET=%~3" + +echo. +echo ========== Building for %TARGET_NAME% ========== +echo. +echo Command: unity -projectPath "%PROJECT_PATH%" -buildTarget %BUILD_TARGET% -executeMethod BuildScript::%METHOD% -quit -batchmode -nographics +echo. + +unity -projectPath "%PROJECT_PATH%" -buildTarget %BUILD_TARGET% -executeMethod BuildScript::%METHOD% -quit -batchmode -nographics + +if errorlevel 1 ( + echo. + echo [FAILED] Build failed! + if exist "%BUILD_DIR%\build_log.txt" ( + echo. + echo Last 50 lines of build log: + powershell -Command "Get-Content '%BUILD_DIR%\build_log.txt' | Select-Object -Last 50" + ) + endlocal + exit /b 1 +) else ( + echo. + echo [OK] Build succeeded! + endlocal + exit /b 0 +) + +endlocal +exit /b 0 diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..f1b1306 --- /dev/null +++ b/build.sh @@ -0,0 +1,165 @@ +#!/bin/bash + +# Sui Unity SDK Build Script +# Wrapper script to simplify building for different platforms + +set -e + +PROJECT_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +UNITY_VERSION="2022.3.16f1" +BUILD_DIR="$PROJECT_PATH/Build" + +# Colors for output +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +NC='\033[0m' # No Color + +# Print usage information +print_usage() { + cat << EOF +${BLUE}========== Sui Unity SDK Build Script ==========${NC} + +Usage: ./build.sh [target] [options] + +${BLUE}Targets:${NC} + webgl - Build for WebGL (recommended for testing) + windows - Build for Windows 64-bit standalone + macos - Build for macOS standalone + linux - Build for Linux 64-bit standalone + android - Build for Android + ios - Build for iOS + all - Build for all platforms + help - Show this help message + +${BLUE}Options:${NC} + -d, --dev - Build in development mode (includes debug symbols) + -h, --help - Show this help message + +${BLUE}Examples:${NC} + ./build.sh webgl + ./build.sh windows -d + ./build.sh android + ./build.sh help + +${BLUE}Requirements:${NC} + - Unity ${UNITY_VERSION}+ installed and in PATH + - Project path: ${PROJECT_PATH} + - Output: ${BUILD_DIR}/ + +${BLUE}================================================${NC} +EOF +} + +# Check if Unity is installed +check_unity() { + if ! command -v unity &> /dev/null; then + echo -e "${RED}Error: Unity not found in PATH${NC}" + echo "Please install Unity ${UNITY_VERSION} or add it to your PATH" + exit 1 + fi + + echo -e "${GREEN}✓ Unity found:${NC} $(unity -version 2>/dev/null | head -1 || echo 'Unity Editor')" +} + +# Create build directory +create_build_dir() { + mkdir -p "$BUILD_DIR" + echo -e "${GREEN}✓ Build directory ready:${NC} $BUILD_DIR" +} + +# Run build +run_build() { + local target=$1 + local method=$2 + local build_target=$3 + local dev_flag=$4 + + echo -e "\n${BLUE}========== Building for $target ==========${NC}\n" + + local cmd="unity -projectPath \"$PROJECT_PATH\" -buildTarget $build_target -executeMethod BuildScript::$method -quit -batchmode -nographics" + + if [ "$dev_flag" = "true" ]; then + export DEVELOPMENT_BUILD=1 + echo -e "${YELLOW}Development build enabled${NC}" + cmd="$cmd -logFile" + fi + + echo -e "${BLUE}Command:${NC} unity ... -executeMethod BuildScript::$method" + + # Note: This will fail if Unity is not installed, which is expected + eval "$cmd" + + if [ $? -eq 0 ]; then + echo -e "\n${GREEN}✓ Build succeeded!${NC}" + return 0 + else + echo -e "\n${RED}✗ Build failed!${NC}" + if [ -f "$BUILD_DIR/build_log.txt" ]; then + echo -e "\n${BLUE}Build log:${NC}" + tail -50 "$BUILD_DIR/build_log.txt" + fi + return 1 + fi +} + +# Parse arguments +TARGET=${1:-help} +DEV_BUILD=false + +# Check for development flag +if [[ "$2" == "-d" ]] || [[ "$2" == "--dev" ]]; then + DEV_BUILD=true +fi + +# Main script +case $TARGET in + webgl) + check_unity + create_build_dir + run_build "WebGL" "BuildWebGL" "WebGL" "$DEV_BUILD" + ;; + windows) + check_unity + create_build_dir + run_build "Windows" "BuildWindows" "StandaloneWindows64" "$DEV_BUILD" + ;; + macos) + check_unity + create_build_dir + run_build "macOS" "BuildMac" "StandaloneOSX" "$DEV_BUILD" + ;; + linux) + check_unity + create_build_dir + run_build "Linux" "BuildLinux" "StandaloneLinux64" "$DEV_BUILD" + ;; + android) + check_unity + create_build_dir + run_build "Android" "BuildAndroid" "Android" "$DEV_BUILD" + ;; + ios) + check_unity + create_build_dir + run_build "iOS" "BuildiOS" "iOS" "$DEV_BUILD" + ;; + all) + check_unity + create_build_dir + for platform in webgl windows macos linux; do + echo -e "\n${BLUE}Building $platform...${NC}" + run_build "$platform" "Build${platform^}" "$platform" "$DEV_BUILD" || true + done + echo -e "\n${GREEN}All builds completed!${NC}" + ;; + help|--help|-h|"") + print_usage + ;; + *) + echo -e "${RED}Unknown target: $TARGET${NC}" + print_usage + exit 1 + ;; +esac