PoemPhotoSaveView.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System.Collections;
  2. using UnityEngine;
  3. using UI.Poem;
  4. using System.Collections.Generic;
  5. using ET;
  6. using FairyGUI;
  7. using System.Threading.Tasks;
  8. namespace GFGGame
  9. {
  10. public class PoemPhotoSaveView : BaseWindow
  11. {
  12. private UI_PoemPhotoSaveUI _ui;
  13. private int curIndex;
  14. private bool saveFinished;
  15. private bool captureFinished;
  16. private byte[] byteArr;
  17. public override void Dispose()
  18. {
  19. if(_ui != null)
  20. {
  21. _ui.Dispose();
  22. _ui = null;
  23. }
  24. base.Dispose();
  25. }
  26. protected override void OnInit()
  27. {
  28. base.OnInit();
  29. packageName = UI_PoemPhotoSaveUI.PACKAGE_NAME;
  30. _ui = UI_PoemPhotoSaveUI.Create();
  31. viewCom = _ui.target;
  32. isfullScreen = true;
  33. this.modal = false;
  34. clickBlankToClose = false;
  35. }
  36. protected override void OnShown()
  37. {
  38. base.OnShown();
  39. SavePhotoList savePhoto = (SavePhotoList)viewData;
  40. _ui.m_c1.selectedIndex = savePhoto.pageIndex;
  41. UpdateProgress(0, savePhoto.listChoose.Count);
  42. RefreshUI(savePhoto);
  43. }
  44. protected override void OnHide()
  45. {
  46. base.OnHide();
  47. ResetData();
  48. }
  49. private void ResetData()
  50. {
  51. curIndex = 0;
  52. byteArr = null;
  53. saveFinished = false;
  54. captureFinished = false;
  55. }
  56. private async void RefreshUI(SavePhotoList savePhoto)
  57. {
  58. for(int i = 0; i < savePhoto.listChoose.Count; i++)
  59. {
  60. int index = GetPhotoListIndex(savePhoto.photoInfos, savePhoto.listChoose[i]);
  61. byte[] bytes = null;
  62. if (_ui.m_c1.selectedIndex == 0)
  63. {
  64. RefreshPhoto(savePhoto.photoInfos[index]);
  65. bytes = savePhoto.photoInfos[index].Bytes;
  66. }
  67. else
  68. {
  69. RefreshTravelPhoto(savePhoto.photoInfos[i]);
  70. captureFinished = false;
  71. Timers.inst.StartCoroutine(CaptureByUI());
  72. while (!captureFinished)
  73. {
  74. await Task.Delay(17);
  75. }
  76. bytes = byteArr;
  77. }
  78. saveFinished = false;
  79. SavePhoto(bytes);
  80. while (!saveFinished)
  81. {
  82. await Task.Delay(17);
  83. }
  84. ++curIndex;
  85. UpdateProgress(curIndex, savePhoto.listChoose.Count);
  86. if (curIndex == savePhoto.listChoose.Count)
  87. {
  88. PromptController.Instance.ShowFloatTextPrompt("已全部保存至相册!");
  89. Hide();
  90. }
  91. }
  92. }
  93. private int GetPhotoListIndex(List<PoemPhotoData> photoInfos, long pictureID)
  94. {
  95. for (int i = 0; i < photoInfos.Count; i++)
  96. {
  97. if (photoInfos[i].PictureId == pictureID)
  98. {
  99. return i;
  100. }
  101. }
  102. return -1;
  103. }
  104. private void UpdateProgress(int cur, int max)
  105. {
  106. _ui.m_progress.value = cur;
  107. _ui.m_progress.max = max;
  108. }
  109. private void RefreshPhoto(PoemPhotoData data)
  110. {
  111. UI_ListPhotoPreviewItem item = UI_ListPhotoPreviewItem.Proxy(_ui.m_personalPhoto.target);
  112. item.m_comPhoto.m_loaPhoto.texture = data.Ntexture;
  113. UI_ListPhotoPreviewItem.ProxyEnd();
  114. }
  115. private void RefreshTravelPhoto(PoemPhotoData data)
  116. {
  117. UI_ComPostcard item = UI_ComPostcard.Proxy(_ui.m_travelPhoto.target);
  118. PoemPhotoData photoData = data;
  119. TravelLoactionCfg loactionCfg = TravelLoactionCfgArray.Instance.GetCfg(photoData.TravelLocationId);
  120. item.m_comTravel.m_loaBg.url = ResPathUtil.GetTravelBgPath(loactionCfg.res);
  121. item.m_txtLocationName.text = loactionCfg.name;
  122. item.m_txtTime.text = TimeUtil.FormattingTimeTo_yyyMMdd1(photoData.CreationTime);
  123. item.m_comTravel.m_loaRole.url = "";
  124. if (photoData.TravelSuitId > 0)
  125. {
  126. TravelSuitCfg travelSuitCfg = TravelSuitCfgArray.Instance.GetCfg(photoData.TravelSuitId);
  127. item.m_comTravel.m_loaRole.url = ResPathUtil.GetTravelRolePath(travelSuitCfg.reourcesArr[photoData.SuitResIndex]);
  128. item.m_comTravel.m_loaRole.SetXY(loactionCfg.positionsArr[photoData.PositionIndex][0], loactionCfg.positionsArr[photoData.PositionIndex][1]);
  129. }
  130. UI_ComPostcard.ProxyEnd();
  131. }
  132. private IEnumerator CaptureByUI()
  133. {
  134. //等待帧画面渲染结束
  135. yield return new WaitForEndOfFrame();
  136. int width = (int)(_ui.m_saveImg.width * UIContentScaler.scaleFactor);
  137. int height = (int)(_ui.m_saveImg.height * UIContentScaler.scaleFactor);
  138. Vector2 pos = _ui.m_saveImg.LocalToGlobal(Vector2.zero);
  139. pos.y = Screen.height - pos.y - height;
  140. Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
  141. //从屏幕读取像素, leftBtmX/leftBtnY 是读取的初始位置,width、height是读取像素的宽度和高度
  142. tex.ReadPixels(new Rect(pos.x, pos.y, width, height), 0, 0);
  143. //执行读取操作
  144. tex.Apply();
  145. byte[] bytes = tex.EncodeToPNG();
  146. captureFinished = true;
  147. byteArr = bytes;
  148. }
  149. private void SavePhoto(byte[] bytes)
  150. {
  151. #if UNITY_EDITOR
  152. // pc端保存
  153. string path = Application.dataPath + "/StreamingAssets/" + TimeHelper.ServerNowSecs + curIndex + ".png";
  154. System.IO.File.WriteAllBytes(path, bytes);
  155. Debug.Log("the photo saved in:" + path);
  156. saveFinished = true;
  157. #else
  158. // 手机端
  159. string fileName = "wsj" + TimeHelper.ServerNowSecs + curIndex + ".jpg";
  160. NativeGallery.Permission permission = NativeGallery.SaveImageToGallery(bytes, "Wanshijing", fileName, (success, path) =>
  161. {
  162. if (success)
  163. {
  164. saveFinished = true;
  165. }
  166. else
  167. {
  168. }
  169. });
  170. #endif
  171. }
  172. }
  173. }