PhotographSaveView.cs 9.1 KB

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