Search Unity

Need to replace Unitycontainer

Discussion in 'WebGL' started by l-e-v-a-n, Feb 1, 2020.

  1. l-e-v-a-n

    l-e-v-a-n

    Joined:
    Feb 1, 2020
    Posts:
    8
    Hello. Im newbye here.
    I have problem and I hope you can help me.

    <script>
    UnityLoader.instantiate("unityContainer", "MYPROBLEMHERE");
    </script>

    in this code I need to place whole link, I can not place only file name, because this file will be uploaded on another website and unitycontainer file is uploaded another website. How can I place file container link in this script?
    for example UnityLoader.instantiate("unityContainer", "http://website.com/filename.json");
    im trying this way and it doesnt work :( :(
     
  2. l-e-v-a-n

    l-e-v-a-n

    Joined:
    Feb 1, 2020
    Posts:
    8
    noone can help me? :(
     
  3. lightmapper

    lightmapper

    Joined:
    Jan 14, 2015
    Posts:
    16
    You might need to do a number of things, what issue are you currently running into?
     
  4. Getsumi3

    Getsumi3

    Joined:
    Aug 24, 2014
    Posts:
    2
    Hello.
    You'll need a bit of knowlegde about php and the framework you're working with.

    Im using Yii2 framework but the approach should be similar

    The main problem is that the path to .unityweb inside generated .json is hardcoded.

    Here is the idea how I did it. I created an action in my controller (ConstructorController) that will decode .json, change the path of all .unityweb fields to the path where the files will be on server and than encode .json back.

    Here is the code:

    Code (CSharp):
    1. public function actionGetJson()
    2.     {
    3.         //my build .json is placed in /web/uploads/constructor-data/Build
    4.         $content = file_get_contents('constructor-data/Build/webgl.func.test.json');
    5.         $data = json_decode($content);
    6.         $data->dataUrl = Url::base('https').'/constructor-data/Build/'.$data->dataUrl;
    7.         $data->wasmCodeUrl = Url::base('https').'/constructor-data/Build/'.$data->wasmCodeUrl;
    8.         $data->wasmFrameworkUrl = Url::base('https').'/constructor-data/Build/'.$data->wasmFrameworkUrl;
    9.         $data->wasmSymbolsUrl = Url::base('https').'/constructor-data/Build/'.$data->wasmSymbolsUrl;
    10.         $content = json_encode($data);
    11.         return $content;
    12.     }
    And then in your view (views/constructor/index.php) index.php change to this:

    unityInstance = UnityLoader.instantiate("unityContainer", "/constructor/get-json");
     
  5. l-e-v-a-n

    l-e-v-a-n

    Joined:
    Feb 1, 2020
    Posts:
    8
    My issue is that I can't point json file URL in html because i can't just type only filename (as everyone does) because json file is not same directory (and same website, where I'm trying to place game)
    I can't get what you have told :(

    I'll copy now my whole html
    Code (CSharp):
    1. <!DOCTYPE html>
    2. <html>
    3.   <head>
    4.     <base target="_top">
    5.   </head>
    6.   <body>
    7. <link rel="stylesheet" href="https://example.com/style.css">
    8. <script src="https://example.com/UnityProgress.js"></script>
    9.     <script src="https://example.com/UnityLoader.js"></script>
    10.     <script>
    11.       var gameInstance = UnityLoader.instantiate("gameContainer", "linkhere.json", {onProgress: UnityProgress,Module:{onRuntimeInitialized: function() {UnityProgress(gameInstance, "complete")}}});
    12.     </script>
    13.     <div class="webgl-content">
    14.         <div id="gameContainer" style="width: 1000px; height: 660px; margin: auto"></div>
    15.      </div>
    16. </html>
    17.  
    18.  
    Look here for "linkhere.json"
    this is my problem. all website is using just name here not link because this name exists in same directory. for example if there is written "game.json" and this code is placed in https://example.com/page1 then json file link will be "https://example.com/page1/game.json
    but they are using only "game.json" and if I try to write there whole link ( https://example.com/page1/game.json ) game would not be load. :(
    I can't do same as I do for style.css files or unityloader.js
     
  6. lightmapper

    lightmapper

    Joined:
    Jan 14, 2015
    Posts:
    16
  7. l-e-v-a-n

    l-e-v-a-n

    Joined:
    Feb 1, 2020
    Posts:
    8
    there is not any error message, just game doesn't load. white blank space
     
  8. Getsumi3

    Getsumi3

    Joined:
    Aug 24, 2014
    Posts:
    2
    Yes. It is because your game.json is holding references to your .unityweb data files and after you move your files to server the path need to be changed.

    "dataUrl": "game.data.unityweb",
    "wasmCodeUrl": "game.wasm.code.unityweb",
    "wasmFrameworkUrl": "game.wasm.framework.unityweb",
    "wasmSymbolsUrl": "game.wasm.symbols.unityweb",


    You need to change the path of this files to the one where the files are placed on server like this:

    https://example.com/web/game-data/Build/<file>.unityweb


    P.S: I did not tested it this way but I hope it will help, or at least give you the idea what to do.
     
  9. l-e-v-a-n

    l-e-v-a-n

    Joined:
    Feb 1, 2020
    Posts:
    8
    this files are inserted in .json file, not in html
    in html code i'm just tryin to place .json file link (which contains game data url you have written)
     
  10. lightmapper

    lightmapper

    Joined:
    Jan 14, 2015
    Posts:
    16
    Since your files are hosted on another server, a browser will block your requests to them. You need to add the appropriate headers on your files so they can be accessed from your webpage. Review the second post on this thread: https://forum.unity.com/threads/no-access-control-allow-origin-header.819588/

    And more resources on CORS here: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

    How you set the headers will depend on your hosting server. Fixing the CORS issue will allow the JSON file to load, but you may experience followup issues, so be sure to check the console to see what else you have to fix.
     
unityunity