45 lines
1.2 KiB
C#
45 lines
1.2 KiB
C#
|
using System;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Net.Http;
|
||
|
using BITKit;
|
||
|
using BITKit.UX;
|
||
|
using Cysharp.Threading.Tasks;
|
||
|
using Newtonsoft.Json.Linq;
|
||
|
using UnityEngine;
|
||
|
using UnityEngine.UIElements;
|
||
|
|
||
|
public class UXWeather : MonoBehaviour
|
||
|
{
|
||
|
[SerializeReference,SubclassSelector] private ITicker ticker;
|
||
|
[SerializeReference,SubclassSelector] private IReference api;
|
||
|
private readonly HttpClient _httpClient=new();
|
||
|
|
||
|
[UXBindPath("temperature-label")] private Label _temperatureLabel;
|
||
|
private void OnEnable()
|
||
|
{
|
||
|
ticker.Add(OnTick);
|
||
|
}
|
||
|
private void OnDisable()
|
||
|
{
|
||
|
ticker.Remove(OnTick);
|
||
|
}
|
||
|
private void Start()
|
||
|
{
|
||
|
UXUtils.Inject(this);
|
||
|
OnTick(Time.deltaTime);
|
||
|
}
|
||
|
|
||
|
private async void OnTick(float obj)
|
||
|
{
|
||
|
var json =await _httpClient.GetStringAsync(api.Value);
|
||
|
if(destroyCancellationToken.IsCancellationRequested)return;
|
||
|
var jObject = JObject.Parse(json);
|
||
|
var temperature = jObject["lives"]![0]!["temperature"]!.ToObject<string>();
|
||
|
|
||
|
await UniTask.SwitchToMainThread();
|
||
|
if (destroyCancellationToken.IsCancellationRequested) return;
|
||
|
_temperatureLabel.text = $"{temperature}℃";
|
||
|
}
|
||
|
}
|