551 lines
20 KiB
C#
551 lines
20 KiB
C#
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
|
||
{
|
||
/// <summary>
|
||
/// 查找物体
|
||
/// </summary>
|
||
/// <param name="panelName"></param>
|
||
/// <returns></returns>
|
||
public static GameObject FindHiddenPanel(string panelName)
|
||
{
|
||
// 遍历场景中的所有物体
|
||
GameObject[] allObjects = GameObject.FindObjectsOfType<GameObject>(true); // 第二个参数设置为true,表示查找包括隐藏的物体
|
||
|
||
foreach (GameObject obj in allObjects)
|
||
{
|
||
if (obj.name == panelName)
|
||
{
|
||
return obj; // 找到物体
|
||
}
|
||
}
|
||
|
||
return null; // 没有找到
|
||
}
|
||
|
||
/// <summary>
|
||
/// 引导执行下一步
|
||
/// </summary>
|
||
public static UniTask<bool> LoadTriggerNextGuide(string panelName="")
|
||
{
|
||
if (MotionFramework.MotionEngine.GetModule<ProcessManager>()._currentMode == ProcessMode.教学模式 && TutorialGuideManager.Instance||
|
||
MotionFramework.MotionEngine.GetModule<ProcessManager>()._currentMode == ProcessMode.课程预览 && TutorialGuideManager.Instance)
|
||
{
|
||
return TutorialGuideManager.Instance.TriggerNextGuide(panelName);
|
||
}
|
||
return UniTask.FromResult(false);
|
||
}
|
||
|
||
// /// <summary>
|
||
// /// 关闭引导遮罩
|
||
// /// </summary>
|
||
// public static void LoadHideGuide()
|
||
// {
|
||
// if (MotionFramework.MotionEngine.GetModule<ProcessManager>()._currentMode == ProcessMode.教学模式 && TutorialGuideManager.Instance||
|
||
// MotionFramework.MotionEngine.GetModule<ProcessManager>()._currentMode == ProcessMode.课程预览 && TutorialGuideManager.Instance)
|
||
// {
|
||
// TutorialGuideManager.Instance.HideGuide();
|
||
// }
|
||
// }
|
||
|
||
/// <summary>
|
||
/// 查找Resources
|
||
/// </summary>
|
||
/// <param name="path"></param>
|
||
/// <returns></returns>
|
||
public static GameObject LoadPrefabFromResources(string path,Transform parent)
|
||
{
|
||
if (MotionFramework.MotionEngine.GetModule<ProcessManager>()._currentMode == ProcessMode.教学模式||
|
||
MotionFramework.MotionEngine.GetModule<ProcessManager>()._currentMode == ProcessMode.课程预览 )
|
||
{
|
||
GameObject prefab = Resources.Load<GameObject>(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<T>(string objectName) where T : Component
|
||
{
|
||
// 遍历场景中的所有物体
|
||
GameObject[] allObjects = GameObject.FindObjectsOfType<GameObject>(true); // 第二个参数设置为true,表示查找包括隐藏的物体
|
||
|
||
foreach (GameObject obj in allObjects)
|
||
{
|
||
if (obj.name == objectName)
|
||
{
|
||
T component = obj.GetComponent<T>();
|
||
|
||
// 如果该物体有该组件,则返回
|
||
if (component != null)
|
||
{
|
||
return component;
|
||
}
|
||
else
|
||
{
|
||
Debug.LogWarning("物体 " + objectName + " 找不到指定类型的组件: " + typeof(T));
|
||
}
|
||
}
|
||
}
|
||
|
||
return null; // 如果没有找到组件或物体,返回null
|
||
}
|
||
|
||
#region
|
||
|
||
/// <summary>
|
||
/// 读取数据 PC端
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="fileName"></param>
|
||
/// <param name="type"></param>
|
||
/// <returns></returns>
|
||
public static T LoadData<T>(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<T>(jsonStr);
|
||
break;
|
||
case JsonType.JsonUtility:
|
||
data = JsonUtility.FromJson<T>(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<byte[]> 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<string> 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<string> 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<string, string> header, Action<string> 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<T>(string url, Action<T> 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<T>(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<T>(string url, Dictionary<string, string> header, Action<T> 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<T>(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<T>(string url, Action<T> 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<T>(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<T>(string url, WWWForm formdata, Dictionary<string, string> headers = null, Action<T> 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<T>(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<string, string> headers = null, Action<string> 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<string, string> headers = null, string jsonBody = null, Action<string> 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<string, string> headers = null, int time = 50, Action<string> 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<string, string> headers = null, Action<string> 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<string, string> headers = null, string jsonBody = null, Action<string> 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;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|