ChangDaoZhanGuan/ChangDaoPro/Assets/Scr/Cooler_tip.cs

185 lines
6.1 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using static Drainage_tip;
public class Cooler_tip : MonoBehaviour
{
public Image uiImage; // 引用UI图片
public Image maskImage;
private RectTransform imageRectTransform; // 图片的RectTransform
public string targetTag = "cooler";
public List<Button> btnList = new List<Button>();
public List<GameObject> cbtnList = new List<GameObject>();
public List<TextMeshProUGUI> textList = new List<TextMeshProUGUI>();
public List<TextMeshProUGUI> dwList = new List<TextMeshProUGUI>();
public Button xx;
private int showTarId = 0;
private KTDataMdoel temp = null;
private void Start()
{
// 获取图片的RectTransform组件
imageRectTransform = uiImage.GetComponent<RectTransform>();
// 初始时隐藏图片
uiImage.gameObject.SetActive(false);
maskImage.gameObject.SetActive(false);
for (int i = 0; i < btnList.Count; i++)
{
int index = i;
btnList[i].onClick.AddListener(() =>
{
showTarId = index;
showTar();
Debug.LogError(index);
});
}
showTar();
xx.onClick.AddListener(() =>
{
uiImage.gameObject.SetActive(false);
maskImage.gameObject.SetActive(false);
});
}
private void showTar()
{
for(int i = 0;i < btnList.Count;i++)
{
if(showTarId == i)
{
btnList[i].gameObject.SetActive(false);
cbtnList[i].gameObject.SetActive(true);
}
else
{
btnList[i].gameObject.SetActive(true);
cbtnList[i].gameObject.SetActive(false);
}
}
if(temp != null)
{
// public int UnitMode;
//public float OperatingTime;
//public float OutTemperature;
//public float ReturnTemperature;
//1=制热模式2=制冷模式
switch(temp.data[showTarId].UnitMode)
{
case 1:
textList[0].text = "制热模式";
break;
case 2:
textList[0].text = "制冷模式";
break;
}
textList[1].text = temp.data[showTarId].OperatingTime.ToString("F1");
textList[2].text = temp.data[showTarId].OutTemperature.ToString("F1");
textList[3].text = temp.data[showTarId].ReturnTemperature.ToString("F1");
dwList[1].transform.localPosition = new Vector3(textList[1].transform.localPosition.x + textList[1].preferredWidth + 4, dwList[1].transform.localPosition.y, 0);
dwList[2].transform.localPosition = new Vector3(textList[2].transform.localPosition.x + textList[2].preferredWidth + 4, dwList[2].transform.localPosition.y, 0);
dwList[3].transform.localPosition = new Vector3(textList[3].transform.localPosition.x + textList[3].preferredWidth + 4, dwList[3].transform.localPosition.y, 0);
}
}
private void Update()
{
// 检测鼠标点击或触摸屏点击
if (Input.GetMouseButtonDown(0))
{
// 射线检测,判断是否点击在模型上
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.CompareTag(targetTag))
{
// 如果点击到具有特定标签的模型,执行你的操作
Debug.Log("Clicked on object with tag: " + targetTag);
// 点击到模型
StartCoroutine(GetDataFromAPI_kt());
ShowImage(hit.point);
}
}
}
}
IEnumerator GetDataFromAPI_kt()
{
using (UnityWebRequest webRequest = UnityWebRequest.Get(Config.kongtiao))
{
yield return webRequest.SendWebRequest();
if (webRequest.result == UnityWebRequest.Result.ConnectionError ||
webRequest.result == UnityWebRequest.Result.ProtocolError)
{
Debug.Log("Error: " + webRequest.error);
}
else
{
string jsonResponse = webRequest.downloadHandler.text;
Debug.Log(jsonResponse);
temp = JsonConvert.DeserializeObject<KTDataMdoel>(jsonResponse);
if (temp.msg == "成功")
{
ShowImage(new Vector3());
//for (int i = 0; i < temp.data.Count; i++)
//{
// //Debug.LogError(temp.data[i].UnitName);
//}
}
//Test(jsonResponse);
//{"code":200,"msg":"成功","data":
//[{"UnitName":"空调机组1",
//"UnitMode":2,
//"OperatingTime":"15194.000",
//"OutTemperature":"28.100",
//"ReturnTemperature":"26.300"},
//{"UnitName":"空调机组2",
//"UnitMode":2,
//"OperatingTime":"15950.000",
//"OutTemperature":"27.700",
//"ReturnTemperature":"28.000"},
//{"UnitName":"空调机组3",
//"UnitMode":2,
//"OperatingTime":"11327.000",
//"OutTemperature":"26.700",
//"ReturnTemperature":"27.500"}]}
}
}
}
private void ShowImage(Vector3 position)
{
// 显示图片并设置位置
uiImage.gameObject.SetActive(true);
maskImage.gameObject.SetActive(true);
showTar();
//imageRectTransform.position = Camera.main.WorldToScreenPoint(position);
}
}
public class KTDataMdoel
{
public string msg;
public List<KTData> data = new List<KTData>();
}
public class KTData
{
public string UnitName;
public int UnitMode;
public float OperatingTime;
public float OutTemperature;
public float ReturnTemperature;
}