using UnityEngine; using System.Collections; using System.IO; using System.Text; using System.Collections.Generic; using AX.MessageSystem; public class ScreenShots : MonoBehaviour { public void TheJiePingBtn() { string path = Application.dataPath + @"/ExtendFolder/" + "TheImage"+"/"; string fileDir = Path.GetDirectoryName(path); if (!Directory.Exists(fileDir)) { Directory.CreateDirectory(fileDir); } string Now = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() + System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString(); string filename = Application.dataPath + @"/ExtendFolder/TheImage/" + Now + ".png"; Application.CaptureScreenshot(filename, 0); StartCoroutine(ScreenShot()); //协程调用 } IEnumerator ScreenShot() { yield return new WaitForEndOfFrame(); MessageDispatcher.SendMessage("Operatinghints", (object)"截屏成功"); } public void CaptureScreen(Rect rect) { // 先创建一个的空纹理,大小可根据实现需要来设置 Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false); int width = Screen.width; int height = Screen.height; // 读取屏幕像素信息并存储为纹理数据, screenShot.ReadPixels(rect, 0, 0); screenShot.Apply(); string Now = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() + System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString(); // 然后将这些纹理数据,成一个png图片文件 byte[] bytes = screenShot.EncodeToPNG(); string filename = Application.dataPath + @"/ExtendFolder/TheImage/" + Now + ".png"; System.IO.File.WriteAllBytes(filename, bytes); Debug.Log(string.Format("截屏了一张图片: {0}", filename)); // 最后,我返回这个Texture2d对象,这样我们直接,所这个截图图示在游戏中,当然这个根据自己的需求的。 //return screenShot; } public Animator ani; public string name_; public int Num; string path; public GameObject TiShi; ArrayList NameToArray; public void TheGetAllName()//获取文件下所有文件的名字 { string Path = @"d:\TheImage\"; string[] directoryEntries; NameToArray = new ArrayList(); try { directoryEntries = System.IO.Directory.GetFileSystemEntries(Path); for (int i = 0; i < directoryEntries.Length; i++) { string p = directoryEntries[i]; string[] tempPaths = StringExtention(p, Path); if (tempPaths[1].EndsWith(".png")) { continue; } string[] pathSplit = StringExtention(tempPaths[1], "."); if (pathSplit.Length > 1) { NameToArray.Add(p); } else { // TheGetAllName(string path,) } } } catch { } } public string[] StringExtention(string sourceString, string splitString) { //string tempSourceString = sourceString; List arrlist = new List(); string s = string.Empty; while (sourceString.IndexOf(splitString) > -1) { s = sourceString.Substring(0, sourceString.IndexOf(splitString)); sourceString = sourceString.Substring(sourceString.IndexOf(splitString) + splitString.Length); arrlist.Add(s); } arrlist.Add(sourceString); return arrlist.ToArray(); } }