Skip to content

Commit 05d900f

Browse files
committed
Create UnityApp.js
1 parent 57fb982 commit 05d900f

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

react-project/src/UnityApp.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import React, { useEffect } from 'react';
2+
import { Unity, useUnityContext } from "react-unity-webgl";
3+
4+
function UnityApp() {
5+
const { unityProvider, sendMessage, addEventListener, removeEventListener } = useUnityContext({
6+
loaderUrl: "UnityBuild/Build/UnityBuild.loader.js",
7+
dataUrl: "UnityBuild/Build/UnityBuild.data.unityweb",
8+
frameworkUrl: "UnityBuild/Build/UnityBuild.framework.js.unityweb",
9+
codeUrl: "UnityBuild/Build/UnityBuild.wasm.unityweb",
10+
});
11+
12+
// Пример передачи данных в Unity
13+
useEffect(() => {
14+
const user = {
15+
user_id: "666",
16+
user_name: "Rimuru Dev"
17+
};
18+
19+
// Путь до папки со скриптом в юнити - Assets/Codebase/UserDataHandler.cs
20+
sendMessage("UserDataHandler", "ReceiveUserData", JSON.stringify(user));
21+
}, [sendMessage]);
22+
23+
// Добавление обработчика события для получения данных из Unity
24+
useEffect(() => {
25+
window.UnityToReact = (message) => {
26+
console.log("Received from Unity: ", message);
27+
};
28+
29+
return () => {
30+
delete window.UnityToReact;
31+
};
32+
}, []);
33+
34+
return <Unity unityProvider={unityProvider} style={{ width: "800px", height: "600px" }} />;
35+
}
36+
37+
export default UnityApp;

0 commit comments

Comments
 (0)