PhotographSaveView.cs 9.6 KB

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