|
| 1 | +# Unity WebGL Startup Sequence (SpaceCraft Template) |
| 2 | + |
| 3 | +This document precisely outlines the step-by-step initialization sequence for the SpaceCraft Unity WebGL application, emphasizing the critical path of function calls and events between HTML, JavaScript (`spacecraft.js`), and the Bridge system. |
| 4 | + |
| 5 | +For detailed information on the Bridge system's internal implementation, refer to its dedicated documentation (e.g., `Bridge/README.md`). |
| 6 | + |
| 7 | +## Core Initialization Flow |
| 8 | + |
| 9 | +1. **HTML Parsing (`index.html`)**: Browser parses HTML, identifies CSS and JS files. |
| 10 | + * CSS (`spacecraft.css`) is requested. |
| 11 | + * External JS libraries (Supabase, QRCode) are requested. |
| 12 | + * Bridge JS (`bridge.js`, `unity.js`) is requested. |
| 13 | + * Application JS (`spacecraft.js`) is requested. |
| 14 | + |
| 15 | +2. **`spacecraft.js` Execution (Initial)**: Browser executes `spacecraft.js` as soon as it's loaded. |
| 16 | + * **`fetch` initiated**: `window.indexDeepPromise = fetch(...)` starts loading `index-deep.json` asynchronously. |
| 17 | + * **`SpaceCraft` object defined**: The global `window.SpaceCraft` object is created. |
| 18 | + * **`DOMContentLoaded` listener attached**: An event listener is set for `DOMContentLoaded` to trigger `SpaceCraft.initializeDOMAndQRCodes()`. |
| 19 | + |
| 20 | +3. **DOM Ready**: Browser fires `DOMContentLoaded` event. |
| 21 | + * **Listener triggers**: `SpaceCraft.initializeDOMAndQRCodes()` is called. |
| 22 | + |
| 23 | +4. **DOM Initialization (`SpaceCraft.initializeDOMAndQRCodes`)**: |
| 24 | + * Checks for `QRCode` library availability. |
| 25 | + * Calls `SpaceCraft.generateQRCodes()`. |
| 26 | + * Calls `SpaceCraft.configureAndLoadUnity()`. |
| 27 | + |
| 28 | +5. **QR Code Generation (`SpaceCraft.generateQRCodes`)**: |
| 29 | + * Reads `SpaceCraft.qrCodeDefinitions`. |
| 30 | + * Loops through definitions, dynamically creating HTML elements (`<a>`, `<svg>`, `<div>`) and invoking `QRCode({...})` for each. |
| 31 | + * Appends generated elements to `#qrcodes-container`. |
| 32 | + |
| 33 | +6. **Unity Configuration (`SpaceCraft.configureAndLoadUnity`)**: |
| 34 | + * Prepares the `config` object for Unity. |
| 35 | + * Dynamically creates a `<script>` tag for `Build/SpaceCraft.loader.js`. |
| 36 | + * Assigns an `onload` handler to this script tag. |
| 37 | + * Appends the script tag to the document, initiating the loader script download. |
| 38 | + |
| 39 | +7. **Unity Loader Execution (`Build/SpaceCraft.loader.js`)**: Browser downloads and executes the loader script. |
| 40 | + * **`createUnityInstance` defined**: The loader script defines the global `createUnityInstance` function. |
| 41 | + * **`onload` handler triggers**: The handler assigned in Step 6 executes. |
| 42 | + * **`createUnityInstance()` called**: The handler calls `createUnityInstance(canvas, config, progressCallback)`. |
| 43 | + |
| 44 | +8. **Unity Engine Loading (`createUnityInstance`)**: |
| 45 | + * Downloads Unity data (`.data`), framework (`.framework.js`), and code (`.wasm`). |
| 46 | + * Initializes the Unity engine and WebGL context. |
| 47 | + * Returns a Promise that resolves when the engine is ready. |
| 48 | + |
| 49 | +9. **Unity Instance Ready (`.then()` handler in `SpaceCraft.configureAndLoadUnity`)**: The Promise from `createUnityInstance` resolves. |
| 50 | + * Fullscreen button handler is attached. |
| 51 | + * **`bridge.start()` called**: `window.bridge.start("WebGL", ...)` is invoked, notifying the Bridge JS that Unity is ready. |
| 52 | + |
| 53 | +10. **Bridge C# Initialization**: The `bridge.start()` call triggers initialization within the Bridge's C# components in Unity. |
| 54 | + * **`StartedUnity` event sent**: The C# `BridgeTransportWebGL` sends the `StartedUnity` event back to JavaScript via JSLib (`_Bridge_SendBridgeToUnityEvents`). |
| 55 | + |
| 56 | +11. **Bridge JS Event Handling (`bridge.js`)**: The Bridge's central event handler receives the `StartedUnity` event. |
| 57 | + * **Callback triggered**: Recognizing the event, `bridge.js` calls `SpaceCraft.loadCollectionsAndCreateSpaceCraft()`. |
| 58 | + |
| 59 | +12. **Collections Load & SpaceCraft Creation (`SpaceCraft.loadCollectionsAndCreateSpaceCraft`)**: |
| 60 | + * **`await window.indexDeepPromise`**: Waits for the JSON data fetch (initiated in Step 2) to complete. |
| 61 | + * **`this.createSpaceCraftObject()` called**: If JSON fetch succeeded, passes the loaded `collections` data. |
| 62 | + |
| 63 | +13. **Bridge Object Creation (`SpaceCraft.createSpaceCraftObject`)**: |
| 64 | + * **`bridge.createObject()` called**: Sends a request to the Bridge containing prefab path, object ID, interests, and initial `collections` data. |
| 65 | + * (Bridge system handles instantiation and data application in Unity). |
| 66 | + * **`this.setupSupabase()` called**: Initiates Supabase setup. |
| 67 | + |
| 68 | +14. **Supabase Setup (`SpaceCraft.setupSupabase`)**: |
| 69 | + * Initializes the Supabase client. |
| 70 | + * Subscribes to `navigators` and `selectors` channels. |
| 71 | + |
| 72 | +## Critical Dependencies & Timing |
| 73 | + |
| 74 | +* **DOM Readiness**: `initializeDOMAndQRCodes` requires `DOMContentLoaded`. |
| 75 | +* **Script Load Order**: `bridge.js`/`unity.js` must load *before* `spacecraft.js`. `qrcode.min.js` must load *before* `generateQRCodes` executes. |
| 76 | +* **Unity Loader**: `createUnityInstance` function must be defined by `Build/SpaceCraft.loader.js` *before* the `onload` handler in `spacecraft.js` calls it. |
| 77 | +* **`StartedUnity` Event**: `SpaceCraft.loadCollectionsAndCreateSpaceCraft` *only* runs if the C# Bridge sends `StartedUnity` and the JS Bridge correctly handles it. |
| 78 | +* **JSON Data**: `SpaceCraft.createSpaceCraftObject` requires the `indexDeepPromise` to resolve successfully *before* it is called. |
| 79 | + |
| 80 | +Failure to respect these dependencies will break the initialization chain. |
0 commit comments