True Weather - Unity Asset Store - Nephasto

'True Weather' gives you information about the weather conditions and the geolocation of the users of your games.

🕹️ Demo  🛒 Store

Setting

The first thing you should do is create a new Game Object in a scene and add the component ‘True Weather’ to it. There should only be a single instance of the component in your scene.

Once added you will see something like this:

 

In ‘Units’ (1) you can select the metric system you want to use: Metric (International System of Units or SI) or Imperial.

If ‘Try GPS first’ (2) is active, the geolocation service will try to use GPS hardware. This is normally only possible on mobile devices and if the application has the permissions to use it. If available, this mode can save battery life.

Only the GPS is activated during the query and the mode with less precision (~500 meters) is used.

If this option is not active, or is not available, the web service ipap.co will be used. This service does not require registration to use it for less than 1000 consultations per day. Anyway, I recommend you register when your game is public. Once you register, they will provide you with a key that you must put in ‘API Key’ (3).

To obtain meteorological data, you can use the web services of openweathermap.com (basic data) or climacell.co (more data). Select the one you want to use in ‘Service’ (4).

Both services require web registration, even for free use. In the case of openweathermap.com you need to sign up for their ‘Current weather’ service. If you choose climacell.co you must sign up for their ‘Weather API’ service. The key that they provide you, you must put in ‘API Key’ (5).

Do you want to use another service? Here you have some of the best current services, tell me which one you want to use and I will include it in an update.

Use

All the code is inside the namespace ‘Nephasto.TrueWeatherAsset’ and the main component is ‘TrueWeather’. The first thing you should do is import the namespace:

1
 using Nephasto.TrueWeatherAsset;

 

If you have already created a Game Object with the component ‘TrueWeather’ in the Editor, you can access it like this:

1
 TrueWeather trueWeather = FindObjectOfType<TrueWeather>();

 

To detect the current time you must call the function ‘Detect()’. As the information may take a few seconds to be available, you must use an Action as a parameter that will be executed when the query is finished. For example, if you want to get a message through the console with the temperature it would be something like this:

1
2
3
4
5
6
7
 trueWeather.Detect(WeatherRequests.Meteorology, (success, errorMsg) =>
 {
   if (success == true)
     Debug.Log($"Temperature: {trueWeather.Weather.Temperature} {trueWeather.Units == Units.Metric ? "C" : "F"}.");
   else
     Debug.LogError($"Error: {errorMsg}.");
 });

 

Once the Action is called without errors, you can access the geolocation with the ‘Location’ property and the weather information with ‘Weather':

1
2
3
4
5
6
7
8
9
 float latitude = trueWeather.Location.Latitude;
 float longitude = trueWeather.Location.Longitude;

 string description = trueWeather.Weather.Description;
 float temperature = trueWeather.Weather.Temperature;
 float pressure = trueWeather.Weather.Presure;
 float humifity = trueWeather.Weather.Humidity;
 DateTime sunrise = trueWeather.Weather.Sunrise;
 DateTime sunset = trueWeather.Weather.Sunset;

 

All this information will be available until the next time you call ‘Detect()’. To find out all the data you have available, you can consult the files ‘Runtime/ILocation.cs’ (geolocation) and ‘Runtime/WeatherService.cs’ (meteorology).

Some services provide extra information that you can know by casting the generic data to the service used. If you use the service ipap.co for geolocation you can access the extra data like this:

1
2
3
4
5
6
7
8
9
 if (trueWeather.Location is IPAPILocation)
 {
   IPAPILocation ipAPILocation = (IPAPILocation)trueWeather.Location;

   string ip = ipAPILocation.IPAdress;
   string country = ipAPILocation.Country;
   string region = ipAPILocation.Region;
   string city = ipAPILocation.City;
 }

 

Check all the extra data available by looking at the file ‘Runtime/IPAPI/IPAPILocation.cs’.

When you use the service openweathermap.com you can access its extra data like this:

1
2
3
4
5
6
7
 if (trueWeather.Weather is OpenWeatherMapService)
 {
   OpenWeatherMapService openWeatherMap = (OpenWeatherMapService)trueWeather.Weather;

   float tempMin = openWeatherMap.TemperatureMin;
   float tempMax = openWeatherMap.TemperatureMax;
 }

 

Check all the extra data available in the file ‘Runtime/OpenWeatherMap/OpenWeatherMapService.cs’.

If you use the service climacell.co, you can do it like this:

1
2
3
4
5
6
7
8
 if (trueWeather.Weather is ClimacellService)
 {
   ClimacellService climacell = (ClimacellService)trueWeather.Weather;

   MoonPhases moonPhase = climacell.MoonPhase;
   float visibility = climacell.Visibility;
   float solarRadiation = climacell.SolarRadiation;
 }

 

You can also access air quality data (not available using openweathermap.com), like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
 trueWeather.Detect(WeatherRequests.AirQuality, (success, errorMsg) =>
 {
   if (success == true)
   {
     if (trueWeather.Weather is ClimacellService)
     {
       ClimacellService climacell = (ClimacellService)trueWeather.Weather;

       Debug.Log($"Air quality : {climacell.AirQuality} ppb.");
     }
   }
   else
     Debug.LogError($"Error: {errorMsg}");
 });

 

Check all the extra data available by looking at the file ‘Runtime/Climacell/ClimacellService.cs’.

Any questions or suggestions you have, I will be happy to answer you in

hello@nephasto.com