/* * Snapshot currnet game screen * Reference : https://answers.unity.com/questions/22954/how-to-save-a-picture-take-screenshot-from-a-camer.html */ private void GetScreenShot(Enum_ScreenShotFor en, out byte[] screenShotBytes) { int resWidth; int resHeight; // setting resolution if (en == Enum_ScreenShotFor.SaveFile) { resWidth = saveFileResWitdh; resHeight = saveFileResHeight; } else if (en == Enum_ScreenShotFor.LoadingScene) { resHeight = Screen.height; resWidth = Screen.width; } else { resHeight = saveFileResWitdh; // default value resWidth = saveFileResHeight; } // Create renderTexture & Render RenderTexture rt = new RenderTexture(resWidth, resHeight, 24); m_screenShotCamera.targetTexture = rt; // camera renderTarget => renderTexture m_screenShotCamera.Render(); m_screenShotCamera.targetTexture = null; // camera renderTarget => Screen // Create Texture2D & Save texture as png file RenderTexture.active = rt; // ReadPixel target = renderTextrue Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false); screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0); RenderTexture.active = null; // JC: added to avoid errors Destroy(rt); screenShotBytes = screenShot.EncodeToPNG(); return; /* * string filename = ScreenShotName(resWidth, resHeight); * System.IO.File.WriteAllBytes(filename, bytes); * Debug.Log(string.Format("Took screenshot to: {0}", filename)); * takeHiResShot = false; */ }
private void GetScreenShotTexture2D(Enum_ScreenShotFor en, out Texture2D texture2D) { int resWidth; int resHeight; // setting resolution if (en == Enum_ScreenShotFor.SaveFile) { resWidth = saveFileResWitdh; resHeight = saveFileResHeight; } else if (en == Enum_ScreenShotFor.LoadingScene) { resHeight = Screen.height; resWidth = Screen.width; } else { resHeight = saveFileResWitdh; // default value resWidth = saveFileResHeight; } // Create renderTexture & Render RenderTexture rt = new RenderTexture(resWidth, resHeight, 24); m_screenShotCamera.targetTexture = rt; // camera renderTarget => renderTexture m_screenShotCamera.Render(); m_screenShotCamera.targetTexture = null; // camera renderTarget => Screen // Create Texture2D & Save texture as png file RenderTexture.active = rt; // ReadPixel target = renderTextrue texture2D = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false); texture2D.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0); RenderTexture.active = null; // JC: added to avoid errors Destroy(rt); return; }