PhotographSaveView.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Threading.Tasks;
  6. using ET;
  7. using FairyGUI;
  8. using UI.DressUp;
  9. using UnityEngine;
  10. using UnityEngine.Android;
  11. namespace GFGGame
  12. {
  13. public class PhotographSaveView : BaseView
  14. {
  15. private UI_PhotographSaveUI _ui;
  16. private Texture2D tex;
  17. private byte[] bytes;
  18. protected override void OnInit()
  19. {
  20. base.OnInit();
  21. packageName = UI_PhotographUI.PACKAGE_NAME;
  22. _ui = UI_PhotographSaveUI.Create();
  23. viewCom = _ui.target;
  24. isfullScreen = true;
  25. isReturnView = true;
  26. _ui.m_btnClose.onClick.Add(this.Hide);
  27. _ui.m_btnSave.onClick.Add(this.OnClickBtnSave);
  28. _ui.m_btnSavePhoto.onClick.Add(this.OnClickBtnSavePhoto);
  29. }
  30. protected override void OnShown()
  31. {
  32. base.OnShown();
  33. tex = this.viewData as Texture2D;
  34. bytes = tex.EncodeToJPG();//将纹理数据,转化成一个jpg图片
  35. _ui.m_imgRes.texture = new NTexture(tex);
  36. float width = _ui.m_imgBorder.width;
  37. float height = width * tex.height / tex.width;
  38. _ui.m_imgRes.SetSize(width, height);
  39. _ui.m_imgBorder.SetSize(width, height + 12);
  40. Timers.inst.AddUpdate(CheckGuide);
  41. }
  42. protected override void OnHide()
  43. {
  44. base.OnHide();
  45. _ui.m_btnSave.selected = false;
  46. _ui.m_btnSave.enabled = true;
  47. _ui.m_btnSavePhoto.selected = false;
  48. _ui.m_btnSavePhoto.enabled = true;
  49. Timers.inst.Remove(CheckGuide);
  50. }
  51. public override void Dispose()
  52. {
  53. if (_ui != null)
  54. {
  55. _ui.Dispose();
  56. _ui = null;
  57. }
  58. base.Dispose();
  59. }
  60. private void OnClickBtnSave()
  61. {
  62. //检查用户是否已授予对需要授权的设备资源或信息的访问权。
  63. if (!Permission.HasUserAuthorizedPermission(Permission.ExternalStorageWrite))
  64. {
  65. if (LocalCache.GetBool(GameConst.WRITE_EXTERNAL_STORAGE_FORBIDDEN, false))
  66. {
  67. AlertSystem.Show("保存至本地需要使用存储权限,您已经禁止!请前往手机系统设置开启应用存储权限。")
  68. .SetLeftButton(true, "知道了");
  69. return;
  70. }
  71. PromptController.Instance.ShowFloatTextPrompt("保存至本地需要使用存储权限,请同意!");
  72. //请求用户授权访问需要授权的设备资源或信息.
  73. PermissionCallbacks permissionCallbacks = new PermissionCallbacks();
  74. permissionCallbacks.PermissionGranted += (string a) =>
  75. {
  76. TrySavePicturoToLocal();
  77. };
  78. permissionCallbacks.PermissionDenied += (string a) =>
  79. {
  80. PromptController.Instance.ShowFloatTextPrompt("由于被禁止存储权限,保存失败!");
  81. };
  82. permissionCallbacks.PermissionDeniedAndDontAskAgain += (string a) =>
  83. {
  84. LocalCache.SetBool(GameConst.WRITE_EXTERNAL_STORAGE_FORBIDDEN, true);
  85. PromptController.Instance.ShowFloatTextPrompt("由于被禁止存储权限,保存失败!");
  86. };
  87. Permission.RequestUserPermission(Permission.ExternalStorageWrite, permissionCallbacks);
  88. }
  89. else
  90. {
  91. LocalCache.SetBool(GameConst.WRITE_EXTERNAL_STORAGE_FORBIDDEN, false);
  92. TrySavePicturoToLocal();
  93. }
  94. }
  95. private void TrySavePicturoToLocal()
  96. {
  97. if (Permission.HasUserAuthorizedPermission(Permission.ExternalStorageWrite))
  98. {
  99. string fileName = "wsj" + TimeHelper.ServerNowSecs + ".jpg";
  100. PhotographUtil.Instance.SavePicturoToLocal(bytes, fileName);
  101. _ui.m_btnSave.enabled = false;
  102. }
  103. }
  104. private async void OnClickBtnSavePhoto()
  105. {
  106. if (PoemPhotoDataManager.Instance.PersonalPhotoInfos.Count >= GlobalCfgArray.globalCfg.maxPhotoCount)
  107. {
  108. PromptController.Instance.ShowFloatTextPrompt("照片数量已达上限");
  109. return;
  110. }
  111. object[] rsp = await PoemPhotoSProxy.ReqTempPictureUrl();
  112. if (rsp == null) return;
  113. bool pushResult = await PictureStorageHelper.PushToHWCloud(rsp[0].ToString(), bytes);
  114. if (!pushResult) return;
  115. long addResult = await PoemPhotoSProxy.ReqAddTophoto((long)rsp[1]);
  116. if (addResult > 0)
  117. {
  118. List<PoemPhotoData> list = PoemPhotoDataManager.Instance.PersonalPhotoInfos;
  119. for (int i = 0; i < list.Count; i++)
  120. {
  121. if (list[i].PictureId == addResult)
  122. {
  123. list[i].Bytes = bytes;
  124. list[i].Ntexture = new NTexture(tex);
  125. break;
  126. }
  127. }
  128. }
  129. _ui.m_btnSavePhoto.enabled = false;
  130. }
  131. private void CheckGuide(object param)
  132. {
  133. if (GuideDataManager.IsGuideFinish(ConstGuideId.FREEDOM_DRESS) <= 0)
  134. {
  135. UpdateToCheckGuide(null);
  136. }
  137. else
  138. {
  139. Timers.inst.Remove(CheckGuide);
  140. }
  141. }
  142. protected override void UpdateToCheckGuide(object param)
  143. {
  144. if (!ViewManager.CheckIsTopView(this.viewCom)) return;
  145. GuideController.TryGuide(_ui.m_btnSavePhoto, ConstGuideId.FREEDOM_DRESS, 9, "");
  146. GuideController.TryGuide(_ui.m_btnClose, ConstGuideId.FREEDOM_DRESS, 10, "");
  147. GuideController.TryCompleteGuide(ConstGuideId.FREEDOM_DRESS, 10);
  148. }
  149. protected override void TryCompleteGuide()
  150. {
  151. base.TryCompleteGuide();
  152. // GuideCfg cfg = GuideCfgArray.Instance.GetCfg(ConstGuideId.FREEDOM_DRESS);
  153. GuideController.TryCompleteGuideIndex(ConstGuideId.FREEDOM_DRESS, 10);
  154. GuideController.TryCompleteGuide(ConstGuideId.FREEDOM_DRESS, 10);
  155. }
  156. }
  157. }