我正在尝试从web服务获取一些json,但当我尝试使用result时,我收到了这个错误。text它会出现错误“WWW尚未准备好下载”如果我不尝试使用这个变量,我不会收到任何错误
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Boomlagoon.JSON;
public class ClassInfoScript : MonoBehaviour {
private IEnumerator coroutine;
public string name;
private bool showInfo = false;
//esta variable es para controlar que solo seconecte uan vez al entrar en un collider
private bool connected = false;
private bool isClass = false;
private bool isDependency = false;
private bool isOffice= false;
public GUIStyle myStyle;
public GUIStyle labelStyle;
enum Types{classroom, dependency, office};
void Start(){
}
void OnGUI() {
if (showInfo) {
GUI.Box (new Rect (Screen.width * 0.02f, Screen.height * 0.02f, Screen.width * 0.15f, Screen.height * 0.75f), "", myStyle);
WWW result = null;
if (isClass) {
if(!connected){
result = get ("http://localhost/ws/get/clase/D1");
}
//I get the error here when I try to use result.text
JSONObject jo = JSONObject.Parse(result.text);
connected = true;
}
}
//obtener el nombre
public void setName(string value){
name = value;
}
//cambiar el nombre
public string getName(){
return name;
}
//cambiar el valor de showInfo
public void changeInfoState(){
showInfo = !showInfo;
}
public void changeClass(){
isClass = !isClass;
}
public void changeDependency(){
isDependency = !isDependency;
}
public void changeOffice(){
showInfo = !showInfo;
}
public WWW get(string url){
WWW w = new WWW (url);
StartCoroutine(AskWebservice(w));
return w;
}
IEnumerator AskWebservice(WWW w){
Debug.Log ("Coroutine started");
yield return w;
if (w.error == null){
Debug.Log("WWW Ok!: " + w.text);
} else {
Debug.Log("WWW Error: "+ w.error);
}
}
}
现在我的onGui函数是这样的
void OnGUI() {
if (showInfo) {
GUI.Box (new Rect (Screen.width * 0.02f, Screen.height * 0.02f, Screen.width * 0.15f, Screen.height * 0.75f), "", myStyle);
WWW result = null;
JSONObject jo = null;
if (isClass) {
if(!connected){
string url = "http://localhost/ws/get/clase/"+getName();
StartCoroutine(HandleWWWRequest(url, (www) => {
jo = JSONObject.Parse(www.text);
Debug.Log(jo.GetString("info"));
connected = true;
}));
}
GUI.Label (new Rect (Screen.width * 0.03f, Screen.height * 0.04f, Screen.width * 0.15f, Screen.height * 0.75f), jo.GetString("nombre"), labelStyle);
GUI.Label (new Rect (Screen.width * 0.03f, Screen.height * 0.15f, Screen.width * 0.15f, Screen.height * 0.75f), jo.GetString("info"), labelStyle);
} else if (isDependency) {
/*coroutine = AskWebservice ("dependencia", name, Types.dependency);
if(!connected)
StartCoroutine (coroutine);
GUI.Label (new Rect (Screen.width * 0.03f, Screen.height * 0.04f, Screen.width * 0.15f, Screen.height * 0.75f), response [0], labelStyle);
GUI.Label (new Rect (Screen.width * 0.03f, Screen.height * 0.15f, Screen.width * 0.15f, Screen.height * 0.75f), response [1], labelStyle);
GUI.Label (new Rect (Screen.width * 0.03f, Screen.height * 0.20f, Screen.width * 0.15f, Screen.height * 0.75f), response [2], labelStyle);*/
}
connected = true;
}
}
但在这两行中
GUI.Label (new Rect (Screen.width * 0.03f, Screen.height * 0.04f, Screen.width * 0.15f, Screen.height * 0.75f), jo.GetString("nombre"), labelStyle);
GUI.Label (new Rect (Screen.width * 0.03f, Screen.height * 0.15f, Screen.width * 0.15f, Screen.height * 0.75f), jo.GetString("info"), labelStyle);
我不能在标签中使用jsonobject字段,它是空的,我已经尝试将这个字段存储在另一个变量和列表中,但它不起作用,我如何在执行协程后存储jsonobject字段以使用它??我想在标签上使用这个滤镜。