PhotographSaveView.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 = "/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. var filePath = path + fileName;
  58. File.WriteAllBytes(filePath, bytes);
  59. //GetSaveState(path);
  60. savePngAndUpdate(filePath);
  61. }
  62. //调用iOS或Android原生方法保存图片后更新相册.
  63. private void savePngAndUpdate(string fileName)
  64. {
  65. #if UNITY_ANDROID
  66. GetAndroidJavaObject().Call("scanFile", fileName, "保存成功辣٩(๑>◡<๑)۶ "); //这里我们可以设置保存成功弹窗内容
  67. #endif
  68. }
  69. //用于获取Android原生方法类对象
  70. private AndroidJavaObject GetAndroidJavaObject()
  71. {
  72. return new AndroidJavaObject("com.gfg.gfglibrary.SaveImage"); //设置成我们aar库中的签名+类名
  73. }
  74. private void GetSaveState(string path)
  75. {
  76. Timers.inst.Add(1, 10, (param) =>
  77. {
  78. if (File.Exists(path))
  79. {
  80. string[] paths = { path };
  81. ScanFile(paths);
  82. PromptController.Instance.ShowFloatTextPrompt("保存成功");
  83. }
  84. });
  85. }
  86. //刷新图片,显示到相册中
  87. void ScanFile(string[] path)
  88. {
  89. using (AndroidJavaClass PlayerActivity = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
  90. {
  91. AndroidJavaObject playerActivity = PlayerActivity.GetStatic<AndroidJavaObject>("currentActivity");
  92. using (AndroidJavaObject Conn = new AndroidJavaObject("android.media.MediaScannerConnection", playerActivity, null))
  93. {
  94. Conn.CallStatic("scanFile", playerActivity, path, null, null);
  95. }
  96. }
  97. }
  98. protected override void OnHide()
  99. {
  100. base.OnHide();
  101. }
  102. public override void Dispose()
  103. {
  104. base.Dispose();
  105. }
  106. }
  107. }