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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

How to return js object to unity

Discussion in 'WebGL' started by alitokur, Sep 17, 2020.

  1. alitokur

    alitokur

    Joined:
    Sep 17, 2020
    Posts:
    3
    I'm using the Google Draco decoder to decode mesh the following.

    Code (JavaScript):
    1. var dracoGeometry;
    2. dracoGeometry = new decoderModule.Mesh();
    3. decodingStatus = decoder.DecodeBufferToMesh(buffer, dracoGeometry);
    when I check the type of the draceGeometrytry:
    Code (JavaScript):
    1. console.log( typeof dracoGeometry);
    I get:
    "object" .

    Now my problem is "how can I return this object to unity". What return type supported in C# to accept js object?
     
  2. Marks4

    Marks4

    Joined:
    Feb 25, 2018
    Posts:
    492
  3. supron

    supron

    Joined:
    Aug 24, 2013
    Posts:
    67
    HI.
    You can check my library. It's made for Tiny/DOTS but it works with WebGL too. It helps with the transfer of complex objects between C# and JS. It does not support arrays yet, but this function will be implemented within a few days. All you have to know is the structure of your object. The library will do the rest for you.
     
  4. alitokur

    alitokur

    Joined:
    Sep 17, 2020
    Posts:
    3
    @Marks4 As far as I understand these are only solutions valid for string and other primitive data types. but I'm creating a mesh on the js side.
     
  5. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    944
    It is not possible to return high-level JavaScript objects back to WebAssembly, since WebAssembly understands only low-level assembly primitives (ints and floats).

    Instead, what people usually do is follow a handle-based approach, where one creates a JavaScript dictionary or an array that holds the high-level JS objects, and then puts the JS object into that dict/array, indexed by a number. Then that number handle is passed back to Unity/WebAssembly side. Then the Unity C# code can point to that object by keeping the handle around.

    When the WebAssembly side wants to perform some kind of action on the object, it will call out to a JavaScript function, and pass that handle there, so that the JavaScript function will know which high-level JS object to pick out from the dict/array.

    For a full example of this in action, see Emscripten repository at https://github.com/emscripten-core/emscripten/blob/master/src/library_webgl.js, which represents GL resources, such as textures and buffers in that fashion.