unity get post
Posted 81192
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unity get post相关的知识,希望对你有一定的参考价值。
采用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
待续。。。。。。。
以上是关于unity get post的主要内容,如果未能解决你的问题,请参考以下文章
Unity Android 使用UnityWebRequest Post 数据后,没有获得服务器返回的数据
WWW / UnityWebRequest POST / GET请求不会从server / url返回最新数据