PhotographSaveView.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. Debug.Log("文件路径path:" + path);
  35. #if UNITY_ANDROID
  36. path = Application.persistentDataPath.IndexOf("Android") > 0 ? path.Substring(0, Application.persistentDataPath.IndexOf("Android")) + "/Pictures/万世镜/" : path + "/Pictures/万世镜/";
  37. #endif
  38. //判断目录是否存在,不存在则会创建目录
  39. if (!Directory.Exists(path))
  40. {
  41. Directory.CreateDirectory(path);
  42. }
  43. string fileName = DateUtils.Instance.GetCurTime() + ".png";
  44. path = path + fileName;
  45. Debug.Log("文件路径:" + path);
  46. Texture2D tex = this.viewData as Texture2D;
  47. byte[] bytes = tex.EncodeToPNG();//将纹理数据,转化成一个png图片
  48. // string path = Application.persistentDataPath.Substring(0, Application.persistentDataPath.IndexOf("Android"));
  49. File.WriteAllBytes(path, bytes);
  50. Timers.inst.StartCoroutine(GetSaveState(path));// ();
  51. }
  52. private IEnumerator GetSaveState(string path)
  53. {
  54. yield return new WaitForEndOfFrame();
  55. if (File.Exists(path))
  56. {
  57. PromptController.Instance.ShowFloatTextPrompt("保存成功");
  58. }
  59. }
  60. protected override void OnHide()
  61. {
  62. base.OnHide();
  63. }
  64. public override void Dispose()
  65. {
  66. base.Dispose();
  67. }
  68. }
  69. }