3D voice sky Balloon (source code sharing)-dynamically create 3D balls and voice 3d through the weather service

Source: Internet
Author: User

3D voice sky Balloon (source code sharing)-dynamically create 3D balls and voice 3d through the weather service

Reprinted please indicate this article from the blog of the big glutinous rice (http://blog.csdn.net/a396901990). Thank you for your support!


Opening nonsense:


This project is prepared in four parts:

1. Create a rotated 3D ball ":3D voice sky Balloon (source code sharing)-create a rotated 3D ball

2. obtain real-time weather information from the network through the weather service and dynamically generate a "3D ball"

3. Android voice service and Unity message delivery

4. Combination of Unity3D and Android

 

For more information about the project and the creation of 3D balls, see the first article (important)
Today, we will explain how to dynamically create 3D balls by obtaining real-time weather information. As follows:

 

On the left is the result of running Unity on the computer (the effect to be achieved in this section)

On the right side, Unity runs on the mobile phone after combining Android and voice control (all the final results after introduction ):




Weather Service:
We need to obtain real-time city weather information through the weather service provided by the website. Many such weather services can be found on the Internet, basically by inputting a fixed city ID to return weather information. The weather information format is generally json or xml, which can be used after resolution.
I compared a lot of weather services on the Internet. The following two services are relatively easy to use. You don't need to register for free, so you can simply use them using a browser: Brief weather: http://www.weather.com.cn/data/cityinfo/101010100.html
Detailed weather: http://weather.51wnl.com/weatherinfo/GetMoreWeather? CityCode =101010100& Amp; weatherType = 0The last 101010100 is the number of Beijing. You can check the weather Number of the city online (in the demo). The returned results are as follows:
{"Weatherinfo": {"city": "Tianjin", "cityid": "101030100", "temp1": "-6 ℃", "temp2 ": "4 ℃", "weather": "Qing", "img1": "n0.gif", "img2": "d0.gif", "ptime "}}
{"Weatherinfo": {"city": "beijing", "city_en": "beijing", "date_y": "May December 16, 2014", "date": "May October ", "week": "Tuesday", "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 bytes ~ 41 bytes "," tempF2 ":" 19.4 bytes ~ 39.2 bytes "," tempF3 ":" 23 bytes ~ 37.4 bytes "," tempF4 ":" 23 bytes ~ 33.8 bytes "," tempF5 ":" 19.4 bytes ~ 37.4 bytes "," tempF6 ":" 21.2 bytes ~ 41. "," weather1 ":" clear "," Weather ":" clear to cloudy "," weather3 ":" Cloudy "," weather4 ":" clear "," weather5 ": "clear", "weather6": "clear", "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 ": "clear", "img_title2": "clear", "img_title3": "clear", "img_title4": "Cloudy", "img_title5": "Cloudy", "img_title6 ": "Cloudy", "img_title7": "clear", "img_title8": "clear", "img_t Itle9 ":" Qing "," img_title10 ":" Qing "," img_title11 ":" Qing "," img_title12 ":" Qing "," img_title_single ":" Qing ", "wind1": "Beijing wind 3-4 to breeze", "wind2": "Breeze", "wind3": "breeze to North Wind 4-5", "wind4 ": "Beijing wind 3-4", "wind5": "Breeze", "wind6": "Breeze", "fx1": "Beijing Wind", "fx2": "Breeze ", "fl1": "3-4 to 3", "fl2": "less than 3", "fl3": "less than 3 to 4-5", "fl4 ": "3-4", "fl5": "less than 3", "fl6": "less than 3", "index": "cooler", "index_d ": "We recommend that you wear thick coats and sweaters. Old and weak people should wear coats, coat and sweater. "," Index48 ":" "," index48_d ":" "," index_uv ":" medium "," index48_uv ":" "," index_xc ":" More suitable ", "index_tr": "suitable", "index_co": "More comfortable", "st1": "4", "st2": "-6", "st3 ": "4", "st4": "-5", "st5": "3", "st6": "-4", "index_cl": "appropriate ", "index_ls": "Basic", "index_ag": "extremely difficult "}}
We can see that the returned data is in Json format. The task is to parse the returned data to assign values to our city objects.



Parse data:
I believe that Json Parsing is no stranger to everyone. In this example, the Json parsing uses the LitJson plug-in. Download The LitJson plug-in and put it into Unity and export the package during use. The following is a simple example of the parsing method. The detailed usage of the online tutorials will not be detailed here.
Brief weather: http://www.weather.com.cn/data/cityinfo/101010100.html
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 indicates the current weather image, which needs to be downloaded from China weather network in advance.

Detailed weather: http://weather.51wnl.com/weatherinfo/GetMoreWeather? CityCode = 101010100 & Amp; weatherType = 0
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"];}




Dynamically generate a daily balloon:
Currently, we know how to obtain and parse real-time weather services from the Internet. Now we can use it to dynamically generate our 3D balls.

Unity. StartCoroutine:Because network is involved, the first thing that comes to mind is to use threads to generate each ball. In Unity, you can use StartCoroutine (coroutine) to implement:
In Unity3D, you can use StartCoroutine (string methodName) and StartCoroutine (IEnumerator routine) to open a thread. The difference is that you can use a string as a parameter to enable the thread and terminate the thread before the end of the thread. On the contrary, you can use IEnumerator as a parameter to only wait for the end of the thread and cannot terminate it at any time (unless you use the StopAllCoroutines ); in addition, when the string is used as a parameter, only one parameter can be passed when the thread is enabled, and the performance consumption is greater. IEnumerator is used as a parameter without this restriction.

Main Code:The following code is based on the previous article. The previous article details how to generate a ball with an average distribution in the big ball. The next task is to set various attributes for the generated small ball gameobject:
// Obtain the weather information public void getWeather (City city, GameObject text) {// obtain the weather service url = "http://www.weather.com.cn/data/cityinfo/" + city according to the City id. getCityID () + ". html "; // assign StartCoroutine (GET (url, city, text) to the ball through url ));}

The coordination method of IEnumerator is returned to parse the weather results and assign values to the ball:
IEnumerator GET (string url, City city, GameObject text) {WWW www = new WWW (url); yield return www; // GET request failure if (www. error! = Null) {Debug. log ("error is:" + www. error);} // GET request successful else {// Debug. log (www. text); // parse the weather information and assign the city value to the object. parseJson (www. text); // copy TextMesh tm = (TextMesh) text to the Ball Based on the attributes of the current city object. getComponent <TextMesh> (); ChangeColor changeColor = text. getComponent <ChangeColor> (); // determines whether the current object is a city or a province to assign values to 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 ();} // sets the current color of tm. color = city. getWeatherColor (); // determines whether to set the image 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 ("unregister/Transparent"); child. renderer. material. mainTexture = (Texture) pic;} else if (child. tag = "pic2") {// Debug. log (getPicPath (city. getimg (); Texture2D pic = (Texture2D) Resources. load (Util. getPicPath (city. getimg (); child. renderer. material. shader = Shader. find ("unregister/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 ;}// set the size of the ball text. transform. localScale = city. getSize (); text. getComponent <ChangeColor> (). oldScale = text. transform. localScale ;}}
The method for displaying detailed weather information in a single city is similar to that in the previous section. I will not introduce it more here.

Finally:

1. Create a rotated 3D ball ":3D voice sky Balloon (source code sharing)-create a rotated 3D ball

2. obtain real-time weather information from the network through the weather service and dynamically generate a "3D ball"

3. Android voice service and Unity message delivery

4. Combination of Unity3D and Android


This section does not cover much content, but there are a lot of updated code that can be used as a single Unity project. The reason for not giving a detailed description is mainly the comparison of writing, because the logic is chaotic to quickly complete the function, and the method and code style are not known, right. If you are interested, you can download it and have a look: GitHub: https://github.com/a396901990/3D_Sphere/tree/feature/Wather_3D_Sphere
The remaining content is only for Android voice search, which is combined with Android and Unity. I will update it as soon as possible, so stay tuned...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.