using UnityEngine; using System.Collections; using System.IO; using LuaInterface; public static class TakeScreenShot { static string screenshotpath, thumbpath; static Texture2D screenShot; static Texture2D thumb; static int w, h; static TakeScreenShot() { screenshotpath = Application.persistentDataPath + "/screenshot.jpg"; thumbpath = Application.persistentDataPath + "/screenshot_thumb.jpg"; } public static void Take(LuaFunction callback) { if (FileExist(screenshotpath)) { File.Delete(screenshotpath); } if (FileExist(screenshotpath)) { File.Delete(screenshotpath); } GameApplication.Instance.StartCoroutine(CheckFile(callback)); } static IEnumerator CheckFile(LuaFunction callback) { if (FileExist(screenshotpath) && FileExist(thumbpath)) { yield return new WaitForEndOfFrame(); GameApplication.Instance.StartCoroutine(CheckFile(callback)); } else { yield return new WaitForEndOfFrame(); screenShot = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false); Rect r = new Rect(0, 0, Screen.width, Screen.height); screenShot.ReadPixels(r, 0, 0, false); screenShot.Apply(); if (Screen.orientation == ScreenOrientation.Landscape) { //480 w = 240; h = (int)(240f * (float)Screen.height / (float)Screen.width); } else { //270 w = 134; h = (int)(134f * (float)Screen.height / (float)Screen.width); } thumb = ScaleTexture(screenShot, w, h); byte[] screenshotdata = screenShot.EncodeToJPG(80); byte[] thumbdata = thumb.EncodeToJPG(60); File.WriteAllBytes(screenshotpath, screenshotdata); File.WriteAllBytes(thumbpath, thumbdata); if (callback != null) { callback.Call(); callback.Dispose(); } } } static Texture2D ScaleTexture(Texture2D source, int targetWidth, int targetHeight) { Texture2D result = new Texture2D(targetWidth, targetHeight, source.format, false); //float incX = (1.0f / (float)targetWidth); //float incY = (1.0f / (float)targetHeight); for (int i = 0; i < result.height; ++i) { for (int j = 0; j < result.width; ++j) { Color newColor = source.GetPixelBilinear((float)j / (float)result.width, (float)i / (float)result.height); result.SetPixel(j, i, newColor); } } result.Apply(); return result; } static bool FileExist(string path) { return File.Exists(path); } }