PhotographSaveView.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System.Collections;
  2. using System.IO;
  3. using FairyGUI;
  4. using UI.DressUp;
  5. using UnityEngine;
  6. namespace GFGGame
  7. {
  8. public class PhotographSaveView : BaseView
  9. {
  10. private UI_PhotographSaveUI _ui;
  11. protected override void OnInit()
  12. {
  13. base.OnInit();
  14. packageName = UI_PhotographUI.PACKAGE_NAME;
  15. _ui = UI_PhotographSaveUI.Create();
  16. viewCom = _ui.target;
  17. isfullScreen = true;
  18. _ui.m_btnClose.onClick.Add(this.Hide);
  19. _ui.m_btnSave.onClick.Add(this.OnClickBtnSave);
  20. }
  21. protected override void OnShown()
  22. {
  23. base.OnShown();
  24. Texture2D tex = this.viewData as Texture2D;
  25. _ui.m_imgRes.texture = new NTexture(tex);
  26. float width = _ui.m_imgBorder.width;
  27. float height = width * tex.height / tex.width;
  28. _ui.m_imgRes.SetSize(width, height);
  29. _ui.m_imgBorder.SetSize(width, height + 12);
  30. }
  31. private void OnClickBtnSave()
  32. {
  33. string path = Application.persistentDataPath;
  34. #if UNITY_ANDROID
  35. path = path + "/万世镜";
  36. #endif
  37. //判断目录是否存在,不存在则会创建目录
  38. if (!Directory.Exists(path))
  39. {
  40. Directory.CreateDirectory(path);
  41. }
  42. string fileName = DateUtils.Instance.GetCurTime() + ".png";
  43. path = path + "/" + fileName;
  44. Debug.Log("文件路径:" + path);
  45. Texture2D tex = this.viewData as Texture2D;
  46. byte[] bytes = tex.EncodeToPNG();//将纹理数据,转化成一个png图片
  47. File.WriteAllBytes(path, bytes);
  48. Timers.inst.StartCoroutine(GetSaveState(path));// ();
  49. }
  50. private IEnumerator GetSaveState(string path)
  51. {
  52. yield return new WaitForEndOfFrame();
  53. if (File.Exists(path))
  54. {
  55. PromptController.Instance.ShowFloatTextPrompt("保存成功");
  56. }
  57. }
  58. protected override void OnHide()
  59. {
  60. base.OnHide();
  61. }
  62. public override void Dispose()
  63. {
  64. base.Dispose();
  65. }
  66. }
  67. }