PhotographSaveView.cs 5.1 KB

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