Files
BingoGrassland/Assets/BingoSun/Scripts/CachKit.cs
T

65 lines
2.0 KiB
C#
Raw Normal View History

2026-04-20 13:49:36 +08:00
using System.IO;
using UnityEngine;
using UnityEngine.Events;
using System.Collections;
using UnityEngine.Networking;
namespace BingoBrain.Asset
{
public class CachKit
{
/// <summary>
/// 保存文本文件
/// </summary>
/// <param name="content"></param>
/// <param name="filePath"></param>
/// <param name="fileName"></param>
private static void SaveTextFile(string content, string filePath, string fileName)
{
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
File.WriteAllText($"{filePath}{fileName}.txt", content);
}
/// <summary>
/// 下载文本文件
/// </summary>
/// <param name="url"></param>
/// <param name="fileName"></param>
/// <param name="action"></param>
/// <param name="isSave"></param>
/// <returns></returns>
public static IEnumerator GetTextFromUrl(string url, string fileName, UnityAction<string> action = null,
string savePath = null, bool isSave = true)
{
var unityWebRequest = UnityWebRequest.Get(url);
yield return unityWebRequest.SendWebRequest();
if (unityWebRequest.result is UnityWebRequest.Result.ConnectionError
or UnityWebRequest.Result.ProtocolError)
{
Debug.LogError($"加载 [ {fileName} ] 文本文件失败 {unityWebRequest.error} [ url: {url} ]");
action?.Invoke(default);
yield break;
}
var content = unityWebRequest.downloadHandler.text;
if (isSave)
{
savePath ??= GetAssetSavePath();
SaveTextFile(content, savePath, fileName);
}
action?.Invoke(content);
}
public static string GetAssetSavePath()
{
return $"{Application.persistentDataPath}/BingoBase/";
}
}
}