转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持!
开篇废话:
这个项目准备分四部分介绍:
一:创建可旋转的“3D球”:3D语音天气球(源码分享)——创建可旋转的3D球
二:通过天气服务,从网络获取时实天气信息并动态生成“3D球”
三:Android语音服务和Unity的消息传递
四:Unity3D端和Android端的结合
关于项目的详细介绍和3D球的创建请看上面第一篇文章(重要)
今天主要讲解如何通过获取实时的天气信息去动态的创建3D球。效果图如下:
左边是Unity做出后在电脑上运行效果图(本节需要实现的效果)
右边是Unity结合Android和语音控制之后在手机运行的效果图(所有都介绍完后的最终效果):
{"weatherinfo":{"city":"天津","cityid":"101030100","temp1":"-6℃","temp2":"4℃","weather":"晴","img1":"n0.gif","img2":"d0.gif","ptime":"18:00"}}
{"weatherinfo":{"city":"北京","city_en":"beijing","date_y":"2014年12月16日","date":"十月廿五","week":"星期二","fchh":"18","cityid":"101010100","temp1":"-6℃~5℃","temp2":"-7℃~4℃","temp3":"-5℃~3℃","temp4":"-5℃~1℃","temp5":"-7℃~3℃","temp6":"-6℃~5℃","tempF1":"21.2℉~41℉","tempF2":"19.4℉~39.2℉","tempF3":"23℉~37.4℉","tempF4":"23℉~33.8℉","tempF5":"19.4℉~37.4℉","tempF6":"21.2℉~41℉","weather1":"晴","weather2":"晴转多云","weather3":"多云","weather4":"晴","weather5":"晴","weather6":"晴","img1":"0","img2":"99","img3":"0","img4":"1","img5":"1","img6":"99","img7":"0","img8":"99","img9":"0","img10":"99","img11":"0","img12":"99","img_single":"0","img_title1":"晴","img_title2":"晴","img_title3":"晴","img_title4":"多云","img_title5":"多云","img_title6":"多云","img_title7":"晴","img_title8":"晴","img_title9":"晴","img_title10":"晴","img_title11":"晴","img_title12":"晴","img_title_single":"晴","wind1":"北风3-4级转微风","wind2":"微风","wind3":"微风转北风4-5级","wind4":"北风3-4级","wind5":"微风","wind6":"微风","fx1":"北风","fx2":"微风","fl1":"3-4级转小于3级","fl2":"小于3级","fl3":"小于3级转4-5级","fl4":"3-4级","fl5":"小于3级","fl6":"小于3级","index":"较冷","index_d":"建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。","index48":"","index48_d":"","index_uv":"中等","index48_uv":"","index_xc":"较适宜","index_tr":"适宜","index_co":"较舒适","st1":"4","st2":"-6","st3":"4","st4":"-5","st5":"3","st6":"-4","index_cl":"适宜","index_ls":"基本适宜","index_ag":"极不易发"}}
public void parseJson(string result) { JsonData jd = JsonMapper.ToObject(result); JsonData jdResult = jd["weatherinfo"]; this.name = (string)jdResult["city"]; this.temperature = (string)jdResult["temp1"]; this.temperature2 = (string)jdResult["temp2"]; setTempture (temperature, temperature2); this.weather = (string)jdResult ["weather"]; this.img1 = (string)jdResult ["img1"]; this.img2 = (string)jdResult ["img2"]; }img1,img2表示当前天气的图片,图片需要提前从中国天气网下载。
public void parseJson(string result) { JsonData jd = JsonMapper.ToObject(result); JsonData jdResult = jd["weatherinfo"]; this.name = (string)jdResult["city"]; this.date_y = (string)jdResult["date_y"]; this.date = (string)jdResult["date"]; this.week = (string)jdResult["week"]; this.temperature = (string)jdResult["temp1"]; this.weather = (string)jdResult ["weather1"]; this.wind = (string)jdResult ["wind1"]; this.fl = (string)jdResult ["fl1"]; this.index = (string)jdResult ["index"]; this.index_uv = (string)jdResult ["index_uv"]; this.index_xc = (string)jdResult ["index_xc"]; this.index_tr = (string)jdResult ["index_tr"]; this.index_co = (string)jdResult ["index_co"]; this.index_cl = (string)jdResult ["index_cl"]; this.index_ls = (string)jdResult ["index_ls"]; this.index_ag = (string)jdResult ["index_ag"]; }
// 获取天气信息 public void getWeather(City city, GameObject text) { // 根据城市id得到获取天气服务的url string url = "http://www.weather.com.cn/data/cityinfo/" + city.getCityID() +".html"; // 通过url来给小球赋值 StartCoroutine(GET(url,city,text)); }
IEnumerator GET(string url, City city, GameObject text) { WWW www = new WWW (url); yield return www; //GET请求失败 if (www.error != null) { Debug.Log ("error is :" + www.error); } //GET请求成功 else { //Debug.Log(www.text); // 解析天气信息,给对象赋值 city.parseJson (www.text); // 根据当前city对象中的属性给小球复制 TextMesh tm = (TextMesh)text.GetComponent<TextMesh> (); ChangeColor changeColor = text.GetComponent<ChangeColor>(); // 判断当前对象是城市还是省份来分别赋值 if (isCity) { tm.text = city.getName (); changeColor.name = city.getName(); changeColor.id = city.getCityID(); changeColor.tempture = city.getTempture(); } else { tm.text = city.getProName(); changeColor.name = city.getProName(); changeColor.tempture = city.getTempture(); } // 设置当前颜色 tm.color = city.getWeatherColor(); // 根据城市和省份的不同来判断是否设置图片 foreach (Transform child in text.transform) { if (isCity) { if (child.tag == "pic1") { //Debug.Log(getPicPath(city.getImg1())); Texture2D pic = (Texture2D)Resources.Load(Util.getPicPath(city.getImg1())); child.renderer.material.shader = Shader.Find ("Unlit/Transparent"); child.renderer.material.mainTexture = (Texture)pic; } else if (child.tag == "pic2") { //Debug.Log(getPicPath(city.getImg2())); Texture2D pic = (Texture2D)Resources.Load(Util.getPicPath(city.getImg2())); child.renderer.material.shader = Shader.Find ("Unlit/Transparent"); child.renderer.material.mainTexture = (Texture)pic; } else { TextMesh tm2 = (TextMesh)child.GetComponent<TextMesh> (); tm2.text = city.getDetailsName (); tm2.color = city.getWeatherColor(); } } else { child.active = false; } } // 设置小球大小 text.transform.localScale = city.getSize(); text.GetComponent<ChangeColor>().oldScale = text.transform.localScale; } }显示单个城市详细天气信息的方法和上面大同小异,这里我就不过多介绍了。
一:创建可旋转的“3D球”:3D语音天气球(源码分享)——创建可旋转的3D球
二:通过天气服务,从网络获取时实天气信息并动态生成“3D球”
三:Android语音服务和Unity的消息传递
四:Unity3D端和Android端的结合
原文地址:http://blog.csdn.net/a396901990/article/details/41914739