Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Cannot call JavaScript on .html with Application.ExternalCall (Case 839182)

Discussion in 'WebGL' started by Shushustorm, Oct 7, 2016.

  1. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    Hello everyone!
    This worked fine with Unity Web Player, but ever since I upgraded Unity to a version that uses WebGL instead, I am having problems calling JavaScript that is located on the .html from within the game.

    Does anybody face the same issue?

    Since it's not easy to reproduce this because of the FPSController issue
    ( https://forum.unity3d.com/threads/fpscontroller-webgl-issue.435269/ ),
    I attached a screenshot showing the error message that appears when I try to call JavaScript:

    screenshot.jpg

    If you want to try it in the build, you can take a look at:
    http://shushustorm.com/ShushustormHeadquarters.html

    Here is the code that I am using:

    In the Unity player:
    Code (CSharp):
    1. Application.ExternalCall("NewURL", websiteURL);
    On the .html:
    Code (JavaScript):
    1. function NewURL(chosenURL){
    2.     document.getElementById("newURLalign_ID").textContent = chosenURL;
    3.     document.getElementById("newURL_ID").href = chosenURL;
    4. }
    Best wishes,
    Shu
     
  2. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    I just created an empty project to do in 5.4.1p3 and it seems to work.

    could you try with an empty project and upload the build if it does not work ?
     
  3. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    @Marco-Trivellato

    Thanks for the response!
    I just tried a very simple project from scratch, but I still can't call javascript.
    Am I doing something wrong?
    I documented the whole thing.

    You can take a look at the build and download the project that I made here:
    www.shushustorm.com/_Resources/_/Unity/WebGL_TestProject/WebGLTestProject.html

    This is the method I use on the cube:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CubeScript : MonoBehaviour {
    5. void OnMouseDown () {Application.ExternalCall("WebGL_Test");}
    6. }
    and this is the content of the html:

    Code (JavaScript):
    1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    2. <html xmlns="http://www.w3.org/1999/xhtml">
    3. <head>
    4.  
    5. <script type="text/javascript" language="javascript">
    6.     function WebGL_Test () {window.alert("WebGL_Test called");}
    7. </script>
    8.  
    9. </head>
    10. <body>
    11. <br><br>
    12.  
    13. <p align="center">
    14.     <iframe src ="WebGL_TestProject_Build/index.html" style="border:0px #000000 none;"
    15.     name="Shushustorm Headquarters" scrolling="no" frameborder="100" marginheight="100px"
    16.     marginwidth="600px" height="600px" width="960px"></iframe>
    17. </p>
    18.  
    19. <a href="WebGLTestProject.zip">Download project</a>
    20.  
    21. <br><br><br><br><br><br><br><br>
    22. </body>
    23. </html>
    It's all very basic, yet it's not working.
    I feel like there is a typo somewhere if it's working for you.
    But I made sure to copy / paste the stuff to avoid typos.
     
  4. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    This can't work. You either need to define your JS function in WebGL_TestProject_Build/index.html. Alternatively you could try to call window.parent.WebGL_Test instead.
     
    Shushustorm likes this.
  5. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    @Marco-Trivellato

    Thank you for your answer!
    The first of your suggestions worked for the test project.
    However, when I wanted to integrate that into Shushustorm Headquarters, it wouldn't work like that, because then the NewURL function couldn't access the other elements of the website using getElementById. So I tried to integrate the content of the build's index.html into my own html (ShushustormHeadquarters.html).
    Now it works to some extent. At least ExternalCall() works.

    There are still several issues, though.
    I don't really think it's useful to open up a new topic for each one of these, so I post them here even though the original issue is solved:

    Disclaimer:
    As I had a crash myself with the WebGL player, clicking on any links in this post is at your own risk.
    I don't know if the potential to crash (see issue 5) is specific to my type of Macbook or if this is more wide-spread.

    1.
    FPSController's mouse movement is not working with WebGL:
    https://forum.unity3d.com/threads/fpscontroller-webgl-issue.435269/

    2.
    There is no waiting icon and loading information if I embed the index.html into another .html.
    I made sure to get the paths to the icons and scripts right, but it won't show up.
    http://shushustorm.com/ShushustormHeadquarters.html
    __
    EDIT:
    ALright, I found that the icon is being declared in TemplateData/UnityProgress.js. So I'd have to make deeper changes for this to work.

    __

    3.
    Fullscreen doesn't work even in the unmodified build's own index.html:
    www.shushustorm.com/_Resources/ShushustormHeadquarters/Shushustorm_Headquarters_1_01_WebGL/index.html

    4.
    With WebGL enabled, Safari will sometimes just close when trying to load
    http://shushustorm.com/ShushustormHeadquarters.html
    or
    www.shushustorm.com/_Resources/ShushustormHeadquarters/Shushustorm_Headquarters_1_01_WebGL/index.html

    5.
    At some point when using the WebGL player embedded on my website, my whole machine crashed (!).
    (OSX 11.6, rMBP 2015, AMD Radeon R9 M370X / Intel Iris Pro)
    Are crashes a known issue? Because I'd rather not use it on my website, then.
    After having to actually hard reset the device, I could see the following line having been spammed in the console multiple times per second:
    WindowServer[177]: GPU Driver appears to be hung (over 5 continuous seconds of unreadiness)

    6.
    Which leads me to another issue, but this last one can probably not be solved by you as I am quite sure this would require work on Apple's end, too: When starting the WebGL player, I see some black frames on the machine. This happens very quickly. The screen turns black and then back to the normal content. Something similar happens when turning off the automatic graphics switch in OSX's system preferences.

    Also, I have some further questions, which may be somewhat off-topic, but very important for me:
    Are you planning to stay with current conventions on browser integration? Or is this going to move on quickly? Because I would probably not bother with it any further if I actually have to update the whole html integration regularly just to maintain it.
     
    Last edited: Oct 12, 2016