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; /// /// 跟随 UI提示出现的位置 /// public Transform m_FollowObj; /// /// 偏移 /// public Vector3 m_Offset; /// /// 相机移动点 /// //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; /// /// 开、关图片加载路径 /// 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; /// /// 后端接口 /// 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 = "状态:关"; //waterStateText2.text = "状态:开"; //waterStateText2.text = "状态:离线"; Debug.Log(apiUrl); //t.text = apiUrl; CallTheDrainage(); } /// /// 调用排水接口 /// 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(closePath); Sprite open = Resources.Load(openPath); Sprite offline = Resources.Load(offlinePath); var temp= JsonConvert.DeserializeObject(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 = "状态:离线"; water1.sprite = offline; } else if (WaterState1 == 0)//0开 { waterStateText1.text = "状态:开"; water1.sprite = open; } else if (WaterState1 == 1)//1关 { waterStateText1.text = "状态:关"; water1.sprite = close; } } else if (i == 1) { waterNameText2.text = temp.data[i].WaterName /*+ "#深井泵"*/; WaterState2 = temp.data[i].WaterState; if (WaterState2 == -1)//-1离线 { waterStateText2.text = "状态:离线"; water2.sprite = offline; } else if (WaterState2 == 0)//0开 { waterStateText2.text = "状态:开"; water2.sprite = open; } else if (WaterState2 == 1)//1关 { waterStateText2.text = "状态:关"; 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 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); } } } }