7.11 NGUI天气预报
usingSystem.Collections;
usingSystem.Collections.Generic;
usingUnityEngine;
publicclassAppManager:MonoBehaviour{
//1.显示默认城市的天气信息
//2.选择一个城市a:请求数据b:解析c:更新
publicstaticstringresquestURL="http://apicloud.mob.com/v1/weather/query?key=1e89bc1e64334&city=";//百度天气接口
public WeatherDatacurPageData;
public staticstringcurrentCity="成都";
#regionUI元素
public UILabelmCurrentCity;
publicUILabelmTempLabel;//温度label
publicUILabelmWeather;//weatherlabel
publicUILabelmChildTempLabel;
publicUILabelmWindLabel;//风向label
publicUILabelmWeek;
publicUIPopupListPopupList;
publicUIGridmGrid;
#endregion
publicListfutureChace=newList();
voidStart(){
PopupList.onChange.Add(newEventDelegate(OnPopupValueChange));
RequestPageData();
}
///
///显示数据
///
publicvoidShowPageData(){
mTempLabel.text=curPageData.result[0].temperature;
mWeather.text=curPageData.result[0].future[0].dayTime;
mChildTempLabel.text=curPageData.result[0].future[0].temperature;
mWindLabel.text=curPageData.result[0].future[0].wind;
mWeek.text=curPageData.result[0].future[0].date;
CreatFutureData();
}
///
///请求数据
///
publicvoidRequestPageData(){
StartCoroutine(StartRequest());
}
IEnumeratorStartRequest(){
//post请求方式
WWWFormform=newWWWForm();
form.AddField("key","1e89bc1e64334");
form.AddField("city",currentCity);
WWWwww=newWWW(resquestURL,form);
while(!www.isDone){
yieldreturnnull;
}
stringjsonText=www.text;
curPageData=JsonParseTool.Parse(jsonText);
ShowPageData();//更新界面显示
}
publicvoidOnPopupValueChange(){
if(UIPopupList.current!=null){
currentCity=UIPopupList.current.isLocalized?
Localization.Get(UIPopupList.current.value):
UIPopupList.current.value;
}
mCurrentCity.text=currentCity;
RequestPageData();
}
///
///未来五天的天气页面
///
publicvoidCreatFutureData(){
if(curPageData.result[0].future.Count>0){
Listfdate=curPageData.result[0].future;
Objectprefab=Resources.Load("Item");
for(inti=0;i
if(futureChace.Count-1>=i){
futureChace[i].Init(fdate[i]);
}else{
GameObjectitem=Instantiate(prefab)asGameObject;
item.transform.SetParent(mGrid.transform);
item.transform.localScale=Vector3.one;
item.GetComponent().Init(fdate[i]);
futureChace.Add(item.GetComponent());
}
}
mGrid.Reposition();//刷新网格
}
}
}
usingSystem.Collections;
usingSystem.Collections.Generic;
usingUnityEngine;
publicclassJsonParseTool{
publicstaticWeatherDataParse(stringjsonText){
WeatherDatadata=JsonUtility.FromJson(jsonText);
returndata;
}
}
[System.Serializable]
publicclassWeatherData{
publicstringmsg;
publicListresult;
publicstringretCode;
}
[System.Serializable]
publicclassResultData{
publicstringairCondition;
publicstringcity;
publicstringdate;
publicstringdistrct;
publicstringdressingIndex;
publicstringexerciseIndex;
publicListfuture;
publicstringhumidity;
publicstringpollutionIndex;
publicstringprovince;
publicstringsunrise;
publicstringsunset;
publicstringtemperature;
publicstringtime;
publicstringupdateTime;
publicstringwashIndex;
publicstringweather;
publicstringweek;
publicstringwind;
}
[System.Serializable]
publicclassFutureData{
publicstringdate;
publicstringdayTime;
publicstringnight;
publicstringtemperature;
publicstringweek;
publicstringwind;
}
usingSystem.Collections;
usingSystem.Collections.Generic;
usingUnityEngine;
publicclassItem:MonoBehaviour{
publicUILabelweekLabel;
publicUILabeltempLabel;
publicUILabelweatherLabel;
publicUILabelmWindLabel;
publicvoidInit(FutureDatadata){
if(data!=null){
weekLabel.text=data.week;
tempLabel.text=data.temperature;
weatherLabel.text=data.dayTime;
mWindLabel.text=data.wind;
}
}
}