Advertisement
Rakshalpha

CurrentWeather

Apr 10th, 2018
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.01 KB | None | 0 0
  1. // As seen from this original post: https://forum.unity.com/threads/current-weather-script.242009/
  2. // I am not claiming that I wrote this, See link above for original post.
  3. // SimpleJSON is also linked there..
  4.  
  5. using UnityEngine;
  6. using System.Collections;
  7. using SimpleJSON;
  8.  
  9. public class GetMyWeather : MonoBehaviour {
  10.  
  11.     public UILabel myWeatherLabel;
  12.     public UITexture myWeatherCondition;
  13.  
  14.     public string currentIP;
  15.     public string currentCountry;
  16.     public string currentCity;
  17.  
  18.     //retrieved from weather API
  19.     public string retrievedCountry;
  20.     public string retrievedCity;
  21.     public int conditionID;
  22.     public string conditionName;
  23.     public string conditionImage;
  24.  
  25.     void Start()
  26.     {
  27.         StartCoroutine(SendRequest());
  28.     }
  29.  
  30.     IEnumerator SendRequest()
  31.     {
  32.         //get the players IP, City, Country
  33.         Network.Connect("http://google.com");
  34.         currentIP = Network.player.externalIP;
  35.         Network.Disconnect();
  36.  
  37.         WWW cityRequest = new WWW("http://www.geoplugin.net/json.gp?ip=" + currentIP); //get our location info
  38.         yield return cityRequest;
  39.  
  40.         if (cityRequest.error == null || cityRequest.error == "")
  41.         {
  42.             var N = JSON.Parse(cityRequest.text);
  43.             currentCity = N["geoplugin_city"].Value;
  44.             currentCountry = N["geoplugin_countryName"].Value;
  45.         }
  46.  
  47.         else
  48.         {
  49.             Debug.Log("WWW error: " + cityRequest.error);
  50.         }
  51.  
  52.         //get the current weather
  53.         WWW request = new WWW("http://api.openweathermap.org/data/2.5/weather?q=" + currentCity); //get our weather
  54.         yield return request;
  55.  
  56.         if (request.error == null || request.error == "")
  57.         {
  58.             var N = JSON.Parse(request.text);
  59.  
  60.             retrievedCountry = N["sys"]["country"].Value; //get the country
  61.             retrievedCity = N["name"].Value; //get the city
  62.  
  63.             string temp = N["main"]["temp"].Value; //get the temperature
  64.             float tempTemp; //variable to hold the parsed temperature
  65.             float.TryParse(temp, out tempTemp); //parse the temperature
  66.             float finalTemp = Mathf.Round((tempTemp - 273.0f)*10)/10; //holds the actual converted temperature
  67.  
  68.             int.TryParse(N["weather"][0]["id"].Value, out conditionID); //get the current condition ID
  69.             //conditionName = N["weather"][0]["main"].Value; //get the current condition Name
  70.             conditionName = N["weather"][0]["description"].Value; //get the current condition Description
  71.             conditionImage = N["weather"][0]["icon"].Value; //get the current condition Image
  72.  
  73.             //put all the retrieved stuff in the label
  74.             myWeatherLabel.text =
  75.                 "Country: " + retrievedCountry
  76.                 + "\nCity: " + retrievedCity
  77.                 + "\nTemperature: " + finalTemp + " C"
  78.                 + "\nCurrent Condition: " + conditionName
  79.                 + "\nCondition Code: " + conditionID;
  80.         }
  81.         else
  82.         {
  83.             Debug.Log("WWW error: " + request.error);
  84.         }
  85.  
  86.         //get our weather image
  87.         WWW conditionRequest = new WWW("http://openweathermap.org/img/w/" + conditionImage + ".png");
  88.         yield return conditionRequest;
  89.  
  90.         if (conditionRequest.error == null || conditionRequest.error == "")
  91.         {
  92.             //create the material, put in the downloaded texture and make it visible
  93.             var texture = conditionRequest.texture;
  94.             Shader shader = Shader.Find("Unlit/Transparent Colored");
  95.             if (shader != null)
  96.             {
  97.                 var material = new Material(shader);
  98.                 material.mainTexture = texture;
  99.                 myWeatherCondition.material = material;
  100.                 myWeatherCondition.color = Color.white;
  101.                 myWeatherCondition.MakePixelPerfect();
  102.             }
  103.         }
  104.         else
  105.         {
  106.             Debug.Log("WWW error: " + conditionRequest.error);
  107.         }
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement