PhotographSaveView.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Collections;
  3. using System.IO;
  4. using FairyGUI;
  5. using UI.DressUp;
  6. using UnityEngine;
  7. namespace GFGGame
  8. {
  9. public class PhotographSaveView : BaseView
  10. {
  11. private UI_PhotographSaveUI _ui;
  12. protected override void OnInit()
  13. {
  14. base.OnInit();
  15. packageName = UI_PhotographUI.PACKAGE_NAME;
  16. _ui = UI_PhotographSaveUI.Create();
  17. viewCom = _ui.target;
  18. isfullScreen = true;
  19. _ui.m_btnClose.onClick.Add(this.Hide);
  20. _ui.m_btnSave.onClick.Add(this.OnClickBtnSave);
  21. }
  22. protected override void OnShown()
  23. {
  24. base.OnShown();
  25. Texture2D tex = this.viewData as Texture2D;
  26. _ui.m_imgRes.texture = new NTexture(tex);
  27. float width = _ui.m_imgBorder.width;
  28. float height = width * tex.height / tex.width;
  29. _ui.m_imgRes.SetSize(width, height);
  30. _ui.m_imgBorder.SetSize(width, height + 12);
  31. }
  32. private void OnClickBtnSave()
  33. {
  34. string path = Application.persistentDataPath;
  35. Debug.Log("文件路径path:" + path);
  36. #if UNITY_ANDROID
  37. int index = Application.persistentDataPath.IndexOf("Android");
  38. path = index < 0 ? path : Application.persistentDataPath.Substring(0, index);
  39. #endif
  40. //判断目录是否存在,不存在则会创建目录
  41. if (!Directory.Exists(path))
  42. {
  43. try
  44. {
  45. Directory.CreateDirectory(path);
  46. }
  47. catch (Exception exception)
  48. {
  49. throw new Exception("创建文件夹失败, error:" + exception.Message);
  50. }
  51. }
  52. string fileName = DateUtils.Instance.GetCurTime() + ".png";
  53. Debug.Log("文件路径:" + path);
  54. Texture2D tex = this.viewData as Texture2D;
  55. byte[] bytes = tex.EncodeToPNG();//将纹理数据,转化成一个png图片
  56. File.WriteAllBytes(path + "/Pictures/Screenshots/" + fileName, bytes);
  57. Timers.inst.StartCoroutine(GetSaveState(path));// ();
  58. string[] paths = new string[1];
  59. paths[0] = path;
  60. ScanFile(paths);
  61. }
  62. private IEnumerator GetSaveState(string path)
  63. {
  64. yield return new WaitForEndOfFrame();
  65. if (File.Exists(path))
  66. {
  67. PromptController.Instance.ShowFloatTextPrompt("保存成功");
  68. }
  69. }
  70. //刷新图片,显示到相册中
  71. void ScanFile(string[] path)
  72. {
  73. using (AndroidJavaClass PlayerActivity = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
  74. {
  75. AndroidJavaObject playerActivity = PlayerActivity.GetStatic<AndroidJavaObject>("currentActivity");
  76. using (AndroidJavaObject Conn = new AndroidJavaObject("android.media.MediaScannerConnection", playerActivity, null))
  77. {
  78. Conn.CallStatic("scanFile", playerActivity, path, null, null);
  79. }
  80. }
  81. }
  82. protected override void OnHide()
  83. {
  84. base.OnHide();
  85. }
  86. public override void Dispose()
  87. {
  88. base.Dispose();
  89. }
  90. }
  91. }