PoemPhotoSaveView.cs 6.5 KB

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