Sunday 1 November 2020

Unity - communicating with clientside Javascript and ajax. How to pass data back to the webpage from unity?

What I am really asking is this; if there are dependencies which are impossible to compile into the unity build, is there a way of still calling them from within the unity and simply using the scripts loaded into the browser from the website and communicating with them?

Relevant documentation does not address this deeply: https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html

I am creating a website wrapper for a unity application. The buttons for the experience are located within the website, as the buttons affect the rest of the site, not just the unity application.

When certain content is loaded in the unity game play however, the app needs to be able to affect the website. Is there a way to pass the data back to the website in a creative way? Currently, I am including all my javascript code for the website in the unity compile, and it is erroring out on:

gameInstance = UnityLoader.instantiate("gameContainer", "/Build/DomumProto16_Web.json", {
    onProgress: UnityProgress
}); 

Sending data to the gameplay from the website:

gameInstance.SendMessage('Manager','Filter', JSON.stringify(filterActive));

Need to call this function from the unity gameplay. However, ajax.ajax_url is undefined due to it being localized using wp_localize_script() on the backend.

function showStudentWork(studentIndex){
        
    //make sure to remove all the 
    var x = document.getElementById("studentWork");

    var studentID = studentWork[studentIndex];
    console.log(studentID);
    
    $.ajax({
        url: ajax.ajax_url,
        type: "POST",
        data: {
            action: 'getStudentPost',
            currentStudent: studentID
        },
        success: function (data) {
            x.innerHTML = "";
            x.innerHTML = data;
            x.style.display = "grid";
        },
        error: function (error) {
            console.log(`Error ${error}`);
        }
    });
    
    return false;

}

What I am really asking is this; if there are dependencies which are impossible to compile into the unity build, is there a way of still calling them from within the unity and simply using the scripts loaded into the browser from the website and communicating with them?



from Unity - communicating with clientside Javascript and ajax. How to pass data back to the webpage from unity?

No comments:

Post a Comment