唔,直接丢在一个公共的静态类就可以了
QDebug:https://blog.csdn.net/qq_17813937/article/details/79801371
QFileOperation:https://blog.csdn.net/qq_17813937/article/details/81190245
/// <summary>
/// 截屏
/// </summary>
/// <param name="action"></param>
public static void CaptureScreenshot(Action action)
{
//QCoroutine 用来开协程的对象
QCoroutine.Start(CaptureScreenshotIEnumerator(action));
}
private static IEnumerator CaptureScreenshotIEnumerator(Action action)
{
var screenshotPath = Application.persistentDataPath + "/Screenshot.jpg";
QFileOperation.DeleteFile(screenshotPath);
ScreenCapture.CaptureScreenshot("Screenshot.jpg");
yield return new WaitForSeconds(1f);
#if UNITY_ANDROID || UNITY_IPHONE
//添加到相册
string path = Application.persistentDataPath.Substring(0, Application.persistentDataPath.IndexOf("Android"));
path += "DCIM/Camera";
QFileOperation.CreateDirctory(path);
var filePath = string.Format("{0}/石现科技{1}.jpg", path, QGlobalFun.GetCurTime());
var texture = QFileOperation.ReadTexture2D(screenshotPath);
QDebug.Log("读取图片");
QFileOperation.WriteBytes(filePath, texture.EncodeToJPG());
QDebug.Log("写入相册 "+filePath);
ScanFile(new string[] { filePath });
QDebug.Log("刷新相册");
GameObject.Destroy(texture);
QFileOperation.DeleteFile(screenshotPath);
#else
#endif
action();
}
/// <summary>
/// 刷新相册
/// </summary>
/// <param name="path"></param>
public static 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);
}
}
}