【发布时间】:2018-03-09 15:36:56
【问题描述】:
最近我一直在尝试以 JSON 的形式从外部源获取一些数据。 我使用的库是 Newtonsoft.Json 的统一分支。当我在我的计算机上运行该项目时,它会从外部源中提取数据,并将其转换为一个对象。我制作的 UI/文本元素应该显示从我的外部源提取的数据,当我在我的主计算机上运行项目时它没有问题并且数据显示没有问题,但是当我将项目发送到我的 Hololens 时,我的调试器获取数据,我可以从字面上看到数据正在从外部源中提取,但数据不会显示在 hololens 上。谁能告诉我如何解决这个问题?
我的代码如下:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//using SimpleJSON;
using Newtonsoft.Json;
[System.Serializable]
public class TimeProperties
{
public string Year { get; set; }
public string Month { get; set; }
public string Day { get; set; }
public string Hour { get; set; }
public string Minutes { get; set; }
public string Seconds { get; set; }
}
[System.Serializable]
public class TimeClass
{
public TimeProperties Time { get; set; }
}
public class test : MonoBehaviour
{
string url = "http://172.16.24.135:8080";
public Text year;
public Text month;
public Text day;
public Text hour;
public Text minutes;
public Text seconds;
private void Start()
{
StartCoroutine(UpdateValues());
}
IEnumerator PullJsonData()
{
Debug.Log("entered");
WWW www = new WWW(url);
yield return www;
if(www.error != null)
{
print("There was an error getting the data: " + www.error);
yield break;
}
string jsonstring = www.text;
var data = JsonConvert.DeserializeObject<TimeClass>(jsonstring);
Debug.Log(data.Time.Seconds);
var jaren = data.Time.Year; //data["Year"].AsInt;
var maanden = data.Time.Month;//data["Month"].AsInt;
var dagen = data.Time.Day;//data["Day"].AsInt;
var uren = data.Time.Hour;//data["Hour"].AsInt;
var minuten = data.Time.Minutes;//data["Minutes"].AsInt;
var seconden = data.Time.Seconds;//data["Seconds"].AsInt;
year.text = "Year: " + jaren;
month.text = "Month: " + maanden;
day.text = "Days: " + dagen;
hour.text = "Hours: " + uren;
minutes.text = "Minutes: " + minuten;
seconds.text = "Seconds: " + seconden;
}
IEnumerator UpdateValues()
{
while (true)
{
StartCoroutine(PullJsonData());
yield return new WaitForSeconds(1);
}
}
}
我使用“Release x86”通过 Visual Studio Code 2017 将它发送到我的 hololens。我还收到以下错误:
(Filename: 'C:\buildslave\unity\build\artifacts/generated/Metro/runtime/DebugBindings.gen.cpp Line: 51)
Display is Transparent
(Filename: C:\buildslave\unity\build\artifacts/generated/Metro/runtime/DebugBindings.gen.cpp Line: 51)
There was an error getting the data:
(Filename: C:\buildslave\unity\build\artifacts/generated/Metro/runtime/DebugBindings.gen.cpp Line: 51)
Failed to get spatial stage statics - can't retrieve or interact with boundaries! Error code: '0x80040154'.
(Filename: C:\buildslave\unity\build\Runtime/VR/HoloLens/StageRoot.cpp Line: 20)
entered
(Filename: C:\buildslave\unity\build\artifacts/generated/Metro/runtime/DebugBindings.gen.cpp Line: 51)'
我每秒都从外部源中提取 JSON 数据,因此在运行后的每一秒,这都会显示在我的调试中: 输入(这是类 pulljsondata() 中的 debug.log)。 获取数据时出错:
(Filename: C:\buildslave\unity\build\artifacts/generated/Metro/runtime/DebugBindings.gen.cpp Line: 51)
【问题讨论】:
-
首先,该端点可用于 Hololens,对吗?其次,您是否检查了 Internet 客户端功能?
-
我确实检查了客户端功能,对于端点,您的意思是这样的:docs.unity3d.com/ScriptReference/…?我可以看到数据是从我在 hololens 上的外部 URL 中提取的,当我在我的计算机上启动它时它可以正常工作,但是在我设置的 ui/text 元素上的 hololens 上没有显示任何内容
-
我认为这可能是导致问题的
WWW类。我在我的一个项目中使用了它,但是,它仅用于从 HoloLens 获取本地保存的文件。但是,由于某种原因,它不会在协程内执行WWW。相反,我只是调用了一次,我没有遇到任何问题。可能只是因为我正在访问本地数据。你的jsonString是空的吗? -
我回去工作后再打印出来
标签: json debugging unity3d json.net hololens