내일배움캠프 26일차 TIL - OpenWeather API로 날씨 찍어보기
Сurrent weather and forecast - OpenWeatherMap
Access current weather data for any location on Earth including over 200,000 cities! The data is frequently updated based on the global and local weather models, satellites, radars and a vast network of weather stations. how to obtain APIs (subscriptions w
openweathermap.org
심심해서 날씨 한번 찍어보는거 만들어봤다 사용자 위치를 알아내는건 다른 API를 써야해서
일단 부산으로 하드코딩 해놨다
OpenWeather 웹사이트에 들어간후 회원가입을 하면 API Key를 발급 받을수있다
[System.Serializable]
public class OWM_Coord
{
public float lon;
public float lat;
}
[System.Serializable]
public class OWM_Weather
{
public int id;
public string main;
public string description;
public string icon;
}
[System.Serializable]
public class OWM_Main
{
public int temp;
public float feels_like;
public int temp_min;
public int temp_max;
public int pressure;
public int humidity;
}
[System.Serializable]
public class OWM_Wind
{
public float speed;
public int deg;
}
[System.Serializable]
public class OWM_Clouds
{
public int all;
}
[System.Serializable]
public class OWM_Sys
{
public int type;
public int id;
public string country;
public int sunrise;
public int sunset;
}
[System.Serializable]
public class WeatherData
{
public OWM_Coord coord;
public OWM_Weather[] weather;
public string basem;
public OWM_Main main;
public int visibility;
public OWM_Wind wind;
public OWM_Clouds clouds;
public int dt;
public OWM_Sys sys;
public int timezone;
public int id;
public string name;
public int cod;
}
키를 발급 받고 아래 클래스를 작성한다
using UnityEngine;
using TMPro;
using Cysharp.Threading.Tasks;
public class Weather : MonoBehaviour
{
[SerializeField] Sprite[] WeatherIcons;
[SerializeField] TextMeshProUGUI UserLocation;
[SerializeField] ImageWithText[] Components;
string GetKorCityName(string _engStr)
{
if (_engStr == "Seoul")
{
return "서울";
}
else if (_engStr == "Busan")
{
return "부산";
}
else
{
return "Unknown location";
}
}
int GetWeatherNumber(string _engWeatherName)
{
if(_engWeatherName == "Clear")
{
return 0;
}
else if (_engWeatherName == "Clouds")
{
return 1;
}
else if(_engWeatherName == "Rain")
{
return 2;
}
else
{
return 0;
}
}
string GetWeatherKorName(string _engWeatherName)
{
if (_engWeatherName == "Clear")
{
return "맑음";
}
else if (_engWeatherName == "Clouds")
{
return "흐림";
}
else if (_engWeatherName == "Rain")
{
return "비";
}
else
{
return _engWeatherName;
}
}
private async void OnEnable()
{
UserLocation.text = "현재 위치 : " + GetKorCityName(Define.USER_LOCATION);
Core.Instance.Weather.SetWeather(Define.USER_LOCATION);
await UniTask.WaitUntil(() => Core.Instance.Weather.WeatherReady == true);
Components[0].Set
(WeatherIcons[GetWeatherNumber(Core.Instance.Weather.weatherInfo.weather[0].main)],
GetWeatherKorName(Core.Instance.Weather.weatherInfo.weather[0].main));
Components[1].Set("<size=60>현재 온도</size>\n" + Core.Instance.Weather.weatherInfo.main.temp +"도");
Components[2].Set("<size=60>강수 확률</size>\n" + Core.Instance.Weather.weatherInfo.clouds.all +"%");
}
}
해당 클래스를 사용하여 아래 비동기로 불러오면 된다
코드 더 간단하게 작성하기 위해 Unitask 라이브러리를 사용하였다
https://github.com/Cysharp/UniTask
GitHub - Cysharp/UniTask: Provides an efficient allocation free async/await integration for Unity.
Provides an efficient allocation free async/await integration for Unity. - GitHub - Cysharp/UniTask: Provides an efficient allocation free async/await integration for Unity.
github.com
대충 받아서 UI로 찍으면 이렇게 나온다 현재 비 오는중....