码迷,mamicode.com
首页 > 编程语言 > 详细

unity get post

时间:2017-10-22 17:05:50      阅读:336      评论:0      收藏:0      [点我收藏+]

标签: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

待续。。。。。。。

 

unity get post

标签:rem   start   天气   star   color   col   unit   set   error   

原文地址:http://www.cnblogs.com/llstart-new0201/p/7709802.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!