Skip to content

Instantly share code, notes, and snippets.

@iShengP
Created November 7, 2019 06:41
Show Gist options
  • Save iShengP/1fdab98b43b6561c2b60916c6dbbe3b6 to your computer and use it in GitHub Desktop.
Save iShengP/1fdab98b43b6561c2b60916c6dbbe3b6 to your computer and use it in GitHub Desktop.
Unity OpenWeatherMap 範例
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using SimpleJSON;
using UnityEditor;
using UnityEngine.Serialization;
using UnityEngine.UI;
public class whether : MonoBehaviour
{
public int conditionID; // 天氣狀態 ID
public string conditionImage; // 天氣狀態圖片
public string conditionName; // 天氣狀態
public string currentCity; // 目前城市
public string currentZIPcode; // 目前郵遞區號
public string currentCoordLatitude; // 目前經度
public string currentCoordLongitude; // 目前緯度
public string whetherLanguage; // 天氣語言
public string appID; // appID
public bool searchByCity, searchByZIPcode, searchByCoordinate; // 三個 checkbox 讓使用者選擇要怎麼搜尋(只可選一個)
public Image myWeatherCondition; // UI 天氣狀態
public Text myWeatherLabel; // UI 天氣標籤
public Text myWhetherTemp; // UI 溫度
public Text myHumidity; // UI 濕度
public string retrievedCity; // 回傳城市
public string retrievedCountry; // 回傳國家
public string retrievedHumidity; // 回傳濕度
void Start()
{
StartCoroutine(SendRequest());
}
IEnumerator SendRequest()
{
// 取得天氣
// 範例:http://api.openweathermap.org/data/2.5/weather?q=Tokyo&lang=zh_tw&appid=8b2ee97645e0e072af5872ee00835acb
WWW request;
// 透過『城市名』取得天氣
if (searchByCity == true && searchByCoordinate == false && searchByZIPcode == false)
{
request = new WWW("https://api.openweathermap.org/data/2.5/weather?q=" + currentCity +
"&lang=" + whetherLanguage + "&appid=" + appID );
yield return request;
}
// 透過『地理座標』取得天氣
else if (searchByCity == false && searchByCoordinate == true && searchByZIPcode == false)
{
request = new WWW("https://api.openweathermap.org/data/2.5/weather?lat=" + currentCoordLatitude +
"&lon=" + currentCoordLongitude + "&lang=" + whetherLanguage + "&appid=" + appID );
yield return request;
}
// 透過『郵遞區號』取得天氣
else if (searchByCity == false && searchByCoordinate == false && searchByZIPcode == true)
{
request = new WWW("https://api.openweathermap.org/data/2.5/weather?zip=" + currentZIPcode +
",jp&lang=" + whetherLanguage + "&appid=" + appID );
yield return request;
}
// 其他狀況,預設為『城市名』取得天氣
else
{
request = new WWW("https://api.openweathermap.org/data/2.5/weather?q=" + currentCity +
"&lang=" + whetherLanguage + "&appid=" + appID );
yield return request;
}
if (string.IsNullOrEmpty(request.error))
{
var N = JSON.Parse(request.text);
retrievedCountry = N["sys"]["country"].Value; // 取得國家
retrievedCity = N["name"].Value; // 取得城市
retrievedHumidity = N["main"]["humidity"].Value;
currentCoordLatitude = N["coord"]["lat"].Value;
currentCoordLongitude = N["coord"]["lon"].Value;
string temp = N["main"]["temp"].Value; // 取得溫度
float tempTemp; // 變數,暫存解析中的溫度
float.TryParse(temp, out tempTemp); // 解析溫度
float finalTemp = Mathf.Round((tempTemp - 273.0f)*10)/10; // 儲存實際溫度
int.TryParse(N["weather"][0]["id"].Value, out conditionID); // 取得天氣狀態 ID
//conditionName = N["weather"][0]["main"].Value; // 取得天氣狀態名稱
conditionName = N["weather"][0]["description"].Value; // 取得天氣狀態敘述
conditionImage = N["weather"][0]["icon"].Value; // 取得天氣狀態圖案 ID
// 天氣狀態 ID 對照參考 https://openweathermap.org/weather-conditions
myHumidity.text = "濕度:" + retrievedHumidity + "%"; // UI 顯示濕度
myWhetherTemp.text = finalTemp + "℃"; // UI 顯示最終溫度
myWeatherLabel.text = conditionName; // UI 顯示天氣狀態
}
else
{
Debug.Log("WWW error: " + request.error);
}
// 取得天氣狀態圖案
WWW conditionRequest = new WWW("https://openweathermap.org/img/wn/" + conditionImage + "@2x.png");
yield return conditionRequest;
if (string.IsNullOrEmpty(conditionRequest.error))
{
var mainTexture = myWeatherCondition.mainTexture; // 建立 mainTexture 存放場景中圖案框
Texture2D tex; // 建立 Texture,存放下載後的圖片
tex = new Texture2D(mainTexture.width, mainTexture.height, TextureFormat.DXT1, false);
conditionRequest.LoadImageIntoTexture(tex); // 將下載後的圖片檔案轉換至貼圖
myWeatherCondition.sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0, 0));
Color c = myWeatherCondition.color;
c.a = 1;
myWeatherCondition.color = c;
}
else
{
Debug.Log("WWW error: " + conditionRequest.error);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment