Uniyt3d截屏
按住K; V截取游戲
using UnityEngine; using System.Collections; using System.IO;// Screen Recorder will save individual images of active scene in any resolution and of a specific image format // including raw, jpg, png, and ppm. Raw and PPM are the fastest image formats for saving. // // You can compile these images into a video using ffmpeg: // ffmpeg -i screen_3840x2160_%d.ppm -y test.avipublic class ScreenRecorder : MonoBehaviour {// 4k = 3840 x 2160 1080p = 1920 x 1080public int captureWidth = 1920;public int captureHeight = 1080;// optional game object to hide during screenshots (usually your scene canvas hud)public GameObject hideGameObject;// optimize for many screenshots will not destroy any objects so future screenshots will be fastpublic bool optimizeForManyScreenshots = true;// configure with raw, jpg, png, or ppm (simple raw format)public enum Format { RAW, JPG, PNG, PPM };public Format format = Format.PPM;// folder to write output (defaults to data path)public string folder;// private vars for screenshotprivate Rect rect;private RenderTexture renderTexture;private Texture2D screenShot;private int counter = 0; // image #// commandsprivate bool captureScreenshot = false;private bool captureVideo = false;// create a unique filename using a one-up variableprivate string uniqueFilename(int width, int height){// if folder not specified by now use a good defaultif (folder == null || folder.Length == 0){folder = Application.dataPath;if (Application.isEditor){// put screenshots in folder above asset path so unity doesn't index the filesvar stringPath = folder; // + "/..";folder = Path.GetFullPath(stringPath);}folder += "/screenshots";// make sure directoroy exists System.IO.Directory.CreateDirectory(folder);// count number of files of specified format in folderstring mask = string.Format("screen_{0}x{1}*.{2}", width, height, format.ToString().ToLower());counter = Directory.GetFiles(folder, mask, SearchOption.TopDirectoryOnly).Length;}// use width, height, and counter for unique file namevar filename = string.Format("{0}/screen_{1}x{2}_{3}.{4}", folder, width, height, counter, format.ToString().ToLower());// up counter for next call++counter;// return unique filenamereturn filename;}public void CaptureScreenshot(){captureScreenshot = true;}void Update(){// check keyboard 'k' for one time screenshot capture and holding down 'v' for continious screenshotscaptureScreenshot |= Input.GetKeyDown("k");captureVideo = Input.GetKey("v");if (captureScreenshot || captureVideo){captureScreenshot = false;// hide optional game object if setif (hideGameObject != null) hideGameObject.SetActive(false);// create screenshot objects if neededif (renderTexture == null){// creates off-screen render texture that can rendered intorect = new Rect(0, 0, captureWidth, captureHeight);renderTexture = new RenderTexture(captureWidth, captureHeight, 24);screenShot = new Texture2D(captureWidth, captureHeight, TextureFormat.RGB24, false);}// get main camera and manually render scene into rtCamera camera = this.GetComponent<Camera>(); // NOTE: added because there was no reference to camera in original script; must add this script to Cameracamera.targetTexture = renderTexture;camera.Render();// read pixels will read from the currently active render texture so make our offscreen // render texture active and then read the pixelsRenderTexture.active = renderTexture;screenShot.ReadPixels(rect, 0, 0);// reset active camera texture and render texturecamera.targetTexture = null;RenderTexture.active = null;// get our unique filenamestring filename = uniqueFilename((int)rect.width, (int)rect.height);// pull in our file header/data bytes for the specified image format (has to be done from main thread)byte[] fileHeader = null;byte[] fileData = null;if (format == Format.RAW){fileData = screenShot.GetRawTextureData();}else if (format == Format.PNG){fileData = screenShot.EncodeToPNG();}else if (format == Format.JPG){fileData = screenShot.EncodeToJPG();}else // ppm {// create a file header for ppm formatted filestring headerStr = string.Format("P6\n{0} {1}\n255\n", rect.width, rect.height);fileHeader = System.Text.Encoding.ASCII.GetBytes(headerStr);fileData = screenShot.GetRawTextureData();}// create new thread to save the image to file (only operation that can be done in background)new System.Threading.Thread(() =>{// create file and write optional header with image bytesvar f = System.IO.File.Create(filename);if (fileHeader != null) f.Write(fileHeader, 0, fileHeader.Length);f.Write(fileData, 0, fileData.Length);f.Close();Debug.Log(string.Format("Wrote screenshot {0} of size {1}", filename, fileData.Length));}).Start();// unhide optional game object if setif (hideGameObject != null) hideGameObject.SetActive(true);// cleanup if neededif (optimizeForManyScreenshots == false){Destroy(renderTexture);renderTexture = null;screenShot = null;}}} } View Code?
轉載于:https://www.cnblogs.com/JimmyCode/p/6635978.html
總結
                            
                        - 上一篇: iOS ----------怎么修改xc
 - 下一篇: Hibernate-级联操作