"Basic" unity captures only scene content when capturing

Source: Internet
Author: User

In the game screenshot, we need to intercept only the scene content, not including the Ngui control, implementing the script as follows (only the contents of Gamecamera and Bgcamera are intercepted):

usingUnityengine;usingSystem.Collections; Public classCapturetest:monobehaviour { PublicCamera Gamecamera;  PublicCamera Bgcamera; //Use this for initialization    voidStart () {}//Update is called once per frame    voidUpdate () {}voidOnClick () {rect rect=NewRect (0,0, Screen.width,screen.height);    Capturecamera (Gamecamera, Bgcamera, rect); } texture2d Capturecamera (Camera gamecamera,camera bgcamera, rect rect) {//Create a Rendertexture objectRendertexture RT =NewRendertexture ((int) Rect.width, (int) Rect.height,0); //set up a targettexture of two cameraGamecamera.targettexture =RT; Bgcamera.targettexture=RT; //Note the rendering orderBgcamera.render ();           Gamecamera.render (); //activates the RT and reads the pixels from it. Rendertexture.active =RT; Texture2d screenshot=NewTexture2d ((int) Rect.width, (int) Rect.height, Textureformat.rgb24,false); Screenshot.readpixels (Rect,0,0);//Note: This time, it is reading pixels from the rendertexture.activescreenshot.apply (); //Reset the relevant parameters to use the camera to continue to display on the screenGamecamera.targettexture =NULL; Bgcamera.targettexture=NULL; //ps:camera2.targetTexture = null;Rendertexture.active =NULL;//jc:added to avoid errorsGameobject.destroy (RT); //Finally, these texture data into a PNG image file        byte[] bytes =screenshot.encodetopng (); stringfilename = Application.datapath +"/screenshot.png";        System.IO.File.WriteAllBytes (filename, bytes); Debug.Log (string. Format ("screenshot of a picture: {0}", filename)); returnscreenshot; }}

"Base" Unity only captures scene content

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.