// Preparing Unity Preferences
const unityConfig: UnityConfig = {
loaderUrl: `${UNITY_BASE_URI()}/Build/Build.loader.js`,
dataUrl: `${UNITY_BASE_URI()}/Build/Build.data.unityweb`,
frameworkUrl: `${UNITY_BASE_URI()}/Build/Build.framework.js.unityweb`,
codeUrl: `${UNITY_BASE_URI()}/Build/Build.wasm.unityweb`,
streamingAssetsUrl: `${UNITY_ASSET_URI()}/StreamingAssets`,
// cacheControl: (url: string) => {
// return "no-store";
// },
companyName: "company",
productName: "productName",
productVersion: "0.1.0.0",
webglContextAttributes: {
...Configure.useWebGLOptions
}
};
// Preparing Unity Libraries
const { unityProvider, isLoaded, loadingProgression, unload, sendMessage, addEventListener, removeEventListener } =
useUnityContext(unityConfig);
// page rendering
<UnityStyled
ref={unityCanvas}
className="unity-player"
tabIndex={-1}
unityProvider={unityProvider}
devicePixelRatio={window.devicePixelRatio}
matchWebGLToCanvasSize={true}
style={{
visibility: isLoaded ? "visible" : "hidden",
width: "100%",
height: "100%",
zIndex: "auto"
}}
/>
// Unity Unload process
1. If you want to stop Unity activation by flow of service process, call unity.unload(), return Promise()
2. Once unity is guaranteed that unload() has completed, remove the unity.canvas DOM.
We are handling it like this but the memory is still increasing every time we load.
I am waiting for your solution. First of all, thank you.
Can you share your unloading code? We found out that Development builds might currently have an issue regarding this (due to Emscripten debug code it injects) but Release builds should be fine.
import { UnityConfig } from "react-unity-webgl/distribution/types/unity-config";
const effector = {
onMount() {
EventHandler.emit("UnityDomMount");
if (unityCanvas.current) {
unityCanvas.current.onmousedown = async (ev: MouseEvent) => await fn.request_pointer_lock(ev);
unityCanvas.current.onmouseup = (ev: MouseEvent) => fn.release_pointer_lock(ev);
}`Preformatted text`
},
onUnmount() {
return () => {
/**
* @link https://react-unity-webgl.dev/docs/api/unload
* ! NOTICE
* Normally it was possible to unmount the Unity Application and
* unmount the containing component right after the unload function was invoked,
* but due to a bug in newer Unity versions this is no longer possible when using a build made with
* Unity 2021.2 or later.
* It is still possible to unload the Unity Application, but the canvas has to be kept mounted until
* the promise is resolved.
* As of writing this, the issue has not been fixed, but it is possible to unmount the Unity Application
* manually by halting the navigation to the next page.
*
unityLoaded &&
unload().then(() => {
setUnityLoaded(false);
});
*/
EventHandler.emit("UnityDomUnmount");
};
},
onFullScreen() {
console.debug(`${LOGTAG} Fullscreen`, isFullScreen);
unityCommand.screenModeChanged(isFullScreen ? 1 : 0);
},
onUnityLoaded() {
if (isLoaded) {
console.log(`${LOGTAG} Unity loaded`);
setUnityLoaded(true);
eventQ.processAllEvents();
if (!Configure.useUnityLogger) {
unityCommand.startLogger({ start: false });
}
}
},
onLoadingProgress() {
EventHandler.fire("$UnityPlayer::Loading$", isLoaded ? 100 : Math.round(loadingProgression * 100));
}
};
useEventHandlers(
{
[EventID.CLOSE_UNITY_PLAYER]() {
unityLoaded &&
timer(0).subscribe(async () => {
if (state) {
await unload();
setUnityLoaded(false);
console.debug(LOGTAG, `Unity real unload() success`);
setIsActiveUnity(false);
}
});
},
import { UnityConfig } from “react-unity-webgl/distribution/types/unity-config”;
const effector = {
onMount() {
EventHandler.emit(“UnityDomMount”);
if (unityCanvas.current) {
unityCanvas.current.onmousedown = async (ev: MouseEvent) => await fn.request_pointer_lock(ev);
unityCanvas.current.onmouseup = (ev: MouseEvent) => fn.release_pointer_lock(ev);
}Preformatted text
},
onUnmount() {
return () => {
/**
* @anon34138753Unloading the Unity Application | React Unity WebGL
* ! NOTICE
* Normally it was possible to unmount the Unity Application and
* unmount the containing component right after the unload function was invoked,
* but due to a bug in newer Unity versions this is no longer possible when using a build made with
* Unity 2021.2 or later.
* It is still possible to unload the Unity Application, but the canvas has to be kept mounted until
* the promise is resolved.
* As of writing this, the issue has not been fixed, but it is possible to unmount the Unity Application
* manually by halting the navigation to the next page.
*
unityLoaded &&
unload().then(() => {
setUnityLoaded(false);
});
*/
EventHandler.emit(“UnityDomUnmount”);
};
},
onFullScreen() {
console.debug(${LOGTAG} Fullscreen, isFullScreen);
unityCommand.screenModeChanged(isFullScreen ? 1 : 0);
},
onUnityLoaded() {
if (isLoaded) {
console.log(${LOGTAG} Unity loaded);
setUnityLoaded(true);
Hi, sorry for the delay. First off, can you confirm that the unloading works OK without the presence of React Unity WebGL. To test this, take the Unload button implementation from the HTML file of a the Development build made with the default template and put it into a Release build of you project. If everything is unloaded correctly, then it suggests there might be a bug/shortcoming in the third-party library, in which case I think you should be maybe a report a bug to the developer of that library. If everything is not unloaded correctly in this case, could you please file a bug report to us and we’re happy to take a look.
However, I’ve used this library myself also a year or two ago, and, iirc, I think I implemented unloading in my test app back then, so might be able to take another look at some point, but cannot make any promises on the time frame.