using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; using ET; using FairyGUI; using UI.DressUp; using UnityEngine; using UnityEngine.Android; namespace GFGGame { public class PhotographSaveView : BaseView { private UI_PhotographSaveUI _ui; private Texture2D tex; private byte[] bytes; 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); _ui.m_btnSavePhoto.onClick.Add(this.OnClickBtnSavePhoto); } protected override void OnShown() { base.OnShown(); tex = this.viewData as Texture2D; bytes = tex.EncodeToJPG();//将纹理数据,转化成一个jpg图片 _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); Timers.inst.AddUpdate(CheckGuide); } protected override void OnHide() { base.OnHide(); _ui.m_btnSave.selected = false; _ui.m_btnSave.enabled = true; _ui.m_btnSavePhoto.selected = false; _ui.m_btnSavePhoto.enabled = true; Timers.inst.Remove(CheckGuide); } public override void Dispose() { if (_ui != null) { _ui.Dispose(); _ui = null; } base.Dispose(); } private void OnClickBtnSave() { //检查用户是否已授予对需要授权的设备资源或信息的访问权。 if (!Permission.HasUserAuthorizedPermission(Permission.ExternalStorageWrite)) { if (LocalCache.GetBool(GameConst.WRITE_EXTERNAL_STORAGE_FORBIDDEN, false)) { AlertSystem.Show("保存至本地需要使用存储权限,您已经禁止!请前往手机系统设置开启应用存储权限。") .SetLeftButton(true, "知道了"); return; } PromptController.Instance.ShowFloatTextPrompt("保存至本地需要使用存储权限,请同意!"); //请求用户授权访问需要授权的设备资源或信息. PermissionCallbacks permissionCallbacks = new PermissionCallbacks(); permissionCallbacks.PermissionGranted += (string a) => { TrySavePicturoToLocal(); }; permissionCallbacks.PermissionDenied += (string a) => { PromptController.Instance.ShowFloatTextPrompt("由于被禁止存储权限,保存失败!"); }; permissionCallbacks.PermissionDeniedAndDontAskAgain += (string a) => { LocalCache.SetBool(GameConst.WRITE_EXTERNAL_STORAGE_FORBIDDEN, true); PromptController.Instance.ShowFloatTextPrompt("由于被禁止存储权限,保存失败!"); }; Permission.RequestUserPermission(Permission.ExternalStorageWrite, permissionCallbacks); } else { LocalCache.SetBool(GameConst.WRITE_EXTERNAL_STORAGE_FORBIDDEN, false); TrySavePicturoToLocal(); } } private void TrySavePicturoToLocal() { if (Permission.HasUserAuthorizedPermission(Permission.ExternalStorageWrite)) { string fileName = "wsj" + TimeHelper.ServerNowSecs + ".jpg"; PhotographUtil.Instance.SavePicturoToLocal(bytes, fileName); _ui.m_btnSave.enabled = false; } } private async void OnClickBtnSavePhoto() { if (PoemPhotoDataManager.Instance.PersonalPhotoInfos.Count >= GlobalCfgArray.globalCfg.maxPhotoCount) { PromptController.Instance.ShowFloatTextPrompt("照片数量已达上限"); return; } object[] rsp = await PoemPhotoSProxy.ReqTempPictureUrl(); if (rsp == null) return; bool pushResult = await PictureStorageHelper.PushToHWCloud(rsp[0].ToString(), bytes); if (!pushResult) return; long addResult = await PoemPhotoSProxy.ReqAddTophoto((long)rsp[1]); if (addResult > 0) { List list = PoemPhotoDataManager.Instance.PersonalPhotoInfos; for (int i = 0; i < list.Count; i++) { if (list[i].PictureId == addResult) { list[i].Bytes = bytes; list[i].Ntexture = new NTexture(tex); break; } } } _ui.m_btnSavePhoto.enabled = false; } private void CheckGuide(object param) { if (GuideDataManager.IsGuideFinish(ConstGuideId.FREEDOM_DRESS) <= 0) { UpdateToCheckGuide(null); } else { Timers.inst.Remove(CheckGuide); } } protected override void UpdateToCheckGuide(object param) { if (!ViewManager.CheckIsTopView(this.viewCom)) return; GuideController.TryGuide(_ui.m_btnClose, ConstGuideId.FREEDOM_DRESS, 9, ""); GuideController.TryGuide(_ui.m_btnSave, ConstGuideId.FREEDOM_DRESS, 10, ""); GuideController.TryCompleteGuide(ConstGuideId.FREEDOM_DRESS, 10); } protected override void TryCompleteGuide() { base.TryCompleteGuide(); // GuideCfg cfg = GuideCfgArray.Instance.GetCfg(ConstGuideId.FREEDOM_DRESS); GuideController.TryCompleteGuideIndex(ConstGuideId.FREEDOM_DRESS, 10); GuideController.TryCompleteGuide(ConstGuideId.FREEDOM_DRESS, 10); } } }