亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將js對象返回到unity

如何將js對象返回到unity

UYOU 2023-03-10 15:12:54
我正在使用 Google Draco 解碼器對以下網格進行解碼。var dracoGeometry;dracoGeometry = new decoderModule.Mesh();decodingStatus = decoder.DecodeBufferToMesh(buffer, dracoGeometry);當我檢查 draceGeometry 的類型時: console.log( typeof dracoGeometry);我得到“對象”類型。現在我的問題是“我怎樣才能把這個對象恢復到統一狀態”。C# 支持什么返回類型來接受 js 對象?
查看完整描述

2 回答

?
收到一只叮咚

TA貢獻1821條經驗 獲得超5個贊

您可以發送字符串或數字,所以我要做的是創建一個 js 對象,其中包含您需要的數據,然后調用JSON.stringify它以將其轉換為字符串,然后使用以下方式發送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

}

至于當數據為 JSON 字符串形式時您可以對其執行哪些操作,一旦您在 Unity 中擁有它,您可以使用各種解決方案來解碼 JSON 字符串。有關詳細信息,請參閱在 Unity 中序列化和反序列化 Json 和 Json 數組。


所以如果你的 js 看起來像這樣:


function sendToUnity(obj) {

    unityInstance.SendMessage('MyGameObject', 'MyMethod', JSON.stringify(obj));

}


sendToUnity({

    playerId: "8484239823",

    playerLoc: "Powai",

    playerNick:"Random Nick"

});

你可以在 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);

}


查看完整回答
反對 回復 2023-03-10
?
慕碼人8056858

TA貢獻1803條經驗 獲得超6個贊

有一些方法可用,但目前似乎不可能將網格從 js 發送到 unity。如果你正在使用 google draco,我建議你使用這個fork


查看完整回答
反對 回復 2023-03-10
  • 2 回答
  • 0 關注
  • 157 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號