Files
PrinceOfGlory/Assets/代码/资源下载/OfflineCopy.cs
kridoo 6e91a0c7f0 111
2025-09-15 17:32:08 +08:00

126 lines
4.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////离线拷贝/////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
using LitJson;
using System.Collections;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
public class OfflineCopy : MonoBehaviour
{
float timers;
void Start()
{
Invoke("GetVideoInfo", 1f);
}
//每十分钟获取一次资源信息
private void Update()
{
timers += Time.deltaTime;
if (timers >= 600f)
{
timers = 0f;
GetVideoInfo();
}
}
/// <summary>
/// 获取资源信息,并记录到本地
/// </summary>
public void GetVideoInfo()
{
if (Directory.Exists(Config.AllAssetsInfoPath))
{
Directory.Delete(Config.AllAssetsInfoPath, true);
}
GameTools.CreateDirectory(Config.AllAssetsInfoPath);
StartCoroutine(GetJsonData());
}
//获取资源信息
IEnumerator GetJsonData()
{
//检查网络是否可以访问
using (UnityWebRequest request = UnityWebRequest.Get("http://www.baidu.com"))
{
yield return request.SendWebRequest();
if (request.result != UnityWebRequest.Result.Success)
{
//ClientManager._Instance.ShowLog("网络无法访问");
yield break;
}
else
{
////ClientManager._Instance.ShowLog("网络可用");
}
}
WWWForm form = new WWWForm();
WebNet.Encrypt(Config.SN);
//Debug.Log(WebNet.s);//加密前字符串
//Debug.Log(WebNet.Authorization);//加密后token
form.AddField("deviceSN", Config.SN);
form.AddField("password", WebNet.password);
using (UnityWebRequest req = UnityWebRequest.Post(WebNet.assetsUrl, form))
{
yield return req.SendWebRequest();
JsonData jsonData = JsonMapper.ToObject(req.downloadHandler.text);
//Debug.Log(jsonData["code"]);
//Debug.Log(jsonData["msg"]);
if (jsonData["code"].ToString() == "200")
{
JsonData data = jsonData["data"];
JsonData video = data["video"];
for (int i = 0; i < video.Count; i++)
{
Assets videoData = new Assets();
videoData.resource_id = int.Parse(video[i]["resource_id"].ToString());
videoData.resource_name = video[i]["resource_name"].ToString();
videoData.url = video[i]["url"].ToString();
videoData.brief = video[i]["brief"].ToString();
videoData.icon = video[i]["icon"].ToString();
videoData.file_size = int.Parse(video[i]["file_size"].ToString());
videoData.group_id = int.Parse(video[i]["group_id"].ToString());
videoData.group_name = video[i]["group_name"].ToString();
videoData.device_id = int.Parse(video[i]["device_id"].ToString());
videoData.play_time = int.Parse(video[i]["play_time"].ToString());
videoData.province = video[i]["province"].ToString();
videoData.city = video[i]["city"].ToString();
videoData.updated_at = video[i]["updated_at"].ToString();
//Debug.Log("资源id" + videoData.resource_id);
//Debug.Log("资源名字:" + videoData.resource_name);
//Debug.Log("资源地址:" + videoData.url);
//Debug.Log("资源简介:" + videoData.brief);
//Debug.Log("资源缩略图:" + videoData.icon);
//Debug.Log("资源大小:" + videoData.file_size);
//Debug.Log("资源分组id" + videoData.group_id);
//Debug.Log("资源名称:" + videoData.group_name);
//Debug.Log("设备id" + videoData.device_id);
//Debug.Log("播放次数:" + videoData.play_time);
//Debug.Log("分组所属省份:" + videoData.province);
//Debug.Log("分组所属城市:" + videoData.city);
//Debug.Log("更新日期:" + videoData.updated_at);
RecordInfo(videoData);
}
}
}
}
/// <summary>
/// 记录资源信息
/// </summary>
void RecordInfo(Assets videoData)
{
string json = JsonUtility.ToJson(videoData);
byte[] bytes = Encoding.UTF8.GetBytes(json);
string path = Path.Combine(Config.AllAssetsInfoPath, $"{videoData.resource_name}.json");
GameTools.WriteFile(path, bytes);
}
}