1

I'm using the Google Draco decoder to decode mesh the following.

var dracoGeometry;
dracoGeometry = new decoderModule.Mesh();
decodingStatus = decoder.DecodeBufferToMesh(buffer, dracoGeometry);

when I check the type of the draceGeometrytry: console.log( typeof dracoGeometry);

I get the

"object" type.

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

gman
  • 100,619
  • 31
  • 269
  • 393
badcode
  • 581
  • 1
  • 9
  • 28

2 Answers2

1

You can send strings or numbers, so what I would do is create a js object that has the data you need then call JSON.stringify on it to convert it to a string then send that over using unityInstance.SendMessage:

function sendToUnity(obj) {
    unityInstance.SendMessage('MyGameObject', 'MyMethod', JSON.stringify(obj));
}
// in a script attached to a gameobject named "MyGameObject"
void MyMethod(string s) 
{
    Debug.Log(s);
    // decode from json, do stuff with result etc
}

As for what you can do with that data while it is in JSON string form, you can use a variety of solutions to decode the JSON string once you have it in Unity. See Serialize and Deserialize Json and Json Array in Unity for more information.

So if your js looks like this:

function sendToUnity(obj) {
    unityInstance.SendMessage('MyGameObject', 'MyMethod', JSON.stringify(obj));
}

sendToUnity({
    playerId: "8484239823",
    playerLoc: "Powai",
    playerNick:"Random Nick"
});

You could do something like this in Unity:

[Serializable]
public class Player
{
    public string playerId;
    public string playerLoc;
    public string playerNick;
} 

...

void MyMethod(string s) 
{
    Player player = JsonUtility.FromJson<Player>(s);
    Debug.Log(player.playerLoc);
}

Source: Programmer's answer

Ruzihm
  • 19,749
  • 5
  • 36
  • 48
  • but my object is a kind of mesh. in unity side, its a just byte array, and I want to decode it with js with jslib side. how can I convert it to a string. and then how can create a mesh from the string. – badcode Sep 17 '20 at 11:14
  • 1
    @badcode try doing `console.log(JSON.stringify(dracoGeometry));` and then in C#, writing a `class` that corresponds to the structure you see there. – Ruzihm Sep 17 '20 at 15:14
  • it returns {"ptr":12689328} – badcode Sep 17 '20 at 18:02
0

There are some methods available, but it doesn't seem possible at the moment to send a mesh from js to unity. If you are working with google draco, I recommend you to follow this fork

badcode
  • 581
  • 1
  • 9
  • 28