标签:
using UnityEngine; using System.Collections; public class Global { public static string areaUrl = "http://v.juhe.cn/weather/citys"; //支持的城市 ,在聚合数据/天气预报的第6条 public static string weatherUrl = "http://v.juhe.cn/weather/index"; //更加城市名或id查询天气 ,在聚合数据/天气预报的第1条 public static string appKey = "您申请的AppKey"; //申请是appkey }
保存,回到Unity中,我们在创建一个脚本。命名为Weather。这个脚本中,我们将开启两个协程,一个用来请求区域信息,一个用来请求天气信息。代码如下:
using UnityEngine; using System.Collections; using LitJson; //引入 LitJson public class Weather : MonoBehaviour { public UIPopupList provincePopupList; //省份 public UILabel provinceLable; //显示省份的label public UIPopupList cityPopupList; //城市 public UILabel cityLable; //显示城市的label public UIPopupList districtPopupList; //区/县 public UILabel districtLable; //显示区/县的label public UILabel lable; //显示结果的label private JsonData resultData; //请求到的城市数据 void Start() { provincePopupList.Clear(); //清空省份list StartCoroutine(RequestData()); //开启协程开请求区域数据 } //选择城市,当我们的省份的provincePopupList的值发生变化时,我们调用这个方法,来更新城市 public void SelectCity() { cityPopupList.Clear(); //情况城市List for (int i = 0; i < resultData.Count; i++) { //遍历区域数据,找到省份List上的省份 if (resultData[i]["province"].ToString().Equals(provinceLable.text)) { //不存在这个城市,加到城市List上 if (!cityPopupList.items.Contains(resultData[i]["city"].ToString())) cityPopupList.AddItem(resultData[i]["city"].ToString()); } } } //选择曲线,当我们的省份的cityPopupList的值发生变化时,我们调用这个方法,来更新区县 public void SelectDistrict() { districtPopupList.Clear(); //清空曲线List for (int i = 0; i < resultData.Count; i++) { //遍历区域数据,找到城市List上的城市 if (resultData[i]["city"].ToString().Equals(cityLable.text)) { //不存在这个区县,加到区县List上 if (!districtPopupList.items.Contains(resultData[i]["district"].ToString())) districtPopupList.AddItem(resultData[i]["district"].ToString()); } } } //展示天气 public void ShowWeather() { //拼接请求城市的Url Global.weatherUrl = Global.weatherUrl + "?format=2&cityname=" + GetUTF8(districtLable.text) + "&key=" + Global.appKey; StartCoroutine(RequestWeatherData(Global.weatherUrl)); //开启协程来查询天气数据 } //将字符串转换成UTF8编码,因为根据城市名称来请求数据是,需要传入的城市名为UTF8编码格式的 private string GetUTF8(string str) { byte[] buffer = System.Text.Encoding.UTF8.GetBytes(str); return System.Text.Encoding.UTF8.GetString(buffer); } //协程,用来请求区域数据 IEnumerator RequestData() { WWW www = new WWW(Global.areaUrl + "?key="+ Global.appKey); //拼接请求区域URL while (!www.isDone) { //没有完成 yield return null; } if (www.error == null) //没有错误 { JsonData data = JsonMapper.ToObject(www.text); //将请求到的Json 数据转换成 JsonData对象 resultData = data["result"]; //将结果,赋值给我们的resultData ,因为后面还要用到这个数据来刷新城市和区县 for (int i = 0; i < resultData.Count; i++) { //遍历区域数据,如果没有这个省份,则加上 if (!provincePopupList.items.Contains(resultData[i]["province"].ToString())) provincePopupList.AddItem(resultData[i]["province"].ToString()); } } else //请求出错 print(www.error); } //请求天气情况的协程 IEnumerator RequestWeatherData(string url) { lable.text = ""; //情况显示 WWW wwwWeather = new WWW(url); while (!wwwWeather.isDone) { yield return null; } if (wwwWeather.error == null) { #region 解析Json字符串,并显示, JsonData weatherData = JsonMapper.ToObject(wwwWeather.text); lable.text += "查询结果:\t" + weatherData["reason"].ToString() + "\n\n"; JsonData skData = weatherData["result"]["sk"]; lable.text += "当前实况天气:" + "\n"; lable.text += "当前温度:\t" + skData["temp"].ToString() + "\n"; lable.text += "当前风向:\t" + skData["wind_direction"].ToString() + "\n\n"; lable.text += "当前风力:\t" + skData["wind_strength"].ToString() + "\n\n"; lable.text += "当前湿度:\t" + skData["humidity"].ToString() + "\n\n"; lable.text += "当前时间:\t" + skData["time"].ToString() + "\n\n"; lable.text += "未来几天天气:" + "\n"; JsonData futureData = weatherData["result"]["future"]; for (int i = 0; i < futureData.Count; i++) { lable.text += "Week:" + "\t" + futureData[i]["week"].ToString() + "\n\n"; lable.text += "Date:" + "\t" + futureData[i]["date"].ToString() + "\n"; lable.text += "Temperature:" + futureData[i]["temperature"].ToString() + "\n\n"; lable.text += "Weather:" + futureData[i]["weather"].ToString() + "\n\n"; lable.text += "Wind:" + "\t" + futureData[i]["wind"].ToString() + "\n\n"; } #endregion } } }
编写完成脚本,保存,我们回到Unity中,将Weather脚本挂载到UIRoot上(随意),然后为其拖应用,然后,我们选择ProvincePopupList,在其UI PopupList组件上为其添加事件,将UI Root拖给Notify,选择方法SelectCity。类似,City Popup List添加事件SelectDistrict,DistrictPopup List添加事件 ShowWeather。好了,我们可以点击播放按钮,来测试下,如果没有意外的话,会出现如下界面:
标签:
原文地址:http://www.cnblogs.com/AhrenLi/p/4883992.html