首页 > 解决方案 > 我很难在检查器中恢复我的变量 id

问题描述

你能帮我在unity的inspector中获取id的值吗?我在统一控制台中有一个 json,我做了一个程序来访问这个 json 中的数据,然后在统一检查器中恢复它。对于“位置”,它是有效的,但对于“id”没有。我尝试了很多方法,但没有奏效。

这是我在启动函数和更新函数中的代码,我已经搜索过,但我没有找到如何为 id 执行此操作:

我正在使用 SimpleJSON。

if(mono != null)
{
     JSONNode id = Face["mono"]["id"].AsInt; 
     JSONNode position = Face["mono"]["position"];
     Vector3 pos = new Vector3(position["x"].AsFloat, position["y"].AsFloat, position["z"].AsFloat);
     int ident = new ident(id);
    
     x = pos.x;
     y = pos.y;
     z = pos.z;
     id= ident.id;
}

private void Update()
{
     Vector3 newPosition = new Vector3(x, y, z);
     gameObject.transform.localPosition = newPosition;
}

这是我的 json: { "cmd": "data", "frame_time": "2021-06-10T11:16:54.678596+00:00", "start_time": "2021-06-10T10:35:40.516632+ 00:00", "streams": [ { "name": "usb0.1245.2", "objects": [ { "name": "face", "mono": { "id": "18", "position “:{“x”:“0.073997”,“y”:“-0.024979”,“z”:“0.677220”}},“多”:[{“id”:“18”,“位置”:{“ x": "0.073997", "y": "-0.024979", "z": "0.677220" } } ] } ] } ], "play": {"list": "test", "item": "right_key " } }

标签: jsonunity3dinspector

解决方案


如果你从你那里得到一个int,你可以通过设置你的类的一个或一个变量来Face["mono"]["id"].AsInt;在检查器中设置它,如下所示:public[SerializeField] private

public myInt = 0; //also [SerializeField] private myInt = 0;
if(mono != null)
{
     myInt = Face["mono"]["id"].AsInt; 
     JSONNode position = Face["mono"]["position"];
     Vector3 pos = new Vector3(position["x"].AsFloat, position["y"].AsFloat, position["z"].AsFloat);
     int ident = new ident(id);
    
     x = pos.x;
     y = pos.y;
     z = pos.z;
     id= ident.id;
}

private void Update()
{
     Vector3 newPosition = new Vector3(x, y, z);
     gameObject.transform.localPosition = newPosition;
}

推荐阅读