PhotographSaveView.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. using System.Collections.Generic;
  2. using ET;
  3. using FairyGUI;
  4. using UI.DressUp;
  5. using UnityEngine;
  6. using UnityEngine.Android;
  7. namespace GFGGame
  8. {
  9. public class PhotographSaveView : BaseView
  10. {
  11. private UI_PhotographSaveUI _ui;
  12. private Texture2D tex;
  13. private byte[] bytes;
  14. protected override void OnInit()
  15. {
  16. base.OnInit();
  17. packageName = UI_PhotographUI.PACKAGE_NAME;
  18. _ui = UI_PhotographSaveUI.Create();
  19. viewCom = _ui.target;
  20. isfullScreen = true;
  21. _ui.m_btnClose.onClick.Add(this.Hide);
  22. _ui.m_btnSave.onClick.Add(this.OnClickBtnSave);
  23. _ui.m_btnSavePhoto.onClick.Add(this.OnClickBtnSavePhoto);
  24. _ui.m_share.onClick.Add(this.BtnClickShare);
  25. }
  26. protected override void OnShown()
  27. {
  28. base.OnShown();
  29. tex = this.viewData as Texture2D;
  30. bytes = tex.EncodeToJPG();//将纹理数据,转化成一个jpg图片
  31. ShareDataManager.Instance.imageBytes = bytes;
  32. _ui.m_imgRes.texture = new NTexture(tex);
  33. float width = _ui.m_imgBorder.width;
  34. float height = width * tex.height / tex.width;
  35. _ui.m_imgRes.SetSize(width, height);
  36. _ui.m_imgBorder.SetSize(width, height + 12);
  37. Timers.inst.AddUpdate(CheckGuide);
  38. }
  39. protected override void OnHide()
  40. {
  41. base.OnHide();
  42. _ui.m_btnSave.selected = false;
  43. _ui.m_btnSave.enabled = true;
  44. _ui.m_btnSavePhoto.selected = false;
  45. _ui.m_btnSavePhoto.enabled = true;
  46. Timers.inst.Remove(CheckGuide);
  47. }
  48. public override void Dispose()
  49. {
  50. if (_ui != null)
  51. {
  52. _ui.Dispose();
  53. _ui = null;
  54. }
  55. base.Dispose();
  56. }
  57. private void OnClickBtnSave()
  58. {
  59. #if UNITY_EDITOR
  60. PromptController.Instance.ShowFloatTextPrompt("此功能只能在移动端使用!");
  61. #else
  62. CheckSaveLocal();
  63. #endif
  64. }
  65. private async void CheckSaveLocal()
  66. {
  67. string permissionName = "存储";
  68. #if UNITY_IOS
  69. permissionName = "相册";
  70. #endif
  71. NativeGallery.Permission permission = NativeGallery.CheckPermission(NativeGallery.PermissionType.Write, NativeGallery.MediaType.Image);
  72. Debug.Log("Permission result: " + permission);
  73. if(permission.Equals(NativeGallery.Permission.Denied))
  74. {
  75. string tips = $"保存至本地需要使用{permissionName}权限,您已经禁止!请前往手机系统设置开启应用的{permissionName}权限。";
  76. if (NativeGallery.CanOpenSettings())
  77. {
  78. AlertSystem.Show(tips)
  79. .SetLeftButton(true, "前往", (data) => { NativeGallery.OpenSettings(); })
  80. .SetRightButton(true, "拒绝");
  81. }
  82. else
  83. {
  84. AlertSystem.Show(tips)
  85. .SetLeftButton(true, "知道了");
  86. }
  87. }
  88. else
  89. {
  90. if (permission.Equals(NativeGallery.Permission.ShouldAsk))
  91. {
  92. string tips = $"保存至本地需要使用{permissionName}权限,请同意!";
  93. PromptController.Instance.ShowFloatTextPrompt(tips);
  94. permission = await NativeGallery.RequestPermissionAsync(NativeGallery.PermissionType.Write, NativeGallery.MediaType.Image);
  95. //Debug.Log("Permission result: " + permission);
  96. if(!permission.Equals(NativeGallery.Permission.Granted))
  97. {
  98. PromptController.Instance.ShowFloatTextPrompt($"由于被禁止{permissionName}权限,保存失败!");
  99. return;
  100. }
  101. }
  102. string fileName = "wsj" + TimeHelper.ServerNowSecs + ".jpg";
  103. permission = NativeGallery.SaveImageToGallery(bytes, "Wanshijing", fileName, (success, path) =>
  104. {
  105. if(success)
  106. {
  107. PromptController.Instance.ShowFloatTextPrompt("已保存至本地!");
  108. _ui.m_btnSave.enabled = false;
  109. }
  110. else
  111. {
  112. PromptController.Instance.ShowFloatTextPrompt("保存失败!");
  113. }
  114. });
  115. //Debug.Log("Permission result: " + permission);
  116. }
  117. }
  118. //private void OnClickBtnSaveBackup()
  119. //{
  120. // //检查用户是否已授予对需要授权的设备资源或信息的访问权。
  121. // if (!Permission.HasUserAuthorizedPermission(Permission.ExternalStorageWrite))
  122. // {
  123. // if (LocalCache.GetBool(GameConst.WRITE_EXTERNAL_STORAGE_FORBIDDEN, false))
  124. // {
  125. // AlertSystem.Show("保存至本地需要使用存储权限,您已经禁止!请前往手机系统设置开启应用存储权限。")
  126. // .SetLeftButton(true, "知道了");
  127. // return;
  128. // }
  129. // PromptController.Instance.ShowFloatTextPrompt("保存至本地需要使用存储权限,请同意!");
  130. // //请求用户授权访问需要授权的设备资源或信息.
  131. // PermissionCallbacks permissionCallbacks = new PermissionCallbacks();
  132. // permissionCallbacks.PermissionGranted += (string a) =>
  133. // {
  134. // TrySavePicturoToLocal();
  135. // };
  136. // permissionCallbacks.PermissionDenied += (string a) =>
  137. // {
  138. // PromptController.Instance.ShowFloatTextPrompt("由于被禁止存储权限,保存失败!");
  139. // };
  140. // permissionCallbacks.PermissionDeniedAndDontAskAgain += (string a) =>
  141. // {
  142. // LocalCache.SetBool(GameConst.WRITE_EXTERNAL_STORAGE_FORBIDDEN, true);
  143. // PromptController.Instance.ShowFloatTextPrompt("由于被禁止存储权限,保存失败!");
  144. // };
  145. // Permission.RequestUserPermission(Permission.ExternalStorageWrite, permissionCallbacks);
  146. // }
  147. // else
  148. // {
  149. // LocalCache.SetBool(GameConst.WRITE_EXTERNAL_STORAGE_FORBIDDEN, false);
  150. // TrySavePicturoToLocal();
  151. // }
  152. //}
  153. //private void TrySavePicturoToLocal()
  154. //{
  155. // if (Permission.HasUserAuthorizedPermission(Permission.ExternalStorageWrite))
  156. // {
  157. // string fileName = "wsj" + TimeHelper.ServerNowSecs + ".jpg";
  158. // PhotographUtil.Instance.SavePicturoToLocal(bytes, fileName);
  159. // _ui.m_btnSave.enabled = false;
  160. // }
  161. //}
  162. private async void OnClickBtnSavePhoto()
  163. {
  164. if (GuideDataManager.currentGuideId > 0) return;//引导时不保存
  165. if (PoemPhotoDataManager.Instance.PersonalPhotoInfos.Count >= GlobalCfgArray.globalCfg.maxPhotoCount)
  166. {
  167. PromptController.Instance.ShowFloatTextPrompt("照片数量已达上限");
  168. return;
  169. }
  170. object[] rsp = await PoemPhotoSProxy.ReqTempPictureUrl();
  171. if (rsp == null) return;
  172. bool pushResult = await PictureStorageHelper.PushToHWCloud(rsp[0].ToString(), bytes);
  173. if (!pushResult) {
  174. ViewManager.Hide<ModalStatusView>();
  175. return;
  176. }
  177. long addResult = await PoemPhotoSProxy.ReqAddTophoto((long)rsp[1]);
  178. if (addResult > 0)
  179. {
  180. List<PoemPhotoData> list = PoemPhotoDataManager.Instance.PersonalPhotoInfos;
  181. for (int i = 0; i < list.Count; i++)
  182. {
  183. if (list[i].PictureId == addResult)
  184. {
  185. list[i].Bytes = bytes;
  186. list[i].Ntexture = new NTexture(tex);
  187. break;
  188. }
  189. }
  190. }
  191. _ui.m_btnSavePhoto.enabled = false;
  192. }
  193. private void BtnClickShare()
  194. {
  195. ShareDataManager.Instance.CaptureCameraToImage(true,false);
  196. }
  197. private void CheckGuide(object param)
  198. {
  199. if (GuideDataManager.IsGuideFinish(ConstGuideId.FREEDOM_DRESS) <= 0)
  200. {
  201. UpdateToCheckGuide(null);
  202. }
  203. else
  204. {
  205. Timers.inst.Remove(CheckGuide);
  206. }
  207. }
  208. protected override void UpdateToCheckGuide(object param)
  209. {
  210. if (!ViewManager.CheckIsTopView(this.viewCom)) return;
  211. GuideController.TryGuide(_ui.m_btnSavePhoto, ConstGuideId.FREEDOM_DRESS, 9, "下次点击这里可以保存至相册哦!");
  212. GuideController.TryGuide(_ui.m_btnClose, ConstGuideId.FREEDOM_DRESS, 10, "");
  213. GuideController.TryCompleteGuide(ConstGuideId.FREEDOM_DRESS, 10);
  214. }
  215. protected override void TryCompleteGuide()
  216. {
  217. base.TryCompleteGuide();
  218. // GuideCfg cfg = GuideCfgArray.Instance.GetCfg(ConstGuideId.FREEDOM_DRESS);
  219. GuideController.TryCompleteGuideIndex(ConstGuideId.FREEDOM_DRESS, 10);
  220. GuideController.TryCompleteGuide(ConstGuideId.FREEDOM_DRESS, 10);
  221. }
  222. }
  223. }