PhotographSaveView.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. GuideController.TryGuideBtnSave(_ui.m_btnSave);
  32. }
  33. protected override void OnHide()
  34. {
  35. base.OnHide();
  36. }
  37. public override void Dispose()
  38. {
  39. base.Dispose();
  40. }
  41. private void OnClickBtnSave()
  42. {
  43. GuideController.TryCompleteGuide(ConstGuideId.PHOTOGRAPH_GUIDE);
  44. string path = Application.persistentDataPath + "/Pictures/WanshiJing/";
  45. //判断目录是否存在,不存在则会创建目录
  46. if (!Directory.Exists(path))
  47. {
  48. try
  49. {
  50. Directory.CreateDirectory(path);
  51. }
  52. catch (Exception exception)
  53. {
  54. throw new Exception("创建文件夹失败, error:" + exception.Message);
  55. }
  56. }
  57. string fileName = "wsj" + DateUtils.Instance.GetCurTime() + ".jpg";
  58. Texture2D tex = this.viewData as Texture2D;
  59. byte[] bytes = tex.EncodeToJPG();//将纹理数据,转化成一个jpg图片
  60. var filePath = path + fileName;
  61. File.WriteAllBytes(filePath, bytes);
  62. UpdateSystemPhoto(filePath);
  63. }
  64. //调用iOS或Android原生方法保存图片后更新相册.
  65. private void UpdateSystemPhoto(string filePath)
  66. {
  67. #if UNITY_ANDROID
  68. AndroidJavaObject androidJavaObject = new AndroidJavaObject("com.gfg.gfglibrary.SaveImage"); //设置成我们aar库中的签名+类名
  69. androidJavaObject.Call("scanFile", filePath, "已保存至相册"); //这里我们可以设置保存成功弹窗内容
  70. #endif
  71. }
  72. }
  73. }