5

Hey there trying to make my Unity WebGL export work on my React JS project but for some reason is not working I'm using the following 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 and I followed what they said which is basically to put the unity folder in the public folder but still is not working. Anyone has any idea of how to fix this ?

This is how I have my folder structure:

enter image description here

And this is the 2 codes I have try (which they are basically the same):

Code 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

Code 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>
    );
}

And this is how it renders

enter image description here

Take in mind I have try the path in 2 ways either "Megaman/Build/Megaman.json" or just "/Build/Megaman.json"

ReactPotato
  • 1,262
  • 3
  • 26
  • 46
  • Did you find a solution to this problem? Consider [self-answering](https://stackoverflow.com/help/self-answer) and writing an answer below. – Ruzihm Jan 24 '22 at 19:56

2 Answers2

3

Answered here anyone looking: https://github.com/jeffreylanters/react-unity-webgl/discussions/299

Code was fine just needed to add "/Megaman/Build/Megaman.json" and "/Megaman/Build/UnityLoader.js".

And keep in mind the "UnityContent" has been reworked into "UnityContext" from version 7.x to 8.x.

0

you have to move your unitybuid folder as child of Public file, as this /public/Build/yourfiles.json

i am havinbg problems after that, it runs well on local host, but when building on a hosting sites, it doesnt loading for me.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 09 '22 at 06:47
  • It does not have to be "/public/Build/yourfiles.json" developer told me as long as the route/path is correctly it should work and it did. In my code I have the Page in the SRC and the build in the public however this is how I have to write it up: "/Megaman/Build/Megaman.json" because I made another folder inside the public (that way I can separate the multiple games I wanna add) – ReactPotato Jan 18 '22 at 20:05
  • Ah yeah, of coruse... if you have more folders you have to add them in the path,... but what i meant is that the project folder have to be inside the public folder. Glad it worked – Lucilo del Castillo Jan 19 '22 at 14:29