【问题标题】:React unity WebGL not working on React JS projectReact unity WebGL 不适用于 React JS 项目
【发布时间】:2022-01-05 18:50:48
【问题描述】:

嘿,我试图让我的 Unity WebGL 导出在我的 React JS 项目上工作,但由于某种原因无法正常工作,我正在使用以下 React Unity WebGL 版本(react-unity-webgl@7.x # For Unity 2018 和2019 (Active LTS)) 并且我已经按照我正在使用的版本的说明进行操作,有人遇到了与我相同的问题并在GitHub 中提出了一个问题,我按照他们所说的基本上是把公用文件夹中的统一文件夹,但仍然无法正常工作。有人知道如何解决这个问题吗?

这就是我的文件夹结构:

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

代码 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"

【问题讨论】:

  • 您找到解决此问题的方法了吗?考虑self-answering 并在下面写下答案。

标签: reactjs react-native unity3d unity-webgl


【解决方案1】:

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

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

请记住,“UnityContent”已从 7.x 版重新改写为“UnityContext”到 8.x 版。

【讨论】:

    【解决方案2】:

    您必须将您的 unitybuid 文件夹作为公共文件的子文件夹移动,例如 /public/Build/yourfiles.json

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

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    • 它不必是“/public/Build/yourfiles.json”开发人员告诉我,只要路线/路径正确,它应该可以工作并且确实可以。在我的代码中,我有 SRC 中的页面和公共中的构建,但这就是我必须编写它的方式:“/Megaman/Build/Megaman.json”,因为我在公共内部创建了另一个文件夹(这样我可以将我要添加的多个游戏分开)
    • 啊,是的,当然...如果您有更多文件夹,则必须将它们添加到路径中,...但我的意思是项目文件夹必须位于公用文件夹中。很高兴它成功了
    猜你喜欢
    • 1970-01-01
    • 2022-07-21
    • 2018-07-15
    • 2016-10-26
    • 2021-05-23
    • 1970-01-01
    • 2021-06-23
    • 1970-01-01
    • 2015-07-01
    相关资源
    最近更新 更多