using System; using System.Collections; using System.IO; using FairyGUI; using UI.DressUp; using UnityEngine; namespace GFGGame { public class PhotographSaveView : BaseView { private UI_PhotographSaveUI _ui; protected override void OnInit() { base.OnInit(); packageName = UI_PhotographUI.PACKAGE_NAME; _ui = UI_PhotographSaveUI.Create(); viewCom = _ui.target; isfullScreen = true; _ui.m_btnClose.onClick.Add(this.Hide); _ui.m_btnSave.onClick.Add(this.OnClickBtnSave); } protected override void OnShown() { base.OnShown(); Texture2D tex = this.viewData as Texture2D; _ui.m_imgRes.texture = new NTexture(tex); float width = _ui.m_imgBorder.width; float height = width * tex.height / tex.width; _ui.m_imgRes.SetSize(width, height); _ui.m_imgBorder.SetSize(width, height + 12); } private void OnClickBtnSave() { string path = Application.persistentDataPath; Debug.Log("文件路径path:" + path); #if UNITY_ANDROID path = Application.persistentDataPath.IndexOf("Android") > 0 ? path.Substring(0, Application.persistentDataPath.IndexOf("Android")) + "Pictures/万世镜" : path + "/Pictures/万世镜"; #endif //判断目录是否存在,不存在则会创建目录 if (!Directory.Exists(path)) { try { Directory.CreateDirectory(path); } catch (Exception exception) { throw new Exception("创建文件夹失败, error:" + exception.Message); } } string fileName = DateUtils.Instance.GetCurTime() + ".png"; path = path + "/" + fileName; Debug.Log("文件路径:" + path); Texture2D tex = this.viewData as Texture2D; byte[] bytes = tex.EncodeToPNG();//将纹理数据,转化成一个png图片 try { FileStream newFs = new FileStream(path, FileMode.Create, FileAccess.Write); newFs.Write(bytes, 0, bytes.Length); newFs.Close(); newFs.Dispose(); } catch (Exception ex) { throw new Exception("保存失败, error:" + ex.Message); } // string path = Application.persistentDataPath.Substring(0, Application.persistentDataPath.IndexOf("Android")); // File.WriteAllBytes(path, bytes); Timers.inst.StartCoroutine(GetSaveState(path));// (); } private IEnumerator GetSaveState(string path) { yield return new WaitForEndOfFrame(); if (File.Exists(path)) { PromptController.Instance.ShowFloatTextPrompt("保存成功"); } } protected override void OnHide() { base.OnHide(); } public override void Dispose() { base.Dispose(); } } }