React unity WebGL 不适用于 React JS 项目

React unity WebGL not working on React JS project

嘿,试图让我的 Unity WebGL 导出在我的 React JS 项目上工作,但由于某种原因不起作用我正在使用以下 React Unity WebGL version (react-unity-webgl@7.x # For Unity 2018 and 2019 (Active LTS)) and I have followed the instructions accordingly to the version I'm using, someone ran into the same issue than me and asked a question in the GitHub 并且我按照他们所说的基本上是把public 文件夹中的 unity 文件夹,但仍然无法正常工作。有人知道如何解决这个问题吗?

我的文件夹结构是这样的:

这是我尝试过的 2 个代码(它们基本相同):

代码A:

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

let unityContent = new UnityContent(
  "Megaman/Build/Megaman.json",
  "Megaman/Build/UnityLoader.js"
);

function MegamanGame() {
    return (
        <div>
          <h1>Is not working</h1>
          <Unity unityContent={unityContent} />
        </div>
      
    )
}

export default MegamanGame

代码 B:

import Unity, { UnityContent } from "react-unity-webgl";
export default function Megaman() {
    let unityContent = new UnityContent(
        "Megaman/Build/Megaman.json",
        "Megaman/Build/UnityLoader.js"
    );
    return (
        <div><Unity unityContent={unityContent} /></div>
    );
}

这是它的呈现方式

请记住,我已经通过 "Megaman/Build/Megaman.json""/Build/Megaman.json"

两种方式尝试了路径

您必须将您的 unitybuid 文件夹作为 Public 文件的子文件夹移动,因为此 /public/Build/yourfiles.json

之后我遇到了问题,它在本地主机上运行良好,但是在托管站点上构建时,它没有为我加载。

任何人都在这里回答:https://github.com/jeffreylanters/react-unity-webgl/discussions/299

代码很好,只需要添加“/Megaman/Build/Megaman.json”和“/Megaman/Build/UnityLoader.js”.

请记住,“UnityContent”已从版本 7.x 改写为“UnityContext”至 8.x。