using DG.Tweening; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking; using UnityEngine.UI; using SimpleJSON; using static GameManager; using Newtonsoft.Json; public class WebInteraction : MonoBehaviour { public static WebInteraction Inst; public string current_videoNumber = null; /// /// 视频监控是否为打开状态 /// public bool isVideoPlay = false; /// /// 工作票是否为打开状态 /// public bool isWorkPlay = false; public GameManager gameManager; private void Awake() { Inst = this; //StartCoroutine(QiangZhi()); } private void Start() { bt.onClick.AddListener(() => { //gameManager.token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2OTI2NDMyODksInVzZXJuYW1lIjoiMTgzMTIzNDU2NzgifQ.uMoC7I4SytLEgYTKHxyvOv7Lygeb9zDCmhLDYv1cJrA"; //gameManager.enabled = true; }); } //前端调用Unity 方法逻辑 //参数1 脚本所挂载的物体名 GameManager //参数2 脚本内方法名 unity_token_value //参数3 传递的参数 token值 /// /// 前端调用此方法传递Token /// /// public void unity_token_value(string jsonData) { try { JSONNode dataNode = JSON.Parse(jsonData); gameManager.arguments.token = dataNode["data"]["token"].Value; gameManager.arguments.heatThresholdValueMax = dataNode["data"]["heatThresholdValueMax"].Value; gameManager.arguments.heatThresholdValueMin = dataNode["data"]["heatThresholdValueMin"].Value; gameManager.arguments.humidityThresholdValueMax = dataNode["data"]["humidityThresholdValueMax"].Value; gameManager.arguments.humidityThresholdValueMin = dataNode["data"]["humidityThresholdValueMin"].Value; Debug.Log("温度最大阈值 " + gameManager.arguments.heatThresholdValueMax); Debug.Log("温度最小阈值 " + gameManager.arguments.heatThresholdValueMin); Debug.Log("湿度最大阈值 " + gameManager.arguments.humidityThresholdValueMax); Debug.Log("湿度最小阈值 " + gameManager.arguments.humidityThresholdValueMin); } catch (Exception) { Debug.Log("解析Token错误"); } //yield return null; StartCoroutine(GameManager.Inst.LoadAddress((ct) => { if (ct != null) { GameManager.Inst.Jk_URL = new webURL(ct); //Application.ExternalCall("OnSceneLoaded", "三维加载完成"); //Debug.Log("三维加载完成"); //return; StartCoroutine(GameManager.Inst.Initialize((x) => { if (!string.IsNullOrEmpty(x)) { #region 循环读取信息 if (!gameManager.isgonging) { StartCoroutine( CombineJSON.GetJson_POST(GameManager.Inst.Jk_URL.gj_cx, GameManager.Inst.arguments.token, 1800, (jsonResult3) => { if (!string.IsNullOrEmpty(jsonResult3)) { try { gameManager.root_AllAlarm = JsonConvert.DeserializeObject(jsonResult3); Array.ForEach(GameObject.FindObjectsOfType(), itme => { itme.tmsAlarmLists.Clear(); }); gameManager.tmsAlarms.Clear(); gameManager.Load_TmsAlarm(); } catch (Exception e) { Debug.Log("序列化-循环告警查询错误:" + e.Message); } } })); StartCoroutine( CombineJSON.GetJson_POST(GameManager.Inst.Jk_URL.hj_cx, GameManager.Inst.arguments.token, 1800, (jsonResult2) => { if (!string.IsNullOrEmpty(jsonResult2)) { try { gameManager.root_AllENV = JsonConvert.DeserializeObject(jsonResult2); gameManager.ENVs.Clear(); gameManager.Load_ENV(); } catch (Exception e) { Debug.Log("序列化-循环环境查询错误:" + e.Message); } } })); } #endregion gameManager.isgonging = true; } else { SecondConfirmPanel.DeleteConform(null, "进入场景失败"); Debug.Log("接口获取数据失败(初始化失败)"); gameManager.LoadScene(); } })); } else { SecondConfirmPanel.DeleteConform(null, "接口地址错误"); Debug.Log("获取穿透错误!"); } })); } /// /// 当前有画面的摄像头编号 /// /// public void unity_video_state(string videoNumber) { if (!string.IsNullOrEmpty(videoNumber)) { var _videoNumbers = videoNumber.Split(','); for (int i = 0; i < PatternChoose.Inst.uiToCabinet.clickEventLens.Count; i++) { for (int j = 0; j < _videoNumbers.Length; j++) { if (_videoNumbers[j] == PatternChoose.Inst.uiToCabinet.clickEventLens[i].text.text.Replace("摄像", "")) { PatternChoose.Inst.uiToCabinet.clickEventLens[i].isplay = true; PatternChoose.Inst.uiToCabinet.clickEventLens[i].ChangeMaterialColor(PatternChoose.Inst.uiToCabinet.clickEventLens[i].gameObject, 4); break; } else { PatternChoose.Inst.uiToCabinet.clickEventLens[i].isplay = false; PatternChoose.Inst.uiToCabinet.clickEventLens[i].ChangeMaterialColor(PatternChoose.Inst.uiToCabinet.clickEventLens[i].gameObject, 99); } } } } else { for (int i = 0; i < PatternChoose.Inst.uiToCabinet.clickEventLens.Count; i++) { PatternChoose.Inst.uiToCabinet.clickEventLens[i].isplay = false; PatternChoose.Inst.uiToCabinet.clickEventLens[i].ChangeMaterialColor(PatternChoose.Inst.uiToCabinet.clickEventLens[i].gameObject, 99); } } } public Button bt; public IEnumerator QiangZhi() { string filePath = Application.streamingAssetsPath + "/tk.txt"; UnityWebRequest www = UnityWebRequest.Get(filePath); yield return www.SendWebRequest(); if (www.result == UnityWebRequest.Result.Success) { string fileContent = www.downloadHandler.text; if (string.IsNullOrEmpty(fileContent)) yield break; gameManager.arguments.token = fileContent; } } /// /// 发送视频监控坐标的函数 /// /// /// public void SendVideoPosition(float x, float y) { // 在此处将视频监控坐标发送给前端 Debug.Log("Sending video position: (" + x + ", " + y + ")"); Application.ExternalCall("web_video_position", x + "," + y); } /// /// 关闭视频监控的函数 /// /// public void CloseVideo(string videoNumber) { // 在此处处理关闭视频监控的逻辑 Debug.Log("Closing video number: " + videoNumber); isVideoPlay = false; Application.ExternalCall("web_video_close", videoNumber); } /// /// 打开视频监控的函数 /// /// public void OpenVideo(string videoNumber) { // 在此处处理打开视频监控的逻辑 Debug.Log("Opening video number: " + videoNumber); Application.ExternalCall("web_video_open", videoNumber); } /// /// 打开现场作业的函数 /// public void OpenTicket() { // 在此处处理打开现场作业的逻辑 Debug.Log("Opening ticket"); Application.ExternalCall("web_ticket_open", ""); } /// /// 关闭现场作业的函数 /// public void CloseTicket() { // 在此处处理关闭现场作业的逻辑 Debug.Log("Closing ticket"); Application.ExternalCall("web_ticket_close", ""); //foreach (var item in GameManager.Inst.Cabinets_go) //{ // item.transform.Find("高亮").gameObject.SetActive(false); //} for (int i = 0; i < CabinetUIManager.Instance.cabine_hights.Count; i++) { CabinetUIManager.Instance.cabine_hights[i].SetActive(false); } } [ContextMenu("工作票设备关联的函数")] /// /// 工作票设备关联机柜变色 /// /// public void unity_ticket_cabinet(string deviceNumbers) { var devices = deviceNumbers.Split(','); List ls = new List(); ls.AddRange(GameManager.Inst.Cabinets_go); ls.AddRange(GameManager.Inst.Racks_go); for (int i = 0; i < ls.Count; i++) { for (int j = 0; j < devices.Length; j++) { if (devices[j] == ls[i].GetComponent().deviceList.id) { var Cabinet = gameManager.FindParent(ls[i].gameObject, gameManager.IsDesiredParent); if (Cabinet) { var hight = Cabinet.transform.Find("高亮").gameObject; changC(hight, "1"); } } } } } /// /// 工作票设备关联机柜镜头聚焦 /// /// public void unity_ticket_locate_cabinet(string deviceNumbers) { List ls = new List(); ls.AddRange(GameManager.Inst.Cabinets_go); for (int i = 0; i < ls.Count; i++) { if (ls[i].GetComponent().deviceList.id == deviceNumbers) { ls[i].GetComponent().Change_hide(ls[i], true, true); break; } } } public void changC(GameObject hight, string index) { hight.SetActive(true); Renderer renderer = hight.GetComponent(); Material[] sharedMaterials = renderer.sharedMaterials; //for (int i = 0; i < sharedMaterials.Length; i++) //{ // sharedMaterials[i] = newMaterial; //} sharedMaterials[0] = Resources.Load("Materials/" + index + "Tou"); renderer.sharedMaterials = sharedMaterials; } } public static class BoolMonitor { private static bool _value = false; /// /// 镜头是否为不动状态 /// public static bool Value { get { return _value; } set { if (_value != value) { _value = value; OnValueChanged(); } } } public static event EventHandler ValueChanged; static void OnValueChanged() { ValueChanged?.Invoke(null, EventArgs.Empty); } }