PhotographSaveView.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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.touchable = true;
  46. _ui.m_btnSavePhoto.selected = false;
  47. _ui.m_btnSavePhoto.touchable = 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. if(LocalCache.GetBool(GameConst.WRITE_EXTERNAL_STORAGE_FORBIDDEN, false))
  62. {
  63. AlertSystem.Show("保存至本地需要使用存储权限,您已经禁止!请前往手机系统设置开启应用存储权限。")
  64. .SetLeftButton(true, "知道了");
  65. return;
  66. }
  67. //检查用户是否已授予对需要授权的设备资源或信息的访问权。
  68. if (!Permission.HasUserAuthorizedPermission(Permission.ExternalStorageWrite))
  69. {
  70. PromptController.Instance.ShowFloatTextPrompt("保存至本地需要使用存储权限,请同意!");
  71. //请求用户授权访问需要授权的设备资源或信息.
  72. PermissionCallbacks permissionCallbacks = new PermissionCallbacks();
  73. permissionCallbacks.PermissionGranted += (string a) => {
  74. TrySavePicturoToLocal();
  75. };
  76. permissionCallbacks.PermissionDenied += (string a) =>
  77. {
  78. PromptController.Instance.ShowFloatTextPrompt("由于被禁止存储权限,保存失败!");
  79. };
  80. permissionCallbacks.PermissionDeniedAndDontAskAgain += (string a) =>
  81. {
  82. LocalCache.SetBool(GameConst.WRITE_EXTERNAL_STORAGE_FORBIDDEN, true);
  83. PromptController.Instance.ShowFloatTextPrompt("由于被禁止存储权限,保存失败!");
  84. };
  85. Permission.RequestUserPermission(Permission.ExternalStorageWrite, permissionCallbacks);
  86. }
  87. TrySavePicturoToLocal();
  88. }
  89. private void TrySavePicturoToLocal()
  90. {
  91. if (Permission.HasUserAuthorizedPermission(Permission.ExternalStorageWrite))
  92. {
  93. string fileName = "wsj" + TimeHelper.ServerNowSecs + ".jpg";
  94. PhotographUtil.Instance.SavePicturoToLocal(bytes, fileName);
  95. _ui.m_btnSave.touchable = false;
  96. }
  97. }
  98. private async void OnClickBtnSavePhoto()
  99. {
  100. if (PoemPhotoDataManager.Instance.PersonalPhotoInfos.Count >= GlobalCfgArray.globalCfg.maxPhotoCount)
  101. {
  102. PromptController.Instance.ShowFloatTextPrompt("照片数量已达上限");
  103. return;
  104. }
  105. object[] rsp = await PoemPhotoSProxy.ReqTempPictureUrl();
  106. if (rsp == null) return;
  107. bool pushResult = await PictureStorageHelper.PushToHWCloud(rsp[0].ToString(), bytes);
  108. if (!pushResult) return;
  109. long addResult = await PoemPhotoSProxy.ReqAddTophoto((long)rsp[1]);
  110. if (addResult > 0)
  111. {
  112. List<PoemPhotoData> list = PoemPhotoDataManager.Instance.PersonalPhotoInfos;
  113. for (int i = 0; i < list.Count; i++)
  114. {
  115. if (list[i].PictureId == addResult)
  116. {
  117. list[i].Bytes = bytes;
  118. list[i].Ntexture = new NTexture(tex);
  119. break;
  120. }
  121. }
  122. }
  123. _ui.m_btnSavePhoto.touchable = false;
  124. }
  125. private void CheckGuide(object param)
  126. {
  127. if (GuideDataManager.IsGuideFinish(ConstGuideId.FREEDOM_DRESS) <= 0)
  128. {
  129. UpdateToCheckGuide(null);
  130. }
  131. else
  132. {
  133. Timers.inst.Remove(CheckGuide);
  134. }
  135. }
  136. protected override void UpdateToCheckGuide(object param)
  137. {
  138. if (!ViewManager.CheckIsTopView(this.viewCom)) return;
  139. GuideController.TryGuide(_ui.m_btnClose, ConstGuideId.FREEDOM_DRESS, 9, "");
  140. GuideController.TryCompleteGuide(ConstGuideId.FREEDOM_DRESS, 9);
  141. }
  142. protected override void TryCompleteGuide()
  143. {
  144. base.TryCompleteGuide();
  145. // GuideCfg cfg = GuideCfgArray.Instance.GetCfg(ConstGuideId.FREEDOM_DRESS);
  146. GuideController.TryCompleteGuideIndex(ConstGuideId.FREEDOM_DRESS, 9);
  147. GuideController.TryCompleteGuide(ConstGuideId.FREEDOM_DRESS, 9);
  148. }
  149. }
  150. }