- Home /
Can external pure javascript files be accessed from Unity?
I am looking at releasing a project to the web and I wanted to try to integrate Unity in to said project. For testing I tried setting up an event in unity that would being up an alert window in the browser, but even having a file with "alert" within the Unity project stops Unity in it's tracks.
Is there a way to access a js file that would have just an alert (as below), that I could then build upon for better integration?
Apologies for the basic question, all my searches seemed to only bring me to UnityScript results.
function show_alert()
{
alert("Test successful.");
}
Answer by steakpinball · Dec 11, 2015 at 02:41 PM
Yes they can, though not directly. Use Application.ExternalCall
to call js methods. Or use Application.ExternalEval
to run js code written in unity.
You can alternatively put browser js files in the unity project by putting them in Assets/Plugins/WebGL/
and giving them the extension`.jslib`. You can then use C#'s native interop to call those methods.
http://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html
For example, if you had a function called show_alert
in your browser js, call it
Application.ExternalCall("show_alert");
Can I do the opposite process using .jslib file? Can I trigger a javasript function from my C# unity code? I want a javascript function that uses DO$$anonymous$$ to be executed by my C# unity code