using System.IO; using UnityEngine; using UnityEngine.Events; using System.Collections; using UnityEngine.Networking; namespace BingoBrain.Asset { public class CachKit { /// /// 保存文本文件 /// /// /// /// private static void SaveTextFile(string content, string filePath, string fileName) { if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } File.WriteAllText($"{filePath}{fileName}.txt", content); } /// /// 下载文本文件 /// /// /// /// /// /// public static IEnumerator GetTextFromUrl(string url, string fileName, UnityAction 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/"; } } }