unity 获得java数据,有接口地址,请求方式为POST,请求参数为{ "metroid": 279 },返回结果为{ "returnCode": 0, "returnMessage": "调用成功", "data": [ { "doorfaultcount": 7, "doorsubhealthcount": 0, "carriageid": 1738, "carriageno": "01011", "carriagename": "HHHT01011", "doorinfolist": [ { "sort": 0, "doorid": 17414, "doorno": "222222222222222", "doorbusno": "01011", "side": "左", "pairid": null, "iskeydoor": false, "statuscode": "FAULT", "isfaultandsubhealth": null, "repaircode": "NOMAL", "exceptioncode": "NOMAL", "statusname": "故障", "faultcode": null, "faultname": null, "time": null, "updown": null, "paramvalueid1": null, "paramvalueid2": null, "paramvalueid3": null, "paramvalueid4": null, "paramvalueid5": null, "dataid": null, "doorstatus": 1 },获得返回结果的"doorno"数据,并将"doorno"数据内容呈现在文本框中,unity webgl中的完整代码怎么写
时间: 2023-07-09 19:54:16 浏览: 90
Unity 中通过UnityWebRequest POST传JSON格式的参数请求数据。
在Unity中获得Java数据可以使用UnityWebRequest类,代码如下:
```
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class GetJavaData : MonoBehaviour
{
public Text resultText;
void Start()
{
StartCoroutine(GetData());
}
IEnumerator GetData()
{
//设置请求地址
string url = "接口地址";
//设置请求参数
WWWForm form = new WWWForm();
form.AddField("metroid", "279");
//发送POST请求
UnityWebRequest www = UnityWebRequest.Post(url, form);
yield return www.SendWebRequest();
//判断请求是否出错
if (www.result != UnityWebRequest.Result.Success)
{
Debug.Log(www.error);
}
else
{
//解析返回的JSON数据
string json = www.downloadHandler.text;
JSONObject jsonObject = new JSONObject(json);
string doorNo = jsonObject["data"][0]["doorinfolist"][0]["doorno"].str;
//将数据呈现在文本框中
resultText.text = doorNo;
}
}
}
```
注意:需要将"JSONObject.cs"和"JSONObjectAddons.cs"这两个文件添加到Unity项目中,可以在GitHub上下载。另外,为了能够在Unity WebGL中运行,需要将接口地址设置为支持跨域访问的地址。
阅读全文