본문 바로가기
유니티/유니티 기능 구현

실시간 날짜 받기

by xortl98 2022. 6. 2.
728x90

 개요

실시간 날씨를 받고 싶어서 OpenWeather라는 사이트에서 OpenAPI를 받아온 뒤 각 나라 도시별 실시간 정보를 받아왔다.

 참고한 영상 

https://www.youtube.com/watch?v=G92bOA2WiO4 

 스크립트

더보기

파싱하는데 필요한 스크립트 

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[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 System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class Weather : MonoBehaviour
{
	//자신의 API키 입력 
    public string APP_ID;
    //날씨 정보 출력할 Text
    public Text weatherText;
    //아이콘 출력할 RawImage
    public RawImage weatherIcon;
    public WeatherData weatherInfo;

    [Header("도시 이름 입력")]
    public string weatherName;

    // Start is called before the first frame update
    void Start()
    {
        CheckCityWeather(weatherName);
    }

    public void CheckCityWeather(string city)
    {
        StartCoroutine(GetWeather(city));
    }

    IEnumerator GetWeather(string city)
    {
        city = UnityWebRequest.EscapeURL(city);
        string url = "http://api.openweathermap.org/data/2.5/weather?q="+city+"&units=metric&appid="+APP_ID;

        UnityWebRequest www = UnityWebRequest.Get(url);
        yield return www.SendWebRequest();

        string json = www.downloadHandler.text;
        json = json.Replace("\"base\":", "\"basem\":");
        weatherInfo = JsonUtility.FromJson<WeatherData>(json);

        //입력한 도시 이름 + 몇 도인지 소수점 올림
        weatherText.text = weatherInfo.name + "\n";
        weatherText.text += weatherInfo.main.temp.ToString("N1") + " °C \n";

        if (weatherInfo.weather.Length > 0)
        {
            //weatherText.text = weatherInfo.weather[0].main;
            weatherText.text += weatherInfo.weather[0].description;
            StartCoroutine(GetWeatherIcon(weatherInfo.weather[0].icon));
        }
    }

    //아이콘 텍스쳐를 다운 받아서 출력 
    IEnumerator GetWeatherIcon(string icon)
    {
        
        string url = "http://openweathermap.org/img/wn/" + icon + "@2x.png";
        UnityWebRequest www = UnityWebRequestTexture.GetTexture(url);
        yield return www.SendWebRequest();

        weatherIcon.gameObject.SetActive(true);
        weatherIcon.texture = DownloadHandlerTexture.GetContent(www);
    }
}

플레이 영상  

플레이 영상은 현재 새벽 3시 기준 서울의 날씨이다.

 

 

더보기

사용하실 때 API키 변경하셔야합니다.

RealTime_Weather.unitypackage
0.01MB