mirror of
https://github.com/filecoin-project/slate.git
synced 2024-11-23 22:12:19 +03:00
53 lines
1.3 KiB
JavaScript
53 lines
1.3 KiB
JavaScript
import * as React from "react";
|
|
|
|
import { css } from "@emotion/react";
|
|
|
|
const STYLES_CONTAINER = css`
|
|
height: 100%;
|
|
width: 100%;
|
|
`;
|
|
|
|
const _loadScript = (url) =>
|
|
new Promise((res, rej) => {
|
|
// NOTE (Amine): Create script
|
|
const script = document.createElement("script");
|
|
script.src = url;
|
|
document.body.appendChild(script);
|
|
script.onload = () => res(script);
|
|
});
|
|
|
|
const _cleanScripts = () => {
|
|
const scripts = document.getElementsByTagName("script");
|
|
const unityLoaderRegex = new RegExp(/UnityLoader.js/);
|
|
Array.from(scripts).forEach((script) => {
|
|
if (unityLoaderRegex.test(script.src)) {
|
|
script.remove();
|
|
}
|
|
});
|
|
};
|
|
|
|
const UnityFrame = ({ url, unityGameLoader, unityGameConfig }) => {
|
|
React.useEffect(() => {
|
|
let unityInstance;
|
|
_loadScript(`${url}/${unityGameLoader}`).then(() => {
|
|
if (window) {
|
|
unityInstance = window.UnityLoader.instantiate(
|
|
"unityContainer",
|
|
`${url}/${unityGameConfig}`
|
|
);
|
|
}
|
|
});
|
|
return () => {
|
|
// NOTE (Amine): clean up Unity events on unmout
|
|
if (unityInstance) {
|
|
unityInstance.Quit();
|
|
}
|
|
_cleanScripts();
|
|
};
|
|
}, []);
|
|
|
|
return <div id="unityContainer" css={STYLES_CONTAINER} />;
|
|
};
|
|
|
|
export default UnityFrame;
|