PhotographSaveView.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System.IO;
  2. using FairyGUI;
  3. using UI.DressUp;
  4. using UnityEngine;
  5. namespace GFGGame
  6. {
  7. public class PhotographSaveView : BaseView
  8. {
  9. private UI_PhotographSaveUI _ui;
  10. protected override void OnInit()
  11. {
  12. base.OnInit();
  13. packageName = UI_PhotographUI.PACKAGE_NAME;
  14. _ui = UI_PhotographSaveUI.Create();
  15. viewCom = _ui.target;
  16. isfullScreen = true;
  17. _ui.m_btnClose.onClick.Add(this.Hide);
  18. _ui.m_btnSave.onClick.Add(this.OnClickBtnSave);
  19. }
  20. protected override void OnShown()
  21. {
  22. base.OnShown();
  23. Texture2D tex = this.viewData as Texture2D;
  24. _ui.m_imgRes.texture = new NTexture(tex);
  25. float width = _ui.m_imgBorder.width;
  26. float height = width * tex.height / tex.width;
  27. _ui.m_imgRes.SetSize(width, height);
  28. _ui.m_imgBorder.SetSize(width, height + 12);
  29. }
  30. private void OnClickBtnSave()
  31. {
  32. Texture2D tex = this.viewData as Texture2D;
  33. byte[] bytes = tex.EncodeToPNG();//将纹理数据,转化成一个png图片
  34. string path = Application.persistentDataPath + "/wanshijing";
  35. #if UNITY_ANDROID
  36. path = "/sdcard/DCIM/wanshijing";
  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("文件路径:" + fileName);
  46. //存图片
  47. System.IO.File.WriteAllBytes(fileName, bytes);//写入数据
  48. PromptController.Instance.ShowFloatTextPrompt("保持成功");
  49. }
  50. protected override void OnHide()
  51. {
  52. base.OnHide();
  53. }
  54. public override void Dispose()
  55. {
  56. base.Dispose();
  57. }
  58. }
  59. }