Skip to content

Commit 7141033

Browse files
authored
Create README.md
1 parent ec31d0f commit 7141033

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

README.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Пометка на память
2+
3+
## Создание WEBGL игры с интеграцией React и Unity
4+
5+
### Шаги по установке и созданию проекта
6+
7+
1. Создаем новый проект React:
8+
```bash
9+
npx create-react-app react-project
10+
```
11+
12+
2. Переходим в папку проекта:
13+
```bash
14+
cd react-project
15+
```
16+
17+
3. Устанавливаем библиотеку для интеграции Unity в React:
18+
```bash
19+
npm install react-unity-webgl
20+
```
21+
22+
4. Собираем билд Unity и переносим его в папку `public/UnityBuild`. В моем случае проект называется `Clicker`.
23+
24+
5. Создаем файл `App.js` в папке `src` и файл `UnityApp.js` в папке `src`.
25+
26+
### Пример файла `App.js`
27+
28+
```javascript
29+
import React from 'react';
30+
import UnityApp from './UnityApp';
31+
32+
function App() {
33+
return (
34+
<div className="App">
35+
<UnityApp />
36+
</div>
37+
);
38+
}
39+
40+
export default App;
41+
```
42+
43+
### Пример файла `UnityApp.js`
44+
45+
```javascript
46+
import React, { useEffect } from 'react';
47+
import { Unity, useUnityContext } from "react-unity-webgl";
48+
49+
function UnityApp() {
50+
const { unityProvider, sendMessage } = useUnityContext({
51+
loaderUrl: "UnityBuild/Build/Clicker.loader.js",
52+
dataUrl: "UnityBuild/Build/Clicker.data.unityweb",
53+
frameworkUrl: "UnityBuild/Build/Clicker.framework.js.unityweb",
54+
codeUrl: "UnityBuild/Build/Clicker.wasm.unityweb",
55+
});
56+
57+
useEffect(() => {
58+
const user = {
59+
user_id: "666",
60+
user_name: "Rimuru Dev"
61+
};
62+
63+
console.log("Sending user data:", JSON.stringify(user));
64+
65+
sendMessage("UserDataHandler", "ReceiveUserData", JSON.stringify(user));
66+
}, [sendMessage]);
67+
68+
useEffect(() => {
69+
window.UnityToReact = (message) => {
70+
console.log("Received from Unity: ", message);
71+
};
72+
73+
return () => {
74+
delete window.UnityToReact;
75+
};
76+
}, []);
77+
78+
return <Unity unityProvider={unityProvider} style={{ width: "800px", height: "600px" }} />;
79+
}
80+
81+
export default UnityApp;
82+
```
83+
84+
6. Запускаем проект:
85+
```bash
86+
npm start
87+
```
88+
89+
7. Открываем в браузере:
90+
```
91+
http://localhost:3000/
92+
```

0 commit comments

Comments
 (0)