|
| 1 | +#!/bin/bash |
| 2 | +#--------------------------------------------------------------- |
| 3 | +# Copyright (C) 2025, inx limited, UK. |
| 4 | +# All Rights Reserved. |
| 5 | +# You may use, distribute and modify this code under the terms |
| 6 | +# of the LGPLv3 license. You should have received a copy of the |
| 7 | +# LGPLv3 (GNU LESSER GENERAL PUBLIC LICENSE Version 3) license |
| 8 | +# with this file. If not, please visit |
| 9 | +# <https://www.gnu.org/licenses/lgpl-3.0.txt> |
| 10 | +#--------------------------------------------------------------- |
| 11 | +# |
| 12 | +# Unity License Setup — GameCI Docker method |
| 13 | +# |
| 14 | +# Activates a Unity Personal license online using your Unity account |
| 15 | +# credentials, then saves the resulting license file so that |
| 16 | +# make targetenv_unity_export_docker can run headless builds using the |
| 17 | +# unityci/editor Docker image (no local Unity Hub install needed). |
| 18 | +# |
| 19 | +# Run this ONCE on any machine that will build Unity targets. |
| 20 | +# Add the export line printed at the end to ~/.bashrc (or your CI |
| 21 | +# secrets store). |
| 22 | +# |
| 23 | +# Usage: |
| 24 | +# ./scripts/build-deploy/unity/setup-unity-license.sh |
| 25 | +# |
| 26 | +# Or with credentials inline (non-interactive, e.g. for CI setup): |
| 27 | +# UNITY_EMAIL=you@example.com UNITY_PASSWORD=secret \ |
| 28 | +# ./scripts/build-deploy/unity/setup-unity-license.sh |
| 29 | +# |
| 30 | +# Requirements: |
| 31 | +# - Docker installed and the current user in the docker group |
| 32 | +# - A free Unity account at https://id.unity.com |
| 33 | +# - Internet access from the Docker container for online activation |
| 34 | +# - 2FA must be disabled on the Unity account used here |
| 35 | +# |
| 36 | +# Unity version targeted by this repo: |
| 37 | +UNITY_VERSION="2022.3.62f3" |
| 38 | +UNITY_DOCKER_IMAGE="unityci/editor:ubuntu-${UNITY_VERSION}-android-3.1.0" |
| 39 | +ACTIVATION_DIR="/tmp/unity-activation" |
| 40 | +CONTAINER_NAME="unity-license-activation-$$" |
| 41 | + |
| 42 | +set -e |
| 43 | + |
| 44 | +echo "======================================================================" |
| 45 | +echo " Unity License Setup for eRT Unity builds" |
| 46 | +echo " Unity version : $UNITY_VERSION" |
| 47 | +echo " Docker image : $UNITY_DOCKER_IMAGE" |
| 48 | +echo "======================================================================" |
| 49 | +echo "" |
| 50 | +echo " NOTE: Unity no longer supports manual offline activation for Personal" |
| 51 | +echo " licenses. This script activates online using your Unity account." |
| 52 | +echo " 2FA must be disabled on the account used here." |
| 53 | +echo "" |
| 54 | + |
| 55 | +# ── Prompt for credentials if not already set ──────────────────────────── |
| 56 | +if [ -z "$UNITY_EMAIL" ]; then |
| 57 | + read -rp "Unity account email: " UNITY_EMAIL |
| 58 | +fi |
| 59 | +if [ -z "$UNITY_PASSWORD" ]; then |
| 60 | + read -rsp "Unity account password: " UNITY_PASSWORD |
| 61 | + echo "" |
| 62 | +fi |
| 63 | + |
| 64 | +if [ -z "$UNITY_EMAIL" ] || [ -z "$UNITY_PASSWORD" ]; then |
| 65 | + echo "ERROR: UNITY_EMAIL and UNITY_PASSWORD must be set." |
| 66 | + exit 1 |
| 67 | +fi |
| 68 | + |
| 69 | +mkdir -p "$ACTIVATION_DIR" |
| 70 | + |
| 71 | +# Clean up any leftover container from a previous failed run |
| 72 | +docker rm "$CONTAINER_NAME" 2>/dev/null || true |
| 73 | + |
| 74 | +# ── Activate online — use a named container so we can docker cp the result ── |
| 75 | +# We do NOT use --rm here so the container's filesystem survives for inspection. |
| 76 | +# -quit tells Unity to exit after activation rather than hanging. |
| 77 | +echo "" |
| 78 | +echo "Activating Unity Personal license online ..." |
| 79 | + |
| 80 | +docker run --name "$CONTAINER_NAME" --privileged --network host \ |
| 81 | + "${UNITY_DOCKER_IMAGE}" \ |
| 82 | + /opt/unity/Editor/Unity \ |
| 83 | + -quit -batchmode -nographics \ |
| 84 | + -username "${UNITY_EMAIL}" \ |
| 85 | + -password "${UNITY_PASSWORD}" \ |
| 86 | + -logFile /dev/stdout || true |
| 87 | + |
| 88 | +# ── Copy the license file out of the stopped container ─────────────────── |
| 89 | +echo "" |
| 90 | +echo "Searching for license file in stopped container ..." |
| 91 | + |
| 92 | +# Try the standard Linux path first, then search broadly if not found |
| 93 | +docker cp "${CONTAINER_NAME}:/root/.local/share/unity3d/Unity/." \ |
| 94 | + "${ACTIVATION_DIR}/" 2>/dev/null || true |
| 95 | + |
| 96 | +ULF_FILE=$(ls "${ACTIVATION_DIR}"/Unity_lic.ulf \ |
| 97 | + "${ACTIVATION_DIR}"/Unity_v*.ulf 2>/dev/null | head -1) |
| 98 | + |
| 99 | +if [ -z "$ULF_FILE" ]; then |
| 100 | + # Broad search: dump all .ulf files from anywhere in the container |
| 101 | + echo "Not found at standard path — searching entire container filesystem ..." |
| 102 | + FOUND_IN_CONTAINER=$(docker exec "$CONTAINER_NAME" \ |
| 103 | + find / -name "*.ulf" 2>/dev/null | head -5 || true) |
| 104 | + if [ -n "$FOUND_IN_CONTAINER" ]; then |
| 105 | + echo "Found .ulf file(s) in container at:" |
| 106 | + echo "$FOUND_IN_CONTAINER" |
| 107 | + FIRST=$(echo "$FOUND_IN_CONTAINER" | head -1) |
| 108 | + docker cp "${CONTAINER_NAME}:${FIRST}" "${ACTIVATION_DIR}/" |
| 109 | + ULF_FILE="${ACTIVATION_DIR}/$(basename "$FIRST")" |
| 110 | + fi |
| 111 | +fi |
| 112 | + |
| 113 | +docker rm "$CONTAINER_NAME" 2>/dev/null || true |
| 114 | + |
| 115 | +if [ -z "$ULF_FILE" ]; then |
| 116 | + echo "" |
| 117 | + echo "======================================================================" |
| 118 | + echo "ERROR: No license file found." |
| 119 | + echo "" |
| 120 | + echo "Possible causes:" |
| 121 | + echo " - Wrong email or password" |
| 122 | + echo " - 2FA is enabled on the Unity account (disable it and retry)" |
| 123 | + echo " - Network not reachable from Docker container" |
| 124 | + echo " - Unity activation API changed for this version" |
| 125 | + echo "" |
| 126 | + echo "Check the Docker log output above for 'license' or 'error' messages." |
| 127 | + echo "======================================================================" |
| 128 | + exit 1 |
| 129 | +fi |
| 130 | + |
| 131 | +echo "" |
| 132 | +echo "License file saved to: $ULF_FILE" |
| 133 | + |
| 134 | +# ── Print the export instruction ───────────────────────────────────────── |
| 135 | +echo "" |
| 136 | +echo "======================================================================" |
| 137 | +echo " License activation complete." |
| 138 | +echo "" |
| 139 | +echo " Add the following line to your ~/.bashrc so that" |
| 140 | +echo " make targetenv_unity_export_docker can use this license:" |
| 141 | +echo "" |
| 142 | +echo " export UNITY_LICENSE=\$(cat '${ULF_FILE}')" |
| 143 | +echo "" |
| 144 | +echo " To apply it in this shell session right now, run:" |
| 145 | +echo " export UNITY_LICENSE=\$(cat '${ULF_FILE}')" |
| 146 | +echo "======================================================================" |
0 commit comments