nextjs-test/app/unity/page.tsx

78 lines
2.4 KiB
TypeScript

"use client";
import Image from "next/image";
import { useSearchParams } from "next/navigation";
import { Unity, UnityConfig, useUnityContext } from "react-unity-webgl";
const unityConfigBuilder: (path: string) => UnityConfig = (path) => ({
loaderUrl: `${path}.loader.js`,
dataUrl: `${path}.data`,
frameworkUrl: `${path}.framework.js`,
codeUrl: `${path}.wasm`,
streamingAssetsUrl: "StreamingAssets",
companyName: "DTOL",
productName: "3d-test",
productVersion: "0.1",
});
const option = [
{ value: "/unity/3d-test/Build/3d-test", name: "3d-test" },
{ value: "/unity/test-project/build", name: "test-project" },
];
export default function Home() {
const params = useSearchParams();
const p = params.get("p");
console.log(p);
if (!p || isNaN(Number(p))) location.href = "/unity?p=0";
const {
unityProvider,
isLoaded,
loadingProgression,
initialisationError,
UNSAFE__unityInstance,
} = useUnityContext(unityConfigBuilder(option[Number(p)].value));
const loadingPercentage = Math.round(loadingProgression * 100);
return (
<>
<div className="mb-5 mt-5">
Unity , WebGL로
react-unity-webgl .
</div>
<div className="relative h-[600px] w-[800px]">
{isLoaded === false && (
<div className="absolute left-0 top-0 flex h-full w-full items-center justify-center bg-gray-500">
<p>Loading... ({loadingPercentage}%)</p>
</div>
)}
{initialisationError && JSON.stringify(initialisationError, null, 2)}
<Unity className="h-full w-full" unityProvider={unityProvider} />
<div className="flex">
<select
value={Number(p)}
onChange={(e) => (location.href = `/unity?p=${e.target.value}`)}
className="mt-5"
>
{option.map((o, i) => (
<option key={`${o}-${i}`} value={i}>
{o.name}
</option>
))}
</select>
<Image
src={"/unity/3d-test/TemplateData/fullscreen-button.png"}
alt="전체 화면 버튼"
title="전체 화면으로 보기"
width={38}
height={38}
className="ml-auto mt-2 cursor-pointer"
onClick={() => UNSAFE__unityInstance?.SetFullscreen(1)}
/>
</div>
</div>
</>
);
}