Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Current Weather Script

Discussion in 'Made With Unity' started by johnnydj, Apr 22, 2014.

  1. johnnydj

    johnnydj

    Joined:
    Apr 20, 2012
    Posts:
    211
    Was bored today at work and I made a Script that retrieves current weather conditions based on the player's IP address.
    It is using SimpleJSON so make sure you DOWNLOAD HERE then import the unitypackage into your project.
    Then just copy paste the below code into a C# script.

    It is using NGUI label and texture so if you don't have it, just replace
    public UILabel myWeatherLabel;
    public UITexture myWeatherCondition;
    with the GUI versions.

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

    Synapse11

    Joined:
    Apr 18, 2014
    Posts:
    39
    This has great potential, for a game in which you make the game environment reflect the weather outside.
    Do you attack your enemys now, or build up and attack on a foggy day!
    Cue players looking at the weather forecast to plan their strategy.
     
    ahsen35813 likes this.
  3. Lucifer95

    Lucifer95

    Joined:
    Jul 23, 2014
    Posts:
    1
    hello,
    this script is awesome..
    but i want to use this in my game and for that i want name of all weather conditions..so i can imply weather in game according to that..
    can you help
    thank you
     
  4. nakiks

    nakiks

    Joined:
    Dec 4, 2011
    Posts:
    24
    Compliments mate (y)
    Saved a lot of time.
     
  5. Vert

    Vert

    Joined:
    Mar 23, 2010
    Posts:
    1,099
    Awesome! I have always wondered what it would be like to have a game where the weather outside affected the game world. This looks to be what I need to try to make that myself since I never got around to trying to figure it out for myself. Thanks for sharing!

    Edit: And for those who are looking for a list of conditions so you can do neat things in game like I want to, I think this is what we need(as I see it's polling openweathermap.org for the data in the script):
    http://openweathermap.org/weather-conditions
     
  6. John-Gray

    John-Gray

    Joined:
    Nov 27, 2012
    Posts:
    133
    This is really cool.

    Thanks for sharing!
     
  7. Snownebula

    Snownebula

    Joined:
    Nov 29, 2013
    Posts:
    174
    Assets/SimpleJSON/JSON Tests/UnityScript/Test_UnityScript.js(3,8): BCE0021: Namespace 'SimpleJSON' not found, maybe you forgot to add an assembly reference?
     
  8. alexisnlavergne

    alexisnlavergne

    Joined:
    Jan 28, 2020
    Posts:
    2
    Could it be because the website changed and now you need an account?
     
  9. alexisnlavergne

    alexisnlavergne

    Joined:
    Jan 28, 2020
    Posts:
    2
    And also, it seems to be deprecated :(
     
  10. RPKLeads

    RPKLeads

    Joined:
    Jan 4, 2017
    Posts:
    2
    Cant find the equal class method for Network.Connect, unity says to use Multiplayer or Network identity but no help by going through them. WWW as deprecated but UnityWebReq has good documentation. any help on Network.Connect ?
     
  11. NotSoLuckyDucky

    NotSoLuckyDucky

    Joined:
    Apr 16, 2018
    Posts:
    56
    No Way!! Good job! I would make your own weather system integration with it if I were you!
     
  12. Flofoof

    Flofoof

    Joined:
    Mar 2, 2020
    Posts:
    9
    Could you send me all diffrend variation you can get out of it?
    like a list of weather: rainy, foggy just like that, and all of the aviable countrys and citys?
     
  13. ahsen35813

    ahsen35813

    Joined:
    Sep 15, 2019
    Posts:
    9
    Doesn't seem to work for Unity 2021.1. I keep getting a ton of errors
     
    Last edited: Apr 20, 2021
  14. RogueWhiteWolf

    RogueWhiteWolf

    Joined:
    Sep 15, 2020
    Posts:
    2
    How would you get the image part working? I cannot get that to work. The shader part isn't working. Did anyone figure that part out?