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
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): public function actionGetJson() { //my build .json is placed in /web/uploads/constructor-data/Build $content = file_get_contents('constructor-data/Build/webgl.func.test.json'); $data = json_decode($content); $data->dataUrl = Url::base('https').'/constructor-data/Build/'.$data->dataUrl; $data->wasmCodeUrl = Url::base('https').'/constructor-data/Build/'.$data->wasmCodeUrl; $data->wasmFrameworkUrl = Url::base('https').'/constructor-data/Build/'.$data->wasmFrameworkUrl; $data->wasmSymbolsUrl = Url::base('https').'/constructor-data/Build/'.$data->wasmSymbolsUrl; $content = json_encode($data); return $content; } And then in your view (views/constructor/index.php) index.php change to this: unityInstance = UnityLoader.instantiate("unityContainer", "/constructor/get-json");
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): <!DOCTYPE html> <html> <head> <base target="_top"> </head> <body> <link rel="stylesheet" href="https://example.com/style.css"> <script src="https://example.com/UnityProgress.js"></script> <script src="https://example.com/UnityLoader.js"></script> <script> var gameInstance = UnityLoader.instantiate("gameContainer", "linkhere.json", {onProgress: UnityProgress,Module:{onRuntimeInitialized: function() {UnityProgress(gameInstance, "complete")}}}); </script> <div class="webgl-content"> <div id="gameContainer" style="width: 1000px; height: 660px; margin: auto"></div> </div> </html> 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
What errors are you getting when you use the whole link ( https://example.com/page1/game.json ) ? I understand it wont load, but do you see any errors in the console?
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.
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)
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.