using System.Collections;
using UnityEngine;
using System.IO;
using UnityEngine.Networking;
public class ScreenShotTest : MonoBehaviour
{
private string path = string.Empty;
void Start()
{
//windows 不同平臺各異 可參考 https://blog.csdn.net/linxinfa/article/details/51679528
path = Application.dataPath+"/ScreenShot";
if(!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
}
private void OnGUI()
{
if(GUILayout.Button("截取全屏",GUILayout.Width(80), GUILayout.Width(200)))
{
if (!CanShoot) return;
ScreenCapture.CaptureScreenshot(GetFileName());
Refresh();
}
if (GUILayout.Button("截取主相機選取大小", GUILayout.Width(80), GUILayout.Width(200)))
{
if (!CanShoot) return;
StartCoroutine(ShotByRect());
}
GUILayout.Box(texture, GUILayout.Width(500), GUILayout.Height(500));
if (GUILayout.Button("刪除截圖", GUILayout.Width(80), GUILayout.Width(200)))
{
DeleteShot();
}
}
private bool _canShot = false;
private float _lastShotTime = 0;
private bool CanShoot
{
get {
if (Time.time - _lastShotTime > 0.5f)
{
_canShot = true;
_lastShotTime = Time.time;
}else _canShot = false;
return _canShot;
}
}
private string lastFileName = string.Empty;
/// <summary>
/// 獲取當前截圖名
/// </summary>
/// <returns></returns>
private string GetFileName()
{
var sTime = System.DateTime.Now;
var _picName = AddEmpty(sTime.Day) + AddEmpty(sTime.Hour) + AddEmpty(sTime.Minute) + AddEmpty(sTime.Second) + AddEmpty(sTime.Millisecond, DefaultLenth.Three) + ".png";
lastFileName = path + "/ScreenShot_" + _picName;
return lastFileName;
}
/// <summary>
/// 使截圖名稱,長度保持一致
/// </summary>
/// <param name="curvalue"></param>
/// <param name="defaultLenth"></param>
/// <returns></returns>
private string AddEmpty(int curvalue, DefaultLenth defaultLenth = DefaultLenth.Two)
{
var txt = curvalue.ToString();
int txt_length = txt.Length;
if (defaultLenth == DefaultLenth.Two)
{
if (txt_length == 1) txt = "0" + txt;
}else{
if (txt_length == 1) txt = "00" + txt;
else if (txt_length == 2) txt = "0" + txt;
}
return txt;
}
private void Refresh()
{
StartCoroutine(_Refresh());
}
private IEnumerator _Refresh()
{
yield return new WaitForSeconds(0.1f);//全屏截取存儲等待,這裏統一使用
RefreshAsset();
StartCoroutine(GetTexture());
}
/// <summary>
/// 刷新截圖目錄
/// </summary>
private void RefreshAsset()
{
#if UNITY_EDITOR
UnityEditor.AssetDatabase.Refresh();
#endif
}
private Rect rect = new Rect(700, 300, 500, 500);
/// <summary>
/// 截取主相機設定大小
/// </summary>
/// <returns></returns>
private IEnumerator ShotByRect()
{
yield return new WaitForEndOfFrame();
Texture2D texture2D = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
texture2D.ReadPixels(rect, 0, 0);
texture2D.Apply();
File.WriteAllBytes(GetFileName(), texture2D.EncodeToPNG());
Refresh();
}
private Texture texture;
/// <summary>
/// 讀取當前截圖
/// </summary>
/// <returns></returns>
private IEnumerator GetTexture()
{
UnityWebRequest uwr = new UnityWebRequest("file://" + lastFileName);
DownloadHandlerTexture handleTexture = new DownloadHandlerTexture(true);
uwr.downloadHandler = handleTexture;
yield return uwr.SendWebRequest();
if(uwr.isDone)
{
texture = handleTexture.texture;
}else{
texture = null;
}
if (uwr.isNetworkError) Debug.Log("<color=red> == NetworkError == +</color>");
else if (uwr.isHttpError) Debug.Log("<color=red> == HttpError == +</color>");
}
/// <summary>
/// 刪除截圖
/// </summary>
private void DeleteShot()
{
if (!Directory.Exists(path)) return;
var info = Directory.CreateDirectory(path);
var files= info.GetFiles();
for (int i = 0; i < files.Length; i++)
{
string filePath = path +"/"+ files[i].Name;
File.Delete(filePath);
}
RefreshAsset();
}
enum DefaultLenth
{
Two,
Three
}
}
參考: https://blog.csdn.net/qinyuanpei/article/details/39185195