PoemPhotoSaveView.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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[index]);
  70. await Task.Delay(50);
  71. captureFinished = false;
  72. Timers.inst.StartCoroutine(CaptureByUI());
  73. while (!captureFinished)
  74. {
  75. await Task.Delay(17);
  76. }
  77. bytes = byteArr;
  78. }
  79. saveFinished = false;
  80. SavePhoto(bytes);
  81. while (!saveFinished)
  82. {
  83. await Task.Delay(17);
  84. }
  85. ++curIndex;
  86. UpdateProgress(curIndex, savePhoto.listChoose.Count);
  87. if (curIndex == savePhoto.listChoose.Count)
  88. {
  89. PromptController.Instance.ShowFloatTextPrompt("已全部保存至相册!");
  90. Hide();
  91. }
  92. }
  93. }
  94. private int GetPhotoListIndex(List<PoemPhotoData> photoInfos, long pictureID)
  95. {
  96. for (int i = 0; i < photoInfos.Count; i++)
  97. {
  98. if (photoInfos[i].PictureId == pictureID)
  99. {
  100. return i;
  101. }
  102. }
  103. return -1;
  104. }
  105. private void UpdateProgress(int cur, int max)
  106. {
  107. _ui.m_progress.value = cur;
  108. _ui.m_progress.max = max;
  109. }
  110. private void RefreshPhoto(PoemPhotoData data)
  111. {
  112. UI_ListPhotoPreviewItem item = UI_ListPhotoPreviewItem.Proxy(_ui.m_personalPhoto.target);
  113. item.m_comPhoto.m_loaPhoto.texture = data.Ntexture;
  114. UI_ListPhotoPreviewItem.ProxyEnd();
  115. }
  116. private void RefreshTravelPhoto(PoemPhotoData data)
  117. {
  118. UI_ComPostcard item = UI_ComPostcard.Proxy(_ui.m_travelPhoto.target);
  119. PoemPhotoData photoData = data;
  120. TravelLoactionCfg loactionCfg = TravelLoactionCfgArray.Instance.GetCfg(photoData.TravelLocationId);
  121. item.m_comTravel.m_loaBg.url = ResPathUtil.GetTravelBgPath(loactionCfg.res);
  122. item.m_txtLocationName.text = loactionCfg.name;
  123. item.m_txtTime.text = TimeUtil.FormattingTimeTo_yyyMMdd1(photoData.CreationTime);
  124. item.m_comTravel.m_loaRole.url = "";
  125. if (photoData.TravelSuitId > 0)
  126. {
  127. TravelSuitCfg travelSuitCfg = TravelSuitCfgArray.Instance.GetCfg(photoData.TravelSuitId);
  128. item.m_comTravel.m_loaRole.url = ResPathUtil.GetTravelRolePath(travelSuitCfg.reourcesArr[photoData.SuitResIndex]);
  129. item.m_comTravel.m_loaRole.SetXY(loactionCfg.positionsArr[photoData.PositionIndex][0], loactionCfg.positionsArr[photoData.PositionIndex][1]);
  130. }
  131. UI_ComPostcard.ProxyEnd();
  132. }
  133. private IEnumerator CaptureByUI()
  134. {
  135. //等待帧画面渲染结束
  136. yield return new WaitForEndOfFrame();
  137. int width = (int)(_ui.m_saveImg.width * UIContentScaler.scaleFactor);
  138. int height = (int)(_ui.m_saveImg.height * UIContentScaler.scaleFactor);
  139. Vector2 pos = _ui.m_saveImg.LocalToGlobal(Vector2.zero);
  140. pos.y = Screen.height - pos.y - height;
  141. Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
  142. //从屏幕读取像素, leftBtmX/leftBtnY 是读取的初始位置,width、height是读取像素的宽度和高度
  143. tex.ReadPixels(new Rect(pos.x, pos.y, width, height), 0, 0);
  144. //执行读取操作
  145. tex.Apply();
  146. byte[] bytes = tex.EncodeToPNG();
  147. captureFinished = true;
  148. byteArr = bytes;
  149. }
  150. private void SavePhoto(byte[] bytes)
  151. {
  152. #if UNITY_EDITOR
  153. // pc端保存
  154. string path = Application.dataPath + "/StreamingAssets/" + TimeHelper.ServerNowSecs + curIndex + ".png";
  155. System.IO.File.WriteAllBytes(path, bytes);
  156. Debug.Log("the photo saved in:" + path);
  157. saveFinished = true;
  158. #else
  159. // 手机端
  160. string fileName = "wsj" + TimeHelper.ServerNowSecs + curIndex + ".jpg";
  161. NativeGallery.Permission permission = NativeGallery.SaveImageToGallery(bytes, "Wanshijing", fileName, (success, path) =>
  162. {
  163. if (success)
  164. {
  165. saveFinished = true;
  166. }
  167. else
  168. {
  169. }
  170. });
  171. #endif
  172. }
  173. }
  174. }