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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Changing javascript bindings - possible?

Discussion in 'Web' started by Marks4, Oct 5, 2020.

  1. Marks4

    Marks4

    Joined:
    Feb 25, 2018
    Posts:
    502
    So a quick example: When you call webcamdevice.name in Unity, the javascript function
    Code (javascript):
    1. _JS_WebCamVideo_GetDeviceName
    is called in webgl.framework.js, in the webgl build folder.

    Is there a way to somehow change the function that is called? I am well aware that I can simply overwrite the function _JS_WebCamVideo_GetDeviceName. But what if I don't want that? What if I want to bind the C# property to my own function? Because maybe there isn't a javascript binding for a certain property and I want to create one?

    I am aware that this is all possible with my own methods. But I'm talking about the built-in Unity methods/properties. Not something that I coded.

    How was the _JS_WebCamVideo_GetDeviceName added to the webgl.framework.js file? How does it connect to the webcamdevice.name property? The way we as users do it with our own methods/properties, is using .jslib files. But it seems Unity is doing something different here, because I can't find a .jslib that is doing this.

    @alexsuvorov @jukka_j @Marco-Trivellato @Schubkraft @MartinTilo @brendanduncan_u3d
     
    Last edited: Oct 5, 2020
  2. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    951
    The way that Unity does binding of C# property names to getter and setter functions in JavaScript is by supplying intermediate C++ code to do the wrapping, and the identical .jslib mechanism that user code is using. There is no direct "language" mechanism that would route C# properties to JS function.

    Just for build system reasons, the Unity-provided .jslib files are not named with a .jslib suffix, but have a regular .js suffix. So the file WebCam.js with that function is like a "WebCam.jslib" in user project.

    Basically the machinery that is in play here is twofold:
    - "Standard" C#-to-C/C++ "P/Invoke" marshalling is used to call back and forth between C# and C/C++.
    - The .jslib files are an Emscripten compiler provided mechanism to enable implementing JavaScript functions that take place in C/C++ linkage. In Unity C# context, this means C# can "P/Invoke out" to a JS function in a .jslib file.

    https://answers.unity.com/questions/1742028/use-c-script-to-call-javascript-functionmethod.html contains a good thread with example code.
     
  3. Marks4

    Marks4

    Joined:
    Feb 25, 2018
    Posts:
    502
    Hi @jukka_j , thanks for the reply. Yes, I know how to do it with my own methods and properties. But what about native Unity methods and properties? Can I do it with those, somehow? So I call webcamdevice.name on C#. And I want to trigger my own javascript function when I do that, instead of _JS_WebCamVideo_GetDeviceName. Let's call it _JS_WebCamVideo_GetDeviceName_Custom.

    To reiterate, I don't want the _JS_WebCamVideo_GetDeviceName javascript method to be called. I want my own js method to be called.

    Is it possible, for me, as a user to do that?
     
  4. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    951
    Unfortunately it is not possible to override built-in Unity C# API provided properties in user code that I know of.

    If you have custom C# code with properties, you can implement getter and setter functions for them that P/Invoke over out to JavaScript. That can make it look transparent for the user like they would be accessing only a C# surface.
     
    Marks4 likes this.
  5. Marks4

    Marks4

    Joined:
    Feb 25, 2018
    Posts:
    502
    I see, oh well. That's unfortunate. Thanks jukka!