PhotographSaveView.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 = Application.persistentDataPath.IndexOf("Android") > 0 ? path.Substring(0, Application.persistentDataPath.IndexOf("Android")) : path + "/Pictures/万世镜";
  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. // string path = Application.persistentDataPath.Substring(0, Application.persistentDataPath.IndexOf("Android"));
  48. File.WriteAllBytes(path, bytes);
  49. Timers.inst.StartCoroutine(GetSaveState(path));// ();
  50. }
  51. private IEnumerator GetSaveState(string path)
  52. {
  53. yield return new WaitForEndOfFrame();
  54. if (File.Exists(path))
  55. {
  56. PromptController.Instance.ShowFloatTextPrompt("保存成功");
  57. }
  58. }
  59. protected override void OnHide()
  60. {
  61. base.OnHide();
  62. }
  63. public override void Dispose()
  64. {
  65. base.Dispose();
  66. }
  67. }
  68. }