using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using LitJson; using System.IO; using Cysharp.Threading.Tasks; using DefaultNamespace.ProcessMode; using Framework.Manager; public static class InterfaceManager { /// /// 查找物体 /// /// /// public static GameObject FindHiddenPanel(string panelName) { // 遍历场景中的所有物体 GameObject[] allObjects = GameObject.FindObjectsOfType(true); // 第二个参数设置为true,表示查找包括隐藏的物体 foreach (GameObject obj in allObjects) { if (obj.name == panelName) { return obj; // 找到物体 } } return null; // 没有找到 } /// /// 引导执行下一步 /// public static UniTask LoadTriggerNextGuide(string panelName="") { if (MotionFramework.MotionEngine.GetModule()._currentMode == ProcessMode.教学模式 && TutorialGuideManager.Instance|| MotionFramework.MotionEngine.GetModule()._currentMode == ProcessMode.课程预览 && TutorialGuideManager.Instance) { return TutorialGuideManager.Instance.TriggerNextGuide(panelName); } return UniTask.FromResult(false); } // /// // /// 关闭引导遮罩 // /// // public static void LoadHideGuide() // { // if (MotionFramework.MotionEngine.GetModule()._currentMode == ProcessMode.教学模式 && TutorialGuideManager.Instance|| // MotionFramework.MotionEngine.GetModule()._currentMode == ProcessMode.课程预览 && TutorialGuideManager.Instance) // { // TutorialGuideManager.Instance.HideGuide(); // } // } /// /// 查找Resources /// /// /// public static GameObject LoadPrefabFromResources(string path,Transform parent) { if (MotionFramework.MotionEngine.GetModule()._currentMode == ProcessMode.教学模式|| MotionFramework.MotionEngine.GetModule()._currentMode == ProcessMode.课程预览 ) { GameObject prefab = Resources.Load(path); if (prefab != null) { GameObject obj = GameObject.Instantiate(prefab, parent); obj.SetActive(false); return obj; } else { Debug.LogWarning("无法在路径 " + path + " 处找到预制体。"); return null; } } return null; } // 根据物体名称查找并返回对应类型的物体 public static T FindObjectByName(string objectName) where T : Component { // 遍历场景中的所有物体 GameObject[] allObjects = GameObject.FindObjectsOfType(true); // 第二个参数设置为true,表示查找包括隐藏的物体 foreach (GameObject obj in allObjects) { if (obj.name == objectName) { T component = obj.GetComponent(); // 如果该物体有该组件,则返回 if (component != null) { return component; } else { Debug.LogWarning("物体 " + objectName + " 找不到指定类型的组件: " + typeof(T)); } } } return null; // 如果没有找到组件或物体,返回null } #region /// /// 读取数据 PC端 /// /// /// /// /// public static T LoadData(string fileName, JsonType type = JsonType.LitJson) where T : new() { //路径 string path = Application.streamingAssetsPath + "/" + fileName + ".json"; if (!File.Exists(path)) { path = Application.persistentDataPath + "/" + fileName + ".json"; } if (!File.Exists(path)) { return new T(); } //获取json字符串 string jsonStr = File.ReadAllText(path); // T data = new T(); //反序列 switch (type) { case JsonType.LitJson: data = JsonMapper.ToObject(jsonStr); break; case JsonType.JsonUtility: data = JsonUtility.FromJson(jsonStr); break; } return data; } #endregion public static string GetLocalTxt(string path) { using (System.IO.StreamReader reader = new System.IO.StreamReader(path)) { return reader.ReadToEnd(); } } public static IEnumerator GetBytes(string url, Action callback) { using (UnityWebRequest www = UnityWebRequest.Get(url)) { yield return www.SendWebRequest(); switch (www.result) { case UnityWebRequest.Result.InProgress: break; case UnityWebRequest.Result.Success: callback?.Invoke(www.downloadHandler.data); break; case UnityWebRequest.Result.ConnectionError: break; case UnityWebRequest.Result.ProtocolError: break; case UnityWebRequest.Result.DataProcessingError: break; default: break; } } } public static IEnumerator GetString(string url, Action callback) { using (UnityWebRequest www = UnityWebRequest.Get(url)) { yield return www.SendWebRequest(); switch (www.result) { case UnityWebRequest.Result.InProgress: Debug.LogError("UnityWebRequest.Result.InProgress"); break; case UnityWebRequest.Result.Success: callback?.Invoke(www.downloadHandler.text); break; case UnityWebRequest.Result.ConnectionError: Debug.LogError("UnityWebRequest.Result.ConnectionError"); break; case UnityWebRequest.Result.ProtocolError: Debug.LogError("UnityWebRequest.Result.ProtocolError"); break; case UnityWebRequest.Result.DataProcessingError: Debug.LogError("UnityWebRequest.Result.DataProcessingError"); break; default: break; } } } public static IEnumerator GetString(string url, int time, Action callback) { while (true) { using (UnityWebRequest www = UnityWebRequest.Get(url)) { yield return new WaitForSeconds(time); // 每隔 time 秒调用一次接口 yield return www.SendWebRequest(); switch (www.result) { case UnityWebRequest.Result.InProgress: break; case UnityWebRequest.Result.Success: callback?.Invoke(www.downloadHandler.text); break; case UnityWebRequest.Result.ConnectionError: break; case UnityWebRequest.Result.ProtocolError: break; case UnityWebRequest.Result.DataProcessingError: break; default: break; } } } } public static IEnumerator GetString(string url, int time, Dictionary header, Action callback) { while (true) { using (UnityWebRequest www = UnityWebRequest.Get(url)) { yield return new WaitForSeconds(time); // 每隔 time 秒调用一次接口 foreach (var item in header) { www.SetRequestHeader(item.Key, item.Value); } yield return www.SendWebRequest(); switch (www.result) { case UnityWebRequest.Result.InProgress: break; case UnityWebRequest.Result.Success: callback?.Invoke(www.downloadHandler.text); break; case UnityWebRequest.Result.ConnectionError: break; case UnityWebRequest.Result.ProtocolError: break; case UnityWebRequest.Result.DataProcessingError: break; default: break; } } } } public static IEnumerator GetJsonString(string url, Action callback = null, string success_code = "200") { using (UnityWebRequest www = UnityWebRequest.Get(url)) { yield return www.SendWebRequest(); switch (www.result) { case UnityWebRequest.Result.InProgress: break; case UnityWebRequest.Result.Success: var Jobject = JObject.Parse(www.downloadHandler.text); if (Jobject["code"].ToString() == success_code) { callback?.Invoke(JsonConvert.DeserializeObject(Jobject["data"].ToString())); } break; case UnityWebRequest.Result.ConnectionError: break; case UnityWebRequest.Result.ProtocolError: break; case UnityWebRequest.Result.DataProcessingError: break; default: break; } } } public static IEnumerator GetJsonString(string url, Dictionary header, Action callback = null, string success_code = "200") { using (UnityWebRequest www = UnityWebRequest.Get(url)) { foreach (var item in header) { www.SetRequestHeader(item.Key, item.Value); } yield return www.SendWebRequest(); switch (www.result) { case UnityWebRequest.Result.InProgress: break; case UnityWebRequest.Result.Success: //Debug.Log("根据标识返回人员信息...:" + www.downloadHandler.text); var Jobject = JObject.Parse(www.downloadHandler.text); if (Jobject["code"].ToString() == success_code) { callback?.Invoke(JsonConvert.DeserializeObject(Jobject["data"].ToString())); } break; case UnityWebRequest.Result.ConnectionError: break; case UnityWebRequest.Result.ProtocolError: break; case UnityWebRequest.Result.DataProcessingError: break; default: break; } } } public static IEnumerator PostJsonString(string url, Action callback = null, string success_code = "200") { using (UnityWebRequest www = UnityWebRequest.Get(url)) { yield return www.SendWebRequest(); switch (www.result) { case UnityWebRequest.Result.InProgress: break; case UnityWebRequest.Result.Success: var Jobject = JObject.Parse(www.downloadHandler.text); if (Jobject["code"].ToString() == success_code) { callback?.Invoke(JsonConvert.DeserializeObject(Jobject["data"].ToString())); } break; case UnityWebRequest.Result.ConnectionError: break; case UnityWebRequest.Result.ProtocolError: break; case UnityWebRequest.Result.DataProcessingError: break; default: break; } } } public static IEnumerator PostJsonString(string url, WWWForm formdata, Dictionary headers = null, Action callback = null, string success_code = "200") { using (UnityWebRequest www = UnityWebRequest.Post(url, formdata)) { foreach (var item in headers) { www.SetRequestHeader(item.Key, item.Value); } yield return www.SendWebRequest(); switch (www.result) { case UnityWebRequest.Result.InProgress: break; case UnityWebRequest.Result.Success: var Jobject = JObject.Parse(www.downloadHandler.text); if (Jobject["code"].ToString() == success_code) { callback?.Invoke(JsonConvert.DeserializeObject(Jobject["data"].ToString())); } break; case UnityWebRequest.Result.ConnectionError: break; case UnityWebRequest.Result.ProtocolError: break; case UnityWebRequest.Result.DataProcessingError: break; default: break; } } } public static IEnumerator PostString(string url, WWWForm formdata, Dictionary headers = null, Action callback = null) { using (UnityWebRequest www = UnityWebRequest.Post(url, formdata)) { foreach (var item in headers) { www.SetRequestHeader(item.Key, item.Value); } yield return www.SendWebRequest(); switch (www.result) { case UnityWebRequest.Result.InProgress: break; case UnityWebRequest.Result.Success: callback?.Invoke(www.downloadHandler.text); break; case UnityWebRequest.Result.ConnectionError: Debug.Log(www.result); break; case UnityWebRequest.Result.ProtocolError: Debug.Log(www.result); break; case UnityWebRequest.Result.DataProcessingError: Debug.Log(www.result); break; default: break; } } } public static IEnumerator PostString(string url, WWWForm formdata, Dictionary headers = null, string jsonBody = null, Action callback = null) { using (UnityWebRequest www = UnityWebRequest.Post(url, formdata)) { foreach (var item in headers) { www.SetRequestHeader(item.Key, item.Value); } byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(jsonBody); www.uploadHandler = new UploadHandlerRaw(jsonToSend); // 设置 Content-Type 为 application/json www.SetRequestHeader("Content-Type", "application/json"); yield return www.SendWebRequest(); switch (www.result) { case UnityWebRequest.Result.InProgress: break; case UnityWebRequest.Result.Success: callback?.Invoke(www.downloadHandler.text); break; case UnityWebRequest.Result.ConnectionError: Debug.Log(www.result); break; case UnityWebRequest.Result.ProtocolError: Debug.Log(www.result); break; case UnityWebRequest.Result.DataProcessingError: Debug.Log(www.result); break; default: break; } } } public static IEnumerator GetString(string url, Dictionary headers = null, int time = 50, Action callback = null) { while (true) { using (UnityWebRequest www = UnityWebRequest.Get(url)) { yield return new WaitForSeconds(time); // 每隔 time 秒调用一次接口 foreach (var item in headers) { www.SetRequestHeader(item.Key, item.Value); } yield return www.SendWebRequest(); switch (www.result) { case UnityWebRequest.Result.InProgress: break; case UnityWebRequest.Result.Success: callback?.Invoke(www.downloadHandler.text); break; case UnityWebRequest.Result.ConnectionError: break; case UnityWebRequest.Result.ProtocolError: break; case UnityWebRequest.Result.DataProcessingError: break; default: break; } } } } public static IEnumerator GetString(string url, Dictionary headers = null, Action callback = null) { using (UnityWebRequest www = UnityWebRequest.Get(url)) { foreach (var item in headers) { www.SetRequestHeader(item.Key, item.Value); } yield return www.SendWebRequest(); switch (www.result) { case UnityWebRequest.Result.InProgress: break; case UnityWebRequest.Result.Success: callback?.Invoke(www.downloadHandler.text); break; case UnityWebRequest.Result.ConnectionError: break; case UnityWebRequest.Result.ProtocolError: break; case UnityWebRequest.Result.DataProcessingError: break; default: break; } } } public static IEnumerator GetString(string url, Dictionary headers = null, string jsonBody = null, Action callback = null) { using (UnityWebRequest www = UnityWebRequest.Get(url)) { foreach (var item in headers) { www.SetRequestHeader(item.Key, item.Value); } // 将请求体设置为原始JSON数据 byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(jsonBody); www.uploadHandler = new UploadHandlerRaw(jsonToSend); yield return www.SendWebRequest(); switch (www.result) { case UnityWebRequest.Result.InProgress: break; case UnityWebRequest.Result.Success: callback?.Invoke(www.downloadHandler.text); break; case UnityWebRequest.Result.ConnectionError: break; case UnityWebRequest.Result.ProtocolError: break; case UnityWebRequest.Result.DataProcessingError: break; default: break; } } } }