Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. Want to see the most recent patch releases? Take a peek at the patch release page.
    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.
     
  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:
    22
    Compliments mate (y)
    Saved a lot of time.
     
  5. Vert

    Vert

    Joined:
    Mar 23, 2010
    Posts:
    1,090
    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:
    167
    Assets/SimpleJSON/JSON Tests/UnityScript/Test_UnityScript.js(3,8): BCE0021: Namespace 'SimpleJSON' not found, maybe you forgot to add an assembly reference?