123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- using System;
- using System.Collections;
- using System.IO;
- using FairyGUI;
- using UI.DressUp;
- using UnityEngine;
- namespace GFGGame
- {
- public class PhotographSaveView : BaseView
- {
- private UI_PhotographSaveUI _ui;
- protected override void OnInit()
- {
- base.OnInit();
- packageName = UI_PhotographUI.PACKAGE_NAME;
- _ui = UI_PhotographSaveUI.Create();
- viewCom = _ui.target;
- isfullScreen = true;
- _ui.m_btnClose.onClick.Add(this.Hide);
- _ui.m_btnSave.onClick.Add(this.OnClickBtnSave);
- }
- protected override void OnShown()
- {
- base.OnShown();
- Texture2D tex = this.viewData as Texture2D;
- _ui.m_imgRes.texture = new NTexture(tex);
- float width = _ui.m_imgBorder.width;
- float height = width * tex.height / tex.width;
- _ui.m_imgRes.SetSize(width, height);
- _ui.m_imgBorder.SetSize(width, height + 12);
- }
- private void OnClickBtnSave()
- {
- string path = Application.persistentDataPath;
- Debug.Log("文件路径path:" + path);
- #if UNITY_ANDROID
- int index = Application.persistentDataPath.IndexOf("Android");
- path = index < 0 ? path : Application.persistentDataPath.Substring(0, index);
- // path = "file://sdcard/DCIM/";
- #endif
- //判断目录是否存在,不存在则会创建目录
- if (!Directory.Exists(path))
- {
- try
- {
- Directory.CreateDirectory(path);
- }
- catch (Exception exception)
- {
- throw new Exception("创建文件夹失败, error:" + exception.Message);
- }
- }
- string fileName = DateUtils.Instance.GetCurTime() + ".png";
- Debug.Log("文件路径:" + path);
- Texture2D tex = this.viewData as Texture2D;
- byte[] bytes = tex.EncodeToPNG();//将纹理数据,转化成一个png图片
- File.WriteAllBytes(path + "/Pictures/" + fileName, bytes);
- Timers.inst.StartCoroutine(GetSaveState(path));// ();
- // string[] paths = new string[1];
- // paths[0] = path;
- // ScanFile(paths);
- }
- private IEnumerator GetSaveState(string path)
- {
- yield return new WaitForEndOfFrame();
- if (File.Exists(path))
- {
- PromptController.Instance.ShowFloatTextPrompt("保存成功");
- }
- }
- //刷新图片,显示到相册中
- void ScanFile(string[] path)
- {
- using (AndroidJavaClass PlayerActivity = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
- {
- AndroidJavaObject playerActivity = PlayerActivity.GetStatic<AndroidJavaObject>("currentActivity");
- using (AndroidJavaObject Conn = new AndroidJavaObject("android.media.MediaScannerConnection", playerActivity, null))
- {
- Conn.CallStatic("scanFile", playerActivity, path, null, null);
- }
- }
- }
- protected override void OnHide()
- {
- base.OnHide();
- }
- public override void Dispose()
- {
- base.Dispose();
- }
- }
- }
|