IEnumerator LoadAndCopyImage(string path, string saveName, Action <UnityEngine.Object> callback) { UnityWebRequest request = UnityWebRequest.Get("file:///" + path); DownloadHandlerTexture downTexture = new DownloadHandlerTexture(true); request.downloadHandler = downTexture; yield return(request.SendWebRequest()); if (request.result == UnityWebRequest.Result.Success && request != null) { //获取Texture Texture2D texture = downTexture.texture; //直接将选择图保存为png格式,拷贝到指定目录 byte[] bytes = texture.EncodeToPNG(); File.WriteAllBytes(PathMgr.GetInstance().ImagePath + saveName + ".png", bytes); Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f)); if (!m_dicCachSprite.ContainsKey(saveName)) { m_dicCachSprite.Add(saveName, sprite); } callback(sprite); } else { LogMgr.GetInstance().Log(LogEnum.Error, "加载图片失败:" + path); } }
/// <summary> /// 保存json数据到本地 /// </summary> /// <param name="data"></param> public static bool SaveJsonDataToLocal(JsonData data, string saveName) { if (data == null) { return(false); } try { string saveData = JsonMapper.ToJson(data); //转译中文 Regex reg = new Regex(@"(?i)\\[uU]([0-9a-f]{4})"); saveData = reg.Replace(saveData, delegate(Match m) { return(((char)Convert.ToInt32(m.Groups[1].Value, 16)).ToString()); }); StreamWriter streamWriter; if (!PathMgr.GetInstance().CheckFile(PathMgr.GetInstance().StreamPath + "/" + saveName + ".json")) { streamWriter = File.CreateText(PathMgr.GetInstance().StreamPath + "/" + saveName + ".json"); } else { streamWriter = new StreamWriter(PathMgr.GetInstance().StreamPath + "/" + saveName + ".json"); } streamWriter.Write(saveData); streamWriter.Dispose(); streamWriter.Close(); return(true); } catch (Exception e) { LogMgr.GetInstance().Log(LogEnum.Error, "保存" + saveName + ".json失败" + e.Message); return(false); } }
void Start() { FrameMgr.GetInstance(); TimerMgr.GetInstance(); PathMgr.GetInstance(); ConfigMgr.GetInstance(); AssetMgr.GetInstance(); UIMgr.GetInstance(); SoundMgr.GetInstance(); LoadSceneMgr.LoadSence("test"); }
public void DeleteImage(string name) { string path = PathMgr.GetInstance().ImagePath + name; if (m_dicCachSprite.ContainsKey(name)) { m_dicCachSprite.Remove(name); File.Delete(path); } else { LogMgr.GetInstance().Log(LogEnum.Warming, "没有缓存的图片,怎么删除呢:" + path); } }
IEnumerator LoadSpriteAsync(string name, Action <UnityEngine.Object> callback) { string path = PathMgr.GetInstance().ImagePath + name + ".png"; UnityWebRequest request = UnityWebRequest.Get("file:///" + path); DownloadHandlerTexture downTexture = new DownloadHandlerTexture(true); request.downloadHandler = downTexture; yield return(request.SendWebRequest()); if (request.result == UnityWebRequest.Result.Success && request != null) { Texture2D texture = downTexture.texture; Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f)); callback(sprite); } }
private static void Init() { if (DicLocalization.Count == 0) { AssetMgr.GetInstance().LoadAsset(PathMgr.GetInstance().GetPath(Define.DataType.Localization), false, false, () => { string[] loc = AssetMgr.GetInstance().GetAsset().ToString().Split(new[] { '\n' }, System.StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < loc.Length; i++) { loc[i] = loc[i].Replace("\r", ""); } LanguageList = loc[0].Split(','); for (int i = 1; i < loc.Length; i++) { string[] data = loc[i].Split(','); DicLocalization.Add(data[0], data); } }); } }
/// <summary> /// 解析json数据 /// </summary> /// <param name="jsonName">json名字</param> /// <returns>Objec类型的JsonData</returns> public static JsonData ParseToJsonData(string jsonName) { string path = PathMgr.GetInstance().StreamPath + "/" + jsonName + ".json"; if (PathMgr.GetInstance().CheckFile(path)) { try { StreamReader r = new StreamReader(path); string json = r.ReadToEnd(); r.Close(); JsonData data = JsonMapper.ToObject(json); return(data); } catch (Exception e) { LogMgr.GetInstance().Log(LogEnum.Error, "解析Json出错:" + e.Message); return(null); } } return(null); }
public void Init() { //缓存保存的图片资源 if (Directory.Exists(PathMgr.GetInstance().ImagePath)) { DirectoryInfo dir = new DirectoryInfo(PathMgr.GetInstance().ImagePath); FileInfo[] files = dir.GetFiles("*", SearchOption.AllDirectories); for (int i = 0; i < files.Length; i++) { if (files[i].Name.EndsWith(".meta")) { continue; } int index = files[i].Name.LastIndexOf('.'); string key = files[i].Name.Remove(index); //没有缓存过的才请求加载 if (!m_dicCachSprite.ContainsKey(key)) { StartCoroutine(LoadSpriteAsync(key, (obj) => { Sprite sp = obj as Sprite; if (sp == null) { return; } m_dicCachSprite.Add(key, sp); })); } } ConfigMgr.GetInstance().V_MaxImageIndex = m_dicCachSprite.Count; } else { Directory.CreateDirectory(PathMgr.GetInstance().ImagePath); } }