using System.IO;
using UnityEngine;
using UnityEngine.Events;
using LitJson;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine.Networking;
using Unity.VisualScripting.FullSerializer;
using System.Runtime.InteropServices.ComTypes;
public class NetManager : BaseManager<NetManager>
{
private NetManager() { }
/// <summary>
/// 网络访问的token
/// </summary>
public string token;
public string url;
/// <summary>
/// UI_CheckTaskPanel面板内容的学习、练习模式可以外配,读取json的名称
/// </summary>
string fileName = "10001.json";
public string content { get; private set; }
[Serializable]
public class IPConfig
{
public string content;
}
/// <summary>
/// 获取本地配置文件
/// </summary>
/// <param name="action"></param>
public void GetConfig(UnityAction<bool> action)
{
string path = Application.streamingAssetsPath + "/Config/Config.txt";
if (File.Exists(path))
{
url = File.ReadAllText(path);
action?.Invoke(true);
}
else
{
Debug.Log("没有对应的文件");
action?.Invoke(false);
//UIManager.Instance.ShowPanel<UI_MessagePanel>(E_UI_Layer.System, (panel) =>
//{
// panel.Init("没有读取到正确的Ip地址文件,请检查项目StreamingAssets文件夹下Config文件是否正常配置地址端口后再试!", E_MessageType.Error, Const.E_QuitApp);
//});
}
}
/// <summary>
///
/// </summary>
/// <param name="info"></param>
public void SaveInfo(string info)
{
string path = Application.streamingAssetsPath + "/Config/info.txt";
Debug.Log(path);
File.WriteAllText(path, info);
}
/// <summary>
/// UI_CheckTaskPanel面板内容的学习、练习模式用json读取,可以外配
/// </summary>
/// <param name="action"></param>
public void GetCheckTaskState(UnityAction<bool> action)
{
MonoMgr.Instance.StartCoroutine(GetCheckTaskStateAsync(action));
}
private IEnumerator GetCheckTaskStateAsync(UnityAction<bool> action)
{
string path = System.IO.Path.Combine(Application.streamingAssetsPath, fileName);
UnityWebRequest request = UnityWebRequest.Get(path);
yield return request.SendWebRequest();
if (request.result != UnityWebRequest.Result.Success)
{
Debug.Log("Failed to load file: " + request.error);
}
else
{
// 获取文件内容
string fileContent = request.downloadHandler.text;
IPConfig config = JsonUtility.FromJson<IPConfig>(fileContent);
content = config.content;
Debug.Log(content);
action?.Invoke(true);
}
}
/// <summary>
/// UI_CheckTaskPanel面板内容的考试模式用接口调取
/// </summary>
/// <param name="action"></param>
public void GetCheckTaskExam(UnityAction<bool> action)
{
MonoMgr.Instance.StartCoroutine(GetCheckTaskExamAsync(action));
}
private IEnumerator GetCheckTaskExamAsync(UnityAction<bool> action)
{
using (UnityWebRequest unityWebRequest = UnityWebRequest.Post("", string.Empty))
{
yield return unityWebRequest.SendWebRequest();
if (unityWebRequest.result == UnityWebRequest.Result.Success)
{
Debug.Log("连接成功:*******************\n" + unityWebRequest.downloadHandler.text);
string json = unityWebRequest.downloadHandler.text;
action?.Invoke(true);
Debug.Log("连接成功 ");
}
else
{
Debug.Log("连接失败 ");
action?.Invoke(false);
}
}
}
}