将Json.txt的天气信息获取并显示在UI
usingUnityEngine;
usingSystem.Collections;
usingSystem.Collections.Generic;
usingLitJson;
usingSystem.IO;
publicclassWeatherDataModel{
publicdelegatevoidUpdateDataDelegate();
publiceventUpdateDataDelegateUpdateDataEvent;
public int error;
public string status;
public string date;
public List <ResultModel>results;
publicvoidJsonToModel(stringpath){
StreamReadersr=newStreamReader(path);
stringjsonStr=sr.ReadToEnd();
WeatherDataModelmodel=JsonMapper.ToObject(jsonStr);
this.date=model.date;
this.results=model.results;
UpdateDataEvent();
}
}
publicclassResultsModel{
publicstringcurrentCity;
publicstringpm25;
public List<WeatherIndexModel>index;
public List<WeatherWeather_DataModel>weather_data;
}
publicclassWeatherWeather_DataModel{
publicstringdate;
publicstringdayPictureUrl;
publicstringnightPictureUrl;
publicstringweather;
publicstringwind;
publicstringtemperature;
}
publicclassWeatherIndexModel{
publicstringtitle;
publicstringzs;
publicstringtipt;
publicstringdes;
}
usingUnityEngine;
usingSystem.Collections;
usingUnityEngine.UI;
usingUnityEngine.Networking;
publicclassUIView:MonoBehaviour{
publicTextdataText;
publicTextcityText;
publicTextpmText;
publicTextweatherText;
publicTextwindText;
publicTexttemperatureText;
publicTexttitleText;
publicTextzsText;
publicTexttipText;
publicTextdesText;
publicTexttimeText;
//publicvoidUpdateUserInterface(stringdata,stringcity,stringpm,stringweather,stringwind,stringtemperature,stringtitle,stringzs,stringtip,stringdes){
//
//dataText.text=data;
//cityText.text=city;
//pmText.text=pm;
//weatherText.text=weather;
//windText.text=wind;
//temperatureText.text=temperature;
//titleText.text=title;
//zsText.text=zs;
//tipText.text=tip;
//desText.text=des;
//timeText.text=
//
//}
}
usingUnityEngine;
usingSystem.Collections;
publicclassUIController:MonoBehaviour{
UIViewview;
WeatherDataModelmodel;
voidStart( ){
view=GameObject.Find("Canvas").GetComponent( );
model=newWeatherDataModel( );
model.UpdateDataEvent+=ShowDate;
}
voidUpdate( ){
model.JsonToModel(Application.dataPath+"/JsonFile/Json.txt");
}
voidShowDate( ){
view.dataText.text=model.date;
view.cityText.text=model.results[0].currentCity;
view.pmText.text=model.results[0].pm25;
view.weatherText.text=model.results[0].weather_data[0].weather;
view.windText.text=model.results[0].weather_data[0].wind;
view.temperatureText.text=model.results[0].weather_data[0].temperature;
view.titleText.text=model.results[0].index[1].title;
view.zsText.text=model.results[0].index[1].zs;
view.tipText.text=model.results[0].index[1].tipt;
view.desText.text=model.results[0].index[1].des;
view.timeText.text=model.results[0].weather_data[0].date;
}
}
将网络请求来的数据显示在UI
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class WeatherModel {
public int error;
public string status;
public string date;
public List<WeatherResultsModel>results;
}public class WeatherResultsModel {
public string currentCity;
public string pm25;
public List<WeatherIndexModel>index;
public List<WeatherDataModel>weather_data;
}
public class WeatherIndexModel {
public string title;
public string zs;
public string tipt;
public string des;
}
public class WeatherDataModel {
public string date;
public string dayPictureUrl;
public string nightPictureUrl;
public string weather;
public string wind;
public string temperature;
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.Networking;
public class UIManager : MonoBehaviour {
public Text time_Text;
public Text date_Text;
public Text title_Text;
public Image weather_image;
public void UpdateUserInterface (string time, string date, string title, string image_url){
time_Text.text = time;
date_Text.text = date;
title_Text.text = title;
StartCoroutine (DownloadImage (image_url));
}
IEnumerator DownloadImage (string url) {
// WWW www = new WWW (url);
// yield return www;
//
// if (string.IsNullOrEmpty (www.error)) {
// Sprite sprite = Sprite.Create (www.texture, new Rect (0, 0, 100, 100), Vector2.zero);
// weather_image.sprite = sprite;
// }
UnityWebRequest wr = new UnityWebRequest (url);
DownloadHandlerTexture textureHandler = new DownloadHandlerTexture (true);
wr.downloadHandler = textureHandler;
yield return wr.Send ();
if (!wr.isError) {
Sprite sprite = Sprite.Create (textureHandler.texture, new Rect (0, 0, 42, 30), Vector2.zero);
weather_image.sprite = sprite;
}
}
}
网络请求数据
using UnityEngine;
using System.Collections;
using System;
using UnityEngine.Networking;
public class NetworkingScript {
public IEnumerator GetWeatherData (string url, Actionact, Actionerror) {
if (url.Length > 0) {
// WWW www = new WWW (url);
// yield return www;
//
// if (string.IsNullOrEmpty (www.error)) {
// act.Invoke (www.text);
//// act (www.text);
// }
UnityWebRequest wr = new UnityWebRequest (url);
wr.downloadHandler = new DownloadHandlerBuffer ();
yield return wr.Send ();
if (!wr.isError) {
act.Invoke (wr.downloadHandler.text);
} else {
error.Invoke (wr.error);
}
}
}
}
using UnityEngine;
using System.Collections;
using LitJson;
public class GameController : MonoBehaviour {
WeatherModel model;
UIManager manager;
NetworkingScript networking;
string Base_url = "http://api.map.baidu.com/telematics/v3/weather?location=";
string url = "&output=json&ak=G0dStAGfFDOfwPYGagYt8FDKNKw2lRE1";
string city = "成都";
string finallyUrl;
void Awake ( ) {
manager = GameObject.Find ("UIManager").GetComponent();}
void Start ( ) {
finallyUrl = Base_url + city + url;
networking = new NetworkingScript ();
StartCoroutine (networking.GetWeatherData (finallyUrl, delegate(string jsonStr) {//网络请求成功 (连接服务器成功)model = JsonMapper.ToObject(jsonStr);
if (model.status == "success") {
//更新UI
manager.UpdateUserInterface (model.date,
model.results[0].weather_data[0].date,
model.results[0].index[0].title,
model.results[0].weather_data[0].dayPictureUrl);
}else {
print (model.error);
}
}, delegate(string error) {
//网络请求失败(连接服务器失败)
Debug.Log (error);
}));
}
}