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() { Texture2D tex = this.viewData as Texture2D; byte[] bytes = tex.EncodeToPNG();//将纹理数据,转化成一个png图片 string path = Application.persistentDataPath + "/wanshijing"; #if UNITY_ANDROID path = "/sdcard/DCIM/wanshijing"; #endif //判断目录是否存在,不存在则会创建目录 if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string fileName = DateUtils.Instance.GetCurTime() + ".png"; path = path + "/" + fileName; Debug.Log("文件路径:" + fileName); //存图片 System.IO.File.WriteAllBytes(fileName, bytes);//写入数据 PromptController.Instance.ShowFloatTextPrompt("保持成功"); } protected override void OnHide() { base.OnHide(); } public override void Dispose() { base.Dispose(); } } }