PhotographSaveView.cs 6.3 KB

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