219 lines
6.3 KiB
C#
219 lines
6.3 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.EventSystems;
|
|
using TMPro;
|
|
using UnityEngine.Networking;
|
|
using Newtonsoft.Json;
|
|
using static Config;
|
|
|
|
|
|
public class Drainage_tip : MonoBehaviour, IPointerClickHandler
|
|
{
|
|
//相机跳转位置点
|
|
//public Transform cameraTarget;
|
|
private GameObject _mainCamera;
|
|
/// <summary>
|
|
/// 跟随 UI提示出现的位置
|
|
/// </summary>
|
|
public Transform m_FollowObj;
|
|
/// <summary>
|
|
/// 偏移
|
|
/// </summary>
|
|
public Vector3 m_Offset;
|
|
/// <summary>
|
|
/// 相机移动点
|
|
/// </summary>
|
|
//public Transform cameraTarget;
|
|
|
|
public TextMeshProUGUI biaoti_text;
|
|
public TextMeshProUGUI deep1_text;
|
|
public TextMeshProUGUI deep2_text;
|
|
//private TextMeshProUGUI biaoti_jilutext;
|
|
//弹出图片
|
|
public GameObject tanchuang;
|
|
private bool textUpdated = false;
|
|
|
|
public TextMeshProUGUI waterNameText1;
|
|
public TextMeshProUGUI waterStateText1;
|
|
public TextMeshProUGUI waterNameText2;
|
|
public TextMeshProUGUI waterStateText2;
|
|
/// <summary>
|
|
/// 开、关图片加载路径
|
|
/// </summary>
|
|
private string closePath = "waterClose";
|
|
private string openPath = "waterOpen";
|
|
private string offlinePath = "waterOffline";
|
|
|
|
|
|
public Image water1;
|
|
public Image water2;
|
|
private int WaterState1;
|
|
private int WaterState2;
|
|
//public TextMeshProUGUI t;
|
|
public string id;
|
|
/// <summary>
|
|
/// 后端接口
|
|
/// </summary>
|
|
private string apiUrl;
|
|
void Start()
|
|
{
|
|
_mainCamera = GameObject.FindWithTag("MainCamera");
|
|
|
|
apiUrl = IP + "?Id=" + id;
|
|
}
|
|
|
|
public void OnPointerClick(PointerEventData eventData)
|
|
{
|
|
// 获取点击图片的名称
|
|
string imageName = gameObject.name;
|
|
tanchuang.SetActive(true);
|
|
biaoti_text.text = imageName + "号排水点";
|
|
|
|
int name = int.Parse(imageName);
|
|
int name1 = name * 2 - 1;
|
|
int name2 = name * 2;
|
|
//deep1_text.text = name1.ToString() + "#深井泵";
|
|
//deep2_text.text = name2.ToString() + "#深井泵";
|
|
//deep1_text.text = int.Parse(imageName) * 2 - 1;
|
|
//deep2_text.text = imageName * 2;
|
|
|
|
//waterNameText1.text = "1" + "#深井泵";
|
|
//waterNameText2.text = "2" + "#深井泵";
|
|
//waterStateText1.text = "状态:<color=#DF5E5E>关</color>";
|
|
//waterStateText2.text = "状态:<color=#50E953>开</color>";
|
|
//waterStateText2.text = "状态:<color=#8CA5A6>离线</color>";
|
|
Debug.Log(apiUrl);
|
|
//t.text = apiUrl;
|
|
CallTheDrainage();
|
|
}
|
|
/// <summary>
|
|
/// 调用排水接口
|
|
/// </summary>
|
|
public void CallTheDrainage()
|
|
{
|
|
StartCoroutine(GetDataFromAPI());
|
|
}
|
|
|
|
IEnumerator GetDataFromAPI()
|
|
{
|
|
using (UnityWebRequest webRequest = UnityWebRequest.Get(apiUrl))
|
|
{
|
|
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);
|
|
Test(jsonResponse);
|
|
|
|
}
|
|
}
|
|
}
|
|
public void Test(string jsonData)
|
|
{
|
|
// 加载图片资源
|
|
Sprite close = Resources.Load<Sprite>(closePath);
|
|
Sprite open = Resources.Load<Sprite>(openPath);
|
|
Sprite offline = Resources.Load<Sprite>(offlinePath);
|
|
|
|
var temp= JsonConvert.DeserializeObject<RootObject>(jsonData);
|
|
if (temp.msg=="成功")
|
|
{
|
|
temp.data.ForEach(x => Debug.Log(x.WaterName + x.WaterState));
|
|
for (int i = 0; i < temp.data.Count; i++)
|
|
{
|
|
if (i == 0)
|
|
{
|
|
waterNameText1.text = temp.data[i].WaterName /*+ "#深井泵"*/;
|
|
WaterState1 = temp.data[i].WaterState;
|
|
if (WaterState1==-1)//-1离线
|
|
{
|
|
waterStateText1.text = "状态:<color=#8CA5A6>离线</color>";
|
|
water1.sprite = offline;
|
|
}
|
|
else if (WaterState1 == 0)//0开
|
|
{
|
|
waterStateText1.text = "状态:<color=#50E953>开</color>";
|
|
water1.sprite = open;
|
|
}
|
|
else if (WaterState1 == 1)//1关
|
|
{
|
|
waterStateText1.text = "状态:<color=#DF5E5E>关</color>";
|
|
water1.sprite = close;
|
|
}
|
|
}
|
|
else if (i == 1)
|
|
{
|
|
waterNameText2.text = temp.data[i].WaterName /*+ "#深井泵"*/;
|
|
WaterState2 = temp.data[i].WaterState;
|
|
if (WaterState2 == -1)//-1离线
|
|
{
|
|
waterStateText2.text = "状态:<color=#8CA5A6>离线</color>";
|
|
water2.sprite = offline;
|
|
}
|
|
else if (WaterState2 == 0)//0开
|
|
{
|
|
waterStateText2.text = "状态:<color=#50E953>开</color>";
|
|
water2.sprite = open;
|
|
}
|
|
else if (WaterState2 == 1)//1关
|
|
{
|
|
waterStateText2.text = "状态:<color=#DF5E5E>关</color>";
|
|
water2.sprite = close;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public class WaterData
|
|
{
|
|
public string WaterName { get; set; }
|
|
public int WaterState { get; set; }
|
|
}
|
|
|
|
public class RootObject
|
|
{
|
|
|
|
public int code { get; set; }
|
|
|
|
public string msg { get; set; }
|
|
|
|
public List<WaterData> data { get; set; }
|
|
}
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
if (m_FollowObj != null)
|
|
{
|
|
Vector3 screenPos = Camera.main.WorldToScreenPoint(m_FollowObj.position);
|
|
if (screenPos.z > 0)
|
|
{
|
|
screenPos.z = 0;
|
|
transform.position = screenPos + m_Offset;
|
|
|
|
}
|
|
else
|
|
{
|
|
transform.position = new Vector3(8888, 8888, 8888);
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
}
|