Unity 实时天气获取,简单版
直接把代码放进程序就行,默认是上海的地址,可以根据城市编码修改地址。这个是单个的,也可根据IP,或者手动选择地址来获取。
代码片
using DG.Tweening;
using LitJson;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WeahterTest : MonoBehaviour
{
public string lbCity;//城市
public string lbTemperature;//温度
public string lbWeahter;//天气
private void Start()
{
StartCoroutine(GetW());
}
IEnumerator GetW()
{
WWW www = new WWW("http://www.weather.com.cn/data/cityinfo/101020100.html");
while (!www.isDone) { yield return www; }
if (www.text!=null)
{
JsonData jd = JsonMapper.ToObject(www.text);
JsonData jdInfo = jd["weatherinfo"];
lbCity = jdInfo["city"].ToString();
lbTemperature = jdInfo["temp1"].ToString() + "~" + jdInfo["temp2"].ToString();
lbWeahter = jdInfo["weather"].ToString();
//。。。。需要的其它信息自己看接口提取
string strImg1 = jdInfo["img1"].ToString();
Debug.Log("city:" + lbCity + "\n weather:" + lbWeahter+ "\n Temperature" + lbTemperature);
}
Debug.Log("++" + www.text);
}
}
笔记:
解析出来的json格式。