PhotographSaveView.cs 6.0 KB

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