Search Unity

Node+npm+emscripten generator = webGL Unity Magic

Discussion in 'Web' started by apiotuch, Mar 6, 2018.

?

Which do you prefer?

  1. SendMessage

    16.7%
  2. jslib

    83.3%
  1. apiotuch

    apiotuch

    Joined:
    Oct 8, 2009
    Posts:
    42
    So I installed node.js + npm then downloaded the package "emscripten generator" to convert js into jslib files to bring into unity so that I can interact with webpage content.... but I am wondering if anyone knows of a better way? I have only partially tested it, and it seems to be working with accessing page content (I think my js code needs some refactoring however).

    Am I missing something? I didn't find anything out of the box with Unity to do this conversion, so it is a bit of a roundabout way to do things, I would have expected Unity to make this a bit simpler, or at least provide a "how to" guide for us developers considering Unity is abandoning SendMessage.

    I may end up writing up a tutorial if anyone asks, it just seems like Unity dropped the ball on this one. Correct me if I'm wrong, but I didn't see anything in the tutorials to easily convert this stuff. And no, I don't mean this,

    https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html

    ...it was a good start, but this tutorial didn't even mention emscripten, or how to include global variables and nested function calls. The generator automatically sets up all the syntax for you (which is not easy to grasp).
     
    Last edited: Mar 6, 2018
    anastrophe likes this.
  2. anastrophe

    anastrophe

    Joined:
    Feb 8, 2017
    Posts:
    9
    I would love to see a tutorial on how you go that setup and working.
     
  3. JJJohan

    JJJohan

    Joined:
    Mar 18, 2016
    Posts:
    214
    You can actually interact with content on the page you host without any special treatment.

    Say you have a jslib function like this:

    GetTextFromPage : function()
    {
    return getTextFromPage();
    }

    And a JS snippet somewhere on your actual web page:

    function getTextFromPage()
    {
    return "some text";
    }

    It'll compile fine even though there's no reference to this during compile time. It's evaluated at runtime.

    In addition you can also access all regular JavaScript content directly from the jslib:

    GetPageUrl : function()
    {
    return window.location.href;
    }

    So for me the SendMessage deprecation wasn't really as much of an issue since I have a dedicated page with no other webGL content and full control of the web site so I can simply guarantee this connection happens.
     
    JDenBe and apiotuch like this.
  4. OrderOfOut

    OrderOfOut

    Joined:
    Sep 21, 2018
    Posts:
    37
    You, sir, should be writing the Unity documentation. Their page explaining how to implement jslib utterly fails to communicate this scope relationship, and implies that ALL javascript interacting with your game needs to be inside the jslib plugin. My coworker and I, along with many other developers, have been utterly confused and found no help from any of the Unity technicians, until this explanation. Thanks!
     
    JDenBe and apiotuch like this.