PhotographSaveView.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. // path = "file://sdcard/DCIM/";
  40. #endif
  41. //判断目录是否存在,不存在则会创建目录
  42. if (!Directory.Exists(path))
  43. {
  44. try
  45. {
  46. Directory.CreateDirectory(path);
  47. }
  48. catch (Exception exception)
  49. {
  50. throw new Exception("创建文件夹失败, error:" + exception.Message);
  51. }
  52. }
  53. string fileName = DateUtils.Instance.GetCurTime() + ".png";
  54. Debug.Log("文件路径:" + path);
  55. Texture2D tex = this.viewData as Texture2D;
  56. byte[] bytes = tex.EncodeToPNG();//将纹理数据,转化成一个png图片
  57. File.WriteAllBytes(path + "/Pictures/" + fileName, bytes);
  58. Timers.inst.StartCoroutine(GetSaveState(path));// ();
  59. // string[] paths = new string[1];
  60. // paths[0] = path;
  61. // ScanFile(paths);
  62. }
  63. private IEnumerator GetSaveState(string path)
  64. {
  65. yield return new WaitForEndOfFrame();
  66. if (File.Exists(path))
  67. {
  68. PromptController.Instance.ShowFloatTextPrompt("保存成功");
  69. }
  70. }
  71. //刷新图片,显示到相册中
  72. void ScanFile(string[] path)
  73. {
  74. using (AndroidJavaClass PlayerActivity = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
  75. {
  76. AndroidJavaObject playerActivity = PlayerActivity.GetStatic<AndroidJavaObject>("currentActivity");
  77. using (AndroidJavaObject Conn = new AndroidJavaObject("android.media.MediaScannerConnection", playerActivity, null))
  78. {
  79. Conn.CallStatic("scanFile", playerActivity, path, null, null);
  80. }
  81. }
  82. }
  83. protected override void OnHide()
  84. {
  85. base.OnHide();
  86. }
  87. public override void Dispose()
  88. {
  89. base.Dispose();
  90. }
  91. }
  92. }