截屏保存方法
public static IEnumerator ScreenShot(string filePath, string fileName)
{
yield return new WaitForEndOfFrame();
Rect rect = new Rect(0, 0, Screen.width, Screen.height);
Texture2D screenShot = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
screenShot.ReadPixels(rect, 0, 0);
screenShot.Apply();
byte[] bytes = screenShot.EncodeToPNG();
if (!Directory.Exists(filePath))
Directory.CreateDirectory(filePath);
File.WriteAllBytes(filePath + "/" + fileName + ".png", bytes);
#if UNITY_EDITOR
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
#endif
}
以日期为名称调用
public void TakeAScreenShot()
{
string englishTime = DateTime.Now.ToString(); //2000/9/27 1:27:28
string chineseTime = string.Empty;
string[] split1 = englishTime.Split('/'); //2000 - 9 - 27 1:27:28
string[] split2 = split1[2].Split(' '); //27 - 1:27:28
string[] split3 = split2[1].Split(':'); //1 - 27 - 28
chineseTime += split1[0] + "年" + split1[1] + "月" + split2[0] + "日" + split3[0] + "时" + split3[1] + "分" + split3[2] + "秒";
StartCoroutine(UnityHelper.ScreenShot(UIPanel_ScreenShot.M_Path, chineseTime, false));
}
获取截图
private List<Texture2D> m_ScreenShotTextures;
private void GetFilesInFolder(string folderPath)
{
if (Directory.Exists(folderPath))
{
string[] currentFiles = Directory.GetFiles(folderPath);
if (currentFiles.Length > 0)
{
m_ScreenShotTextures = new List<Texture2D>();
foreach (string filePath in currentFiles)
{
#if UNITY_EDITOR
if (filePath.EndsWith("png"))
#endif
m_ScreenShotTextures.Add(ReadTexture(filePath));
}
m_ScreenShotTextures.Reverse();
}
}
SetScreenShot();
}
private Texture2D ReadTexture(string filePath)
{
Texture2D pathTex = new Texture2D(Screen.width, Screen.height);
#if UNITY_EDITOR
string[] split1 = filePath.Split("\\"); //...... - ***.png
#elif UNITY_ANDROID
string[] split1 = filePath.Split("/"); //...... - ***.png
#endif
string[] split2 = split1[split1.Length - 1].Split("."); // *** - png
pathTex.name = split2[0];
byte[] bytes = File.ReadAllBytes(filePath);
pathTex.LoadImage(bytes);
pathTex.Apply();
return pathTex;
}
展示:把上面获取的结果进行展示即可