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