refactor unity.tsx
This commit is contained in:
parent
de6bdf6aa0
commit
76b1d7b5ad
|
|
@ -4,7 +4,7 @@ import { useSearchParams } from "next/navigation";
|
||||||
import { useCallback, useEffect, useState } from "react";
|
import { useCallback, useEffect, useState } from "react";
|
||||||
import { Unity, UnityConfig, useUnityContext } from "react-unity-webgl";
|
import { Unity, UnityConfig, useUnityContext } from "react-unity-webgl";
|
||||||
import { ObjectType } from "../types";
|
import { ObjectType } from "../types";
|
||||||
const unityConfigBuilder: (path: string) => UnityConfig = (path) => ({
|
const unityConfigBuilder = (path: string): UnityConfig => ({
|
||||||
loaderUrl: `${path}.loader.js`,
|
loaderUrl: `${path}.loader.js`,
|
||||||
dataUrl: `${path}.data`,
|
dataUrl: `${path}.data`,
|
||||||
frameworkUrl: `${path}.framework.js`,
|
frameworkUrl: `${path}.framework.js`,
|
||||||
|
|
@ -31,6 +31,7 @@ export default function UnityComponent({
|
||||||
const params = useSearchParams();
|
const params = useSearchParams();
|
||||||
const p = params.get("p");
|
const p = params.get("p");
|
||||||
const [selectedIndex, setSelectedIndex] = useState({ ohg: 0, frame: 0 });
|
const [selectedIndex, setSelectedIndex] = useState({ ohg: 0, frame: 0 });
|
||||||
|
type SelectedIndexType = typeof selectedIndex;
|
||||||
const [match, setMatch] = useState(100);
|
const [match, setMatch] = useState(100);
|
||||||
if (!p || isNaN(Number(p))) location.href = "/unity?p=0";
|
if (!p || isNaN(Number(p))) location.href = "/unity?p=0";
|
||||||
|
|
||||||
|
|
@ -39,34 +40,11 @@ export default function UnityComponent({
|
||||||
|
|
||||||
const loadingPercentage = Math.round(loadingProgression * 100);
|
const loadingPercentage = Math.round(loadingProgression * 100);
|
||||||
|
|
||||||
const handleSelectIndex = useCallback(
|
// 매치율 조회 및 업데이트
|
||||||
// objectName: ohg | frame, newValue: "0", "1", ...
|
const handleMatchUpdate = useCallback(
|
||||||
(objectName: keyof typeof selectedIndex, newValue: string) => {
|
async (newSelectedIndex: SelectedIndexType) => {
|
||||||
const newIndex = Number(newValue);
|
|
||||||
// 화면상의 select 컴포넌트 업데이트
|
|
||||||
setSelectedIndex((prev) => {
|
|
||||||
if (objectName === "ohg") {
|
|
||||||
return {
|
|
||||||
...prev,
|
|
||||||
ohg: newIndex,
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
return {
|
|
||||||
...prev,
|
|
||||||
frame: newIndex,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
});
|
|
||||||
// 유니티 스크립트 호출 - objectName 및 함수 이름은 유니티의 것과 동일해야 함
|
|
||||||
UNSAFE__unityInstance?.SendMessage(objectName, "HandleSelect", newIndex);
|
|
||||||
},
|
|
||||||
[UNSAFE__unityInstance],
|
|
||||||
);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
(async () => {
|
|
||||||
const res = await fetch(
|
const res = await fetch(
|
||||||
`/fit?o=${selectedIndex.ohg}&f=${selectedIndex.frame}`,
|
`/fit?o=${newSelectedIndex.ohg}&f=${newSelectedIndex.frame}`,
|
||||||
{
|
{
|
||||||
method: "GET",
|
method: "GET",
|
||||||
},
|
},
|
||||||
|
|
@ -74,8 +52,46 @@ export default function UnityComponent({
|
||||||
console.log(res);
|
console.log(res);
|
||||||
const json = (await res.json()) as { match: number };
|
const json = (await res.json()) as { match: number };
|
||||||
if (isNaN(Number(json.match)) === false) setMatch(json.match);
|
if (isNaN(Number(json.match)) === false) setMatch(json.match);
|
||||||
})();
|
return json.match;
|
||||||
}, [selectedIndex.frame, selectedIndex.ohg]);
|
},
|
||||||
|
[],
|
||||||
|
);
|
||||||
|
|
||||||
|
// 최초 1회 조회
|
||||||
|
useEffect(() => {
|
||||||
|
handleMatchUpdate({ ohg: 0, frame: 0 });
|
||||||
|
}, [handleMatchUpdate]);
|
||||||
|
|
||||||
|
// ohg나 frame이 바뀔 때마다 호출
|
||||||
|
const handleSelectIndex = useCallback(
|
||||||
|
// objectName: ohg | frame, newValue: "0", "1", ...
|
||||||
|
(
|
||||||
|
currentSelectedIndex: SelectedIndexType,
|
||||||
|
objectName: keyof SelectedIndexType,
|
||||||
|
newValue: string,
|
||||||
|
) => {
|
||||||
|
// 화면상의 select 컴포넌트 업데이트
|
||||||
|
const newIndex = Number(newValue);
|
||||||
|
const newSelectedIndex = {
|
||||||
|
...currentSelectedIndex,
|
||||||
|
[objectName]: newIndex,
|
||||||
|
};
|
||||||
|
setSelectedIndex(newSelectedIndex);
|
||||||
|
|
||||||
|
// 유니티 스크립트 호출 - objectName 및 함수 이름은 유니티의 것과 동일해야 함
|
||||||
|
UNSAFE__unityInstance?.SendMessage(objectName, "HandleSelect", newIndex);
|
||||||
|
|
||||||
|
handleMatchUpdate(newSelectedIndex)
|
||||||
|
// 매치율 이용하여 컴포넌트 업데이트
|
||||||
|
.then((match) => {
|
||||||
|
//
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
// 에러처리
|
||||||
|
});
|
||||||
|
},
|
||||||
|
[UNSAFE__unityInstance, handleMatchUpdate],
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
|
@ -109,7 +125,9 @@ export default function UnityComponent({
|
||||||
<>
|
<>
|
||||||
<select
|
<select
|
||||||
value={selectedIndex.ohg}
|
value={selectedIndex.ohg}
|
||||||
onChange={(e) => handleSelectIndex("ohg", e.target.value)}
|
onChange={(e) =>
|
||||||
|
handleSelectIndex(selectedIndex, "ohg", e.target.value)
|
||||||
|
}
|
||||||
className="ml-5 mt-5"
|
className="ml-5 mt-5"
|
||||||
>
|
>
|
||||||
{ohg?.map((o, i) => (
|
{ohg?.map((o, i) => (
|
||||||
|
|
@ -120,7 +138,9 @@ export default function UnityComponent({
|
||||||
</select>
|
</select>
|
||||||
<select
|
<select
|
||||||
value={selectedIndex.frame}
|
value={selectedIndex.frame}
|
||||||
onChange={(e) => handleSelectIndex("frame", e.target.value)}
|
onChange={(e) =>
|
||||||
|
handleSelectIndex(selectedIndex, "frame", e.target.value)
|
||||||
|
}
|
||||||
className="ml-5 mt-5"
|
className="ml-5 mt-5"
|
||||||
>
|
>
|
||||||
{frame.map((o, i) => (
|
{frame.map((o, i) => (
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue