SolidJS wrapper for Unity WebGL — embed Unity WebGL builds in SolidJS apps with two-way communication.
A port of react-unity-webgl by Jeffrey Lanters, adapted to SolidJS primitives.
bun add @dschz/solid-unity-webgl
# or
npm install @dschz/solid-unity-webglimport { Unity, createUnityContext } from "@dschz/solid-unity-webgl";
const App = () => {
const { unityProvider, isLoaded, loadingProgression } = createUnityContext({
loaderUrl: "/Build/MyGame.loader.js",
dataUrl: "/Build/MyGame.data",
frameworkUrl: "/Build/MyGame.framework.js",
codeUrl: "/Build/MyGame.wasm",
});
return (
<>
{!isLoaded && <p>Loading... {Math.round(loadingProgression * 100)}%</p>}
<Unity
unityProvider={unityProvider}
style={{ width: "960px", height: "540px" }}
/>
</>
);
};const { sendMessage } = createUnityContext({ ... });
// Call a public method on a Unity GameObject
sendMessage("GameManager", "SetScore", 100);In your Unity C# code:
// Call this to dispatch an event to SolidJS
Application.ExternalCall("dispatchSolidUnityEvent", "GameOver", score);In your SolidJS component:
const { addEventListener, removeEventListener } = createUnityContext({ ... });
addEventListener("GameOver", (score) => {
console.log("Game over! Score:", score);
});Creates a Unity context. Call at the top level of a component.
Returns:
unityProvider— pass to<Unity>componentisLoaded— reactive booleanloadingProgression— reactive number (0–1)initialisationError— reactiveError | undefinedsendMessage(gameObject, method, param?)— send message to UnityrequestFullscreen(enabled)— toggle fullscreenrequestPointerLock()— capture pointertakeScreenshot(type?, quality?)— capture canvas as base64unload()— gracefully quit the Unity instanceaddEventListener(event, callback)— listen for Unity eventsremoveEventListener(event, callback)— remove listenerUNSAFE__unityInstance— direct Unity instance access
Renders the Unity WebGL canvas. Props:
unityProvider(required)id?— canvas element IDstyle?— inline stylesclass?— CSS classtabIndex?devicePixelRatio?matchWebGLToCanvasSize?disabledCanvasEvents?
Ported from react-unity-webgl by Jeffrey Lanters.