标签:rem start 天气 star color col unit set error
采用WWW获取网络数据:
(一)get
1)天气数据下载
private string weatherApi = "http://www.sojson.com/open/api/weather/json.shtml?city={0}";
//private string weatherApi = "http://www.sojson.com/open/api/weather/xml.shtml?city={0}"; //xml format
public string city = "闵行区";
private string fullWeatherApi;
public void OnRequest()
{
StartCoroutine(DataRequest(fullWeatherApi));
}
private bool isResponseValid(WWW www)
{
if (www.error != null)
{
print("Conn failed!");
return false;
}
if(string.IsNullOrEmpty(www.text))
{
print("Bad result");
return false;
}
return true;
}
private IEnumerator DataRequest(string url)
{
WWW www = new WWW(url);
yield return www;
if (!isResponseValid(www)) yield break;
print(www.text);
}
2)texture下载
public GameObject quad; private Texture2D downloadTexture; public void OnRequest() { ImageManage(SetSpirite); } private void ImageManage(Action<Texture2D> action) { if(downloadTexture ==null)//缓存检测 { StartCoroutine(ImageRequest(beautyApi, action)); } else { action(downloadTexture); } } private IEnumerator ImageRequest(string url,Action<Texture2D> action) { WWW www = new WWW(url); yield return www; print("request"); downloadTexture = www.texture; action(www.texture); } private void SetSpirite(Texture2D texture) { quad.GetComponent<MeshRenderer>().material.mainTexture = texture; }
(二)post
待续。。。。。。。
标签:rem start 天气 star color col unit set error
原文地址:http://www.cnblogs.com/llstart-new0201/p/7709802.html