$ show jeffreylanters/react-unity-webgl
This is a TypeScript-first React library (`react-unity-webgl`) that embeds Unity WebGL builds into React applications. The `module/source` directory contains the actual npm package: a `Unity` forward-ref component that mounts a canvas and manages the Unity instance lifecycle, plus custom hooks (`useUnityContext`, `useUnityLoader`, `useEventSystem`, `useUnityMetricsInfo`) that encapsulate script loading, two-way UnityβReact event bridging, fullscreen/pointer-lock/screenshot controls, and performance metric polling. The repo is a monorepo with a Docusaurus documentation site (versioned v5 through current) and a Vite + React integration-test harness backed by two prebuilt Unity test projects (2022.1 and 6000.1). Architecturally, Unity instances push events to React through a global `dispatchReactUnityEvent` function wired into the hook-level event system; React sends messages back through `UnityInstance.SendMessage`. The library has no runtime dependencies beyond React peer support, relying on Unity's generated loader/framework/wasm output and on browser WebGL, IndexedDB, and sensor APIs. A notable implementation detail is a cleanup-on-unmount routine that swaps in a temporary detached canvas to safely call Unity's `Quit()` method. The codebase is well organized and strongly typed, with clear separation between components, hooks, and types. CI workflows for CodeQL, module validation, and documentation validation are present, and the last commit is recent. However, there are no automated unit tests (only a manual integration app), the lint script is a placeholder, CI uses older action versions, and the Unity version-specific bridge code will need maintenance as Unity evolves.
The LLM identified 3 files worth highlighting for reuse:
Self-contained hook that injects an external Unity loader script via a dynamically created script tag, tracks its load status, and exposes that state to React consumers. The pattern generalizes cleanly to any library that must load a global SDK script before use.
Typed event subscription system that connects Unity's global event dispatch mechanism to React listeners. It encapsulates add/remove listener registration in an isolated hook and is a good reference for bridging external global callback APIs into a React-friendly event model.
Hook that periodically polls Unity's getMetricsInfo API and formats metrics for React components. The interval-driven polling, state management, and cleanup logic are generic enough to reuse for any periodic metrics or status source.