|
| 1 | +# ┌──────────────────────────────────────────────────────────────────┐ |
| 2 | +# │ Author: Ivan Murzak (https://github.com/IvanMurzak) │ |
| 3 | +# │ Repository: GitHub (https://github.com/IvanMurzak/Unity-MCP) │ |
| 4 | +# │ Copyright (c) 2025 Ivan Murzak │ |
| 5 | +# │ Licensed under the Apache License, Version 2.0. │ |
| 6 | +# │ See the LICENSE file in the project root for more information. │ |
| 7 | +# └──────────────────────────────────────────────────────────────────┘ |
| 8 | + |
| 9 | +name: "Setup Unity MCP" |
| 10 | +description: "Start MCP Server and Unity Editor in Docker containers with license activation" |
| 11 | + |
| 12 | +inputs: |
| 13 | + unity-email: |
| 14 | + description: "Unity account email for license activation" |
| 15 | + required: true |
| 16 | + unity-password: |
| 17 | + description: "Unity account password for license activation" |
| 18 | + required: true |
| 19 | + unity-project-path: |
| 20 | + description: "Relative path to the Unity project folder" |
| 21 | + required: false |
| 22 | + default: "Unity-MCP-Plugin" |
| 23 | + cache-library: |
| 24 | + description: "Enable caching of the Unity Library folder" |
| 25 | + required: false |
| 26 | + default: "true" |
| 27 | + unity-mcp-tools: |
| 28 | + description: "Comma-separated list of MCP tool IDs to enable (optional)" |
| 29 | + required: false |
| 30 | + default: "" |
| 31 | + |
| 32 | +runs: |
| 33 | + using: "composite" |
| 34 | + steps: |
| 35 | + - name: Read Unity version |
| 36 | + id: unity-version |
| 37 | + run: | |
| 38 | + UNITY_VERSION=$(grep "^m_EditorVersion:" ${{ inputs.unity-project-path }}/ProjectSettings/ProjectVersion.txt | awk '{print $2}') |
| 39 | + echo "unity_version=${UNITY_VERSION}" >> "$GITHUB_OUTPUT" |
| 40 | + shell: bash |
| 41 | + |
| 42 | + - name: Cache Unity Library |
| 43 | + if: inputs.cache-library == 'true' |
| 44 | + uses: actions/cache@v4 |
| 45 | + with: |
| 46 | + path: ${{ inputs.unity-project-path }}/Library |
| 47 | + key: unity-library-${{ steps.unity-version.outputs.unity_version }}-ubuntu-base |
| 48 | + |
| 49 | + - name: Start AI-Game-Developer MCP Server |
| 50 | + run: | |
| 51 | + docker rm -f unity-mcp-server 2>/dev/null || true |
| 52 | +
|
| 53 | + docker run -d \ |
| 54 | + --name unity-mcp-server \ |
| 55 | + --network host \ |
| 56 | + -e MCP_PLUGIN_PORT=8080 \ |
| 57 | + -e MCP_PLUGIN_CLIENT_TRANSPORT=streamableHttp \ |
| 58 | + -e MCP_AUTHORIZATION=none \ |
| 59 | + ivanmurzakdev/unity-mcp-server:latest |
| 60 | +
|
| 61 | + echo "Waiting for MCP Server to be ready..." |
| 62 | + for i in $(seq 1 12); do |
| 63 | + sleep 5 |
| 64 | + if docker logs unity-mcp-server 2>&1 | grep -q "Start listening on port:"; then |
| 65 | + echo "MCP Server is ready."; break |
| 66 | + fi |
| 67 | + echo "Still waiting... ($((i*5))s)" |
| 68 | + if [ "$i" -eq 12 ]; then |
| 69 | + echo "Timeout waiting for MCP Server" |
| 70 | + docker logs unity-mcp-server |
| 71 | + exit 1 |
| 72 | + fi |
| 73 | + done |
| 74 | + shell: bash |
| 75 | + |
| 76 | + - name: Setup Node.js |
| 77 | + uses: actions/setup-node@v4 |
| 78 | + with: |
| 79 | + node-version: "20" |
| 80 | + |
| 81 | + - name: Install unity-license-activate |
| 82 | + run: npm install --global unity-license-activate |
| 83 | + shell: bash |
| 84 | + |
| 85 | + - name: Generate Unity activation file (.alf) |
| 86 | + id: alf |
| 87 | + env: |
| 88 | + UNITY_VERSION: ${{ steps.unity-version.outputs.unity_version }} |
| 89 | + run: | |
| 90 | + mkdir -p "${GITHUB_WORKSPACE}/.unity-license" |
| 91 | +
|
| 92 | + docker run --rm \ |
| 93 | + -v "${GITHUB_WORKSPACE}/.unity-license:/output" \ |
| 94 | + -v "${GITHUB_WORKSPACE}/.unity-license:/root/.local/share/unity3d/Unity/" \ |
| 95 | + -w /output \ |
| 96 | + unityci/editor:ubuntu-${UNITY_VERSION}-base-3 \ |
| 97 | + unity-editor -batchmode -createManualActivationFile -logFile /dev/stdout || true |
| 98 | +
|
| 99 | + ALF_FILE=$(find "${GITHUB_WORKSPACE}/.unity-license" -name "*.alf" | head -1) |
| 100 | + if [ -z "$ALF_FILE" ]; then |
| 101 | + echo "Failed to generate .alf file" |
| 102 | + exit 1 |
| 103 | + fi |
| 104 | + echo "Generated ALF file: $ALF_FILE" |
| 105 | + echo "alf_path=${ALF_FILE}" >> "$GITHUB_OUTPUT" |
| 106 | + shell: bash |
| 107 | + |
| 108 | + - name: Activate Unity license |
| 109 | + run: | |
| 110 | + MAX_RETRIES=5 |
| 111 | + for attempt in $(seq 1 $MAX_RETRIES); do |
| 112 | + echo "=== License activation attempt $attempt of $MAX_RETRIES ===" |
| 113 | + if unity-license-activate \ |
| 114 | + "${{ inputs.unity-email }}" \ |
| 115 | + "${{ inputs.unity-password }}" \ |
| 116 | + "${{ steps.alf.outputs.alf_path }}"; then |
| 117 | + echo "unity-license-activate succeeded on attempt $attempt" |
| 118 | + break |
| 119 | + fi |
| 120 | + if [ "$attempt" -eq "$MAX_RETRIES" ]; then |
| 121 | + echo "All $MAX_RETRIES license activation attempts failed" |
| 122 | + exit 1 |
| 123 | + fi |
| 124 | + echo "Attempt $attempt failed, retrying in 10s..." |
| 125 | + sleep 10 |
| 126 | + done |
| 127 | +
|
| 128 | + echo "--- Searching for .ulf file in workspace root ---" |
| 129 | + find "${GITHUB_WORKSPACE}" -maxdepth 1 -name "*.ulf" -ls 2>/dev/null |
| 130 | +
|
| 131 | + ULF_FILE=$(find "${GITHUB_WORKSPACE}" -maxdepth 1 -name "*.ulf" | head -1) |
| 132 | + if [ -z "$ULF_FILE" ]; then |
| 133 | + echo "Failed to obtain .ulf license file" |
| 134 | + exit 1 |
| 135 | + fi |
| 136 | + echo "Found ULF file: $ULF_FILE" |
| 137 | + cp "$ULF_FILE" "${GITHUB_WORKSPACE}/.unity-license/Unity_lic.ulf" |
| 138 | + echo "--- License directory contents ---" |
| 139 | + ls -la "${GITHUB_WORKSPACE}/.unity-license/" |
| 140 | + echo "Unity license activated successfully" |
| 141 | + shell: bash |
| 142 | + |
| 143 | + - name: Upload error screenshot |
| 144 | + if: failure() |
| 145 | + uses: actions/upload-artifact@v4 |
| 146 | + with: |
| 147 | + name: unity-license-error |
| 148 | + path: error.png |
| 149 | + if-no-files-found: ignore |
| 150 | + |
| 151 | + - name: Start Unity Editor in background |
| 152 | + env: |
| 153 | + UNITY_VERSION: ${{ steps.unity-version.outputs.unity_version }} |
| 154 | + UNITY_MCP_TOOLS: ${{ inputs.unity-mcp-tools }} |
| 155 | + run: | |
| 156 | + docker rm -f unity-editor 2>/dev/null || true |
| 157 | +
|
| 158 | + TOOLS_ENV="" |
| 159 | + if [ -n "$UNITY_MCP_TOOLS" ]; then |
| 160 | + TOOLS_ENV="-e UNITY_MCP_TOOLS=${UNITY_MCP_TOOLS}" |
| 161 | + fi |
| 162 | +
|
| 163 | + docker run -d \ |
| 164 | + --name unity-editor \ |
| 165 | + --network host \ |
| 166 | + -e UNITY_MCP_HOST=http://localhost:8080 \ |
| 167 | + -e UNITY_MCP_KEEP_CONNECTED=true \ |
| 168 | + -e UNITY_MCP_AUTH_OPTION=none \ |
| 169 | + ${TOOLS_ENV} \ |
| 170 | + -v "${GITHUB_WORKSPACE}:/workspace" \ |
| 171 | + -v "${GITHUB_WORKSPACE}/.unity-license:/root/.local/share/unity3d/Unity/" \ |
| 172 | + unityci/editor:ubuntu-${UNITY_VERSION}-base-3 \ |
| 173 | + unity-editor -batchmode -projectPath /workspace/${{ inputs.unity-project-path }} -logFile /dev/stdout |
| 174 | +
|
| 175 | + echo "Waiting for Unity project to load..." |
| 176 | + for i in $(seq 1 48); do |
| 177 | + sleep 5 |
| 178 | + # Fail fast if the container already exited |
| 179 | + if [ "$(docker inspect -f '{{.State.Status}}' unity-editor 2>/dev/null)" = "exited" ]; then |
| 180 | + echo "Unity Editor container exited early!" |
| 181 | + docker logs unity-editor |
| 182 | + exit 1 |
| 183 | + fi |
| 184 | + if docker logs unity-editor 2>&1 | grep -q "Loading completed\|Compilation finished\|All assemblies built"; then |
| 185 | + echo "Unity is ready."; break |
| 186 | + fi |
| 187 | + echo "Still waiting... ($((i*5))s)" |
| 188 | + if [ "$i" -eq 48 ]; then |
| 189 | + echo "Timeout waiting for Unity Editor" |
| 190 | + docker logs unity-editor 2>&1 | tail -50 |
| 191 | + exit 1 |
| 192 | + fi |
| 193 | + done |
| 194 | + shell: bash |
0 commit comments