Unity安卓截屏保存本地相冊

 

using UnityEngine;
using System.Collections;
using System;
using System.IO;
using UnityEngine.UI;

/// <summary>
/// 截圖保存安卓手機相冊
/// </summary>
public class CaptureScreenshotMgr : MonoBehaviour
{
    public Text text;
    string _name = "";

    /// <summary>
    /// 保存截屏圖片,並且刷新相冊 Android
    /// </summary>
    /// <param name="name">若空就按照時間命名</param>
    public void CaptureScreenshot()
    {
        _name = "";
        _name = "Screenshot_" + GetCurTime() + ".png";


#if UNITY_STANDALONE_WIN      //PC平臺
       // 編輯器下
       // string path = Application.persistentDataPath + "/" + _name;       
        string path = Application.dataPath + "/" + _name;
        ScreenCapture.CaptureScreenshot(path, 0);
        Debug.Log("圖片保存地址" + path);

#elif UNITY_ANDROID     //安卓平臺
        //Android版本
        StartCoroutine(CutImage(_name));
        //在手機上顯示路徑
        // text.text = "圖片保存地址" + Application.persistentDataPath.Substring(0, Application.persistentDataPath.IndexOf("Android")) + "/DCIM/Camera/" + _name;
        text.text = "圖片保存地址" + Application.persistentDataPath.Substring(0, Application.persistentDataPath.IndexOf("Android")) + "/截屏/" + _name;
#endif
    }
    //截屏並保存
    IEnumerator CutImage(string name)
    {
        //圖片大小  
        Texture2D tex = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, true);
        yield return new WaitForEndOfFrame();
        tex.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0, true);
        tex.Apply();
        yield return tex;
        byte[] byt = tex.EncodeToPNG();

        string path = Application.persistentDataPath.Substring(0, Application.persistentDataPath.IndexOf("Android"));

        //  File.WriteAllBytes(path + "/DCIM/Camera/" + name, byt);   //保存到  安卓手機的  DCIM/下的Camera   文件夾下
        File.WriteAllBytes(path + "/截屏/" + name, byt);         //保存到安卓手機的 文件管理下面的  《截屏》文件夾下      
        string[] paths = new string[1];
        paths[0] = path;
        ScanFile(paths);
    }
    //刷新圖片,顯示到相冊中
    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);
            }
        }

    }
    /// <summary>
    /// 獲取當前年月日時分秒,如20181001444
    /// </summary>
    /// <returns></returns>
    string GetCurTime()
    {
        return DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString()
            + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
    }

}

 

打包到安卓手機,寫入的可能會有些慢,過段時間刷新一下就有了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章