Code Monkey home page Code Monkey logo

unity-react-webgl-clicker's Introduction

Пометка на память

Создание WEBGL игры с интеграцией React и Unity

Шаги по установке и созданию проекта

  1. Создаем новый проект React:

    npx create-react-app react-project
  2. Переходим в папку проекта:

    cd react-project
  3. Устанавливаем библиотеку для интеграции Unity в React:

    npm install react-unity-webgl
  4. Собираем билд Unity и переносим его в папку public/UnityBuild. В моем случае проект называется Clicker.

  5. Создаем файл App.js в папке src и файл UnityApp.js в папке src.

Пример файла App.js

import React from 'react';
import UnityApp from './UnityApp';

function App() {
    return (
        <div className="App">
            <UnityApp />
        </div>
    );
}

export default App;

Пример файла UnityApp.js

import React, { useEffect } from 'react';
import { Unity, useUnityContext } from "react-unity-webgl";

function UnityApp() {
    const { unityProvider, sendMessage } = useUnityContext({
        loaderUrl: "UnityBuild/Build/Clicker.loader.js",
        dataUrl: "UnityBuild/Build/Clicker.data.unityweb",
        frameworkUrl: "UnityBuild/Build/Clicker.framework.js.unityweb",
        codeUrl: "UnityBuild/Build/Clicker.wasm.unityweb",
    });

    useEffect(() => {
        const user = {
            user_id: "666",
            user_name: "Rimuru Dev"
        };

        console.log("Sending user data:", JSON.stringify(user));

        sendMessage("UserDataHandler", "ReceiveUserData", JSON.stringify(user));
    }, [sendMessage]);

    useEffect(() => {
        window.UnityToReact = (message) => {
            console.log("Received from Unity: ", message);
        };

        return () => {
            delete window.UnityToReact;
        };
    }, []);

    return <Unity unityProvider={unityProvider} style={{ width: "800px", height: "600px" }} />;
}

export default UnityApp;
  1. Запускаем проект:

    npm start
  2. Открываем в браузере:

    http://localhost:3000/
    
    

Тестирование проекта

Если вы хотите протестировать этот проект, выполните следующие шаги:

  1. Установите Node.js: Если у вас нет Node.js, скачайте и установите его с официального сайта.

  2. Склонируйте репозиторий:

    git clone https://github.com/RimuruDev/Unity-React-WEBGL-Clicker.git
  3. Перейдите в папку с проектом React:

    cd Unity-React-WEBGL-Clicker/react-project
  4. Установите зависимости:

    npm install
  5. Запустите проект:

    npm start
  6. Откройте проект в браузере: Откройте браузер и перейдите по адресу:

    http://localhost:3000/
    

Теперь вы можете увидеть работу интеграции Unity и React в действии. Если у вас возникнут вопросы или проблемы, пожалуйста, создайте issue в репозитории.

unity-react-webgl-clicker's People

Contributors

rimurudev avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.