Skip to content

Commit

Permalink
Add code to support loading url fragments
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianGlawogger committed Nov 13, 2024
1 parent 09e2fc1 commit 13f56d6
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions unity/Assets/WebGLTemplates/MaroonWeb/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
row_progress.style.display = "none";
row_game.style.display = "flex";
enableFitGame();
parseUrlFragment();
}
}

Expand Down Expand Up @@ -76,6 +77,56 @@
fitGame();
window.addEventListener('resize', fitGame);
}

function parseUrlFragment()
{
// Access the URL fragment (after the #)
const fragment = window.location.hash.substr(1); // Remove the '#' symbol
const fragmentParams = new URLSearchParams(fragment);
const config = fragmentParams.get('config');

if (!config)
{
console.log("URL Fragment Config is empty.");
}
else
{
console.log("URL Fragment Config: ", config);
sendConfig(config);
}
}

function sendConfig(config) {
try {
// Send configuration to Maroon.
sendDataToUnity(config);
} catch (e) {
alert('Check the syntax of the input. ' + e);
}
}

async function sendDataToUnity(data){
console.log("Send Data to Unity");

if (!unityInstance)
{
console.log("UnityInstance is null, waiting...");
}
await waitForNonNullUnityInstance();

unityInstance.SendMessage("WebGL Receiver", "GetDataFromJavaScript", data);
}

async function waitForNonNullUnityInstance() {
return new Promise(resolve => {
const checkInterval = setInterval(() => {
if (unityInstance !== null) {
clearInterval(checkInterval);
resolve(unityInstance); // Resolves once unityInstance is no longer null
}
}, 100); // Check every 100ms
});
}

</script>

Expand Down

0 comments on commit 13f56d6

Please sign in to comment.