using System; using System.Collections; using System.IO; using ET; 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); } protected override void OnHide() { base.OnHide(); } public override void Dispose() { base.Dispose(); } private void OnClickBtnSave() { string path = Application.persistentDataPath + "/Pictures/WanshiJing/"; //判断目录是否存在,不存在则会创建目录 if (!Directory.Exists(path)) { try { Directory.CreateDirectory(path); } catch (Exception exception) { throw new Exception("创建文件夹失败, error:" + exception.Message); } } string fileName = "wsj" + TimeHelper.ServerNowSecs + ".jpg"; Texture2D tex = this.viewData as Texture2D; byte[] bytes = tex.EncodeToJPG();//将纹理数据,转化成一个jpg图片 var filePath = path + fileName; File.WriteAllBytes(filePath, bytes); UpdateSystemPhoto(filePath); } //调用iOS或Android原生方法保存图片后更新相册. private void UpdateSystemPhoto(string filePath) { #if UNITY_ANDROID AndroidJavaObject androidJavaObject = new AndroidJavaObject("com.gfg.gfglibrary.SaveImage"); //设置成我们aar库中的签名+类名 androidJavaObject.Call("scanFile", filePath, "已保存至相册"); //这里我们可以设置保存成功弹窗内容 #endif } } }