66 lines
2.0 KiB
C#
66 lines
2.0 KiB
C#
|
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.IO;
|
|||
|
using UnityEngine;
|
|||
|
|
|||
|
namespace Thousandto.Code.Logic
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// 图片保存类
|
|||
|
/// </summary>
|
|||
|
public class CaptureScreenUtils
|
|||
|
{
|
|||
|
|
|||
|
private static CaptureScreenUtils _instance = null;
|
|||
|
|
|||
|
public static CaptureScreenUtils GetInstances()
|
|||
|
{
|
|||
|
if (_instance == null)
|
|||
|
{
|
|||
|
_instance = new CaptureScreenUtils();
|
|||
|
}
|
|||
|
return _instance;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 文件的保存路径
|
|||
|
/// </summary>
|
|||
|
public string Destination { get; private set; }
|
|||
|
|
|||
|
public string SaveCaptureScreenshot(string fileName)
|
|||
|
{
|
|||
|
string destination = "";
|
|||
|
//等待渲染线程结束
|
|||
|
//yield return new WaitForEndOfFrame();
|
|||
|
Rect rect = new Rect(0, 0, Screen.width, Screen.height);
|
|||
|
//初始化Texture2D
|
|||
|
Texture2D mTexture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
|
|||
|
//读取屏幕像素信息并存储为纹理数据
|
|||
|
mTexture.ReadPixels(rect, 0, 0);
|
|||
|
//应用
|
|||
|
mTexture.Apply();
|
|||
|
destination = SavePicture(fileName, mTexture);
|
|||
|
Destination = destination;
|
|||
|
return destination;
|
|||
|
}
|
|||
|
|
|||
|
//保存图片到相册
|
|||
|
public string SavePicture(string fileName, Texture2D tex)
|
|||
|
{
|
|||
|
byte[] bytes = tex.EncodeToJPG(100);
|
|||
|
string filename = string.Format("{0}.jpg", fileName);
|
|||
|
string destination = Application.persistentDataPath + "/FuncellScreenshot";
|
|||
|
if (!Directory.Exists(destination))
|
|||
|
{
|
|||
|
Directory.CreateDirectory(destination);
|
|||
|
}
|
|||
|
|
|||
|
destination = destination + "/" + filename;
|
|||
|
Destination = destination;
|
|||
|
//保存文件
|
|||
|
File.WriteAllBytes(destination, bytes);
|
|||
|
return destination;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|