private IEnumerator _takeScreenShot(Canvas canvasPanel, SCREENSHOT_TYPE screenShotType = SCREENSHOT_TYPE.IMAGE_AND_TEXT, bool createNewInstance = true) { //Get Visible Canvas In the Scene allOtherCanvas = getAllCanvasInScene(false); //Hide all the other Visible Canvas except the one that is passed in as parameter(Canvas we want to take Picture of) showCanvasExcept(allOtherCanvas, canvasPanel, false); //Reset the position so that both UI will be in the-same place if we make the duplicate a child resetPosAndRot(gameObject); //Check if we should operate on the original image or make a duplicate of it if (createNewInstance) { //Duplicate the Canvas we want to take Picture of duplicatedTargetUI = duplicateUI(canvasPanel.gameObject, "ScreenShotUI"); //Make this game object the parent of the Canvas duplicatedTargetUI.transform.SetParent(gameObject.transform); //Hide the orginal Canvas we want to take Picture of showCanvas(canvasPanel, false); } else { //No duplicate. Use original GameObject //Make this game object the parent of the Canvas canvasPanel.transform.SetParent(gameObject.transform); } RenderMode defaultRenderMode; //Change the duplicated Canvas to RenderMode to overlay Canvas duplicatedCanvas = null; if (createNewInstance) { duplicatedCanvas = duplicatedTargetUI.GetComponent <Canvas>(); defaultRenderMode = duplicatedCanvas.renderMode; duplicatedCanvas.renderMode = RenderMode.ScreenSpaceOverlay; } else { defaultRenderMode = canvasPanel.renderMode; canvasPanel.renderMode = RenderMode.ScreenSpaceOverlay; } if (screenShotType == SCREENSHOT_TYPE.IMAGE_AND_TEXT) { //No Action Needed } else if (screenShotType == SCREENSHOT_TYPE.IMAGE_ONLY) { if (createNewInstance) { //Get all images on the duplicated visible Canvas allTexts = getAllTextsFromCanvas(duplicatedTargetUI, false); //Hide those images showTexts(allTexts, false); } else { //Get all images on the duplicated visible Canvas allTexts = getAllTextsFromCanvas(canvasPanel.gameObject, false); //Hide those images showTexts(allTexts, false); } } else if (screenShotType == SCREENSHOT_TYPE.TEXT_ONLY) { if (createNewInstance) { //Get all images on the duplicated visible Canvas allImages = getAllImagesFromCanvas(duplicatedTargetUI, false); //Hide those images showImages(allImages, false); } else { //Get all images on the duplicated visible Canvas allImages = getAllImagesFromCanvas(canvasPanel.gameObject, false); //Hide those images showImages(allImages, false); } } //////////////////////////////////////Finally Take ScreenShot/////////////////////////////// yield return(new WaitForEndOfFrame()); Texture2D screenImage = new Texture2D(Screen.width, Screen.height); //Get Image from screen screenImage.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0); screenImage.Apply(); //Convert to png byte[] pngBytes = screenImage.EncodeToPNG(); /*FOR TESTING/DEBUGGING PURPOSES ONLY. COMMENT THIS * string path = Application.persistentDataPath + "/CanvasScreenShot.png"; * System.IO.File.WriteAllBytes(path, pngBytes); * Debug.Log(path);*/ //Notify functions that are subscribed to this event that picture is taken then pass in image bytes as png if (OnPictureTaken != null) { OnPictureTaken(pngBytes); } ///////////////////////////////////RE-ENABLE OBJECTS //Change the duplicated Canvas RenderMode back to default Value if (createNewInstance) { duplicatedCanvas.renderMode = defaultRenderMode; } else { canvasPanel.renderMode = defaultRenderMode; } //Un-Hide all the other Visible Canvas except the one that is passed in as parameter(Canvas we want to take Picture of) showCanvas(allOtherCanvas, true); if (screenShotType == SCREENSHOT_TYPE.IMAGE_AND_TEXT) { //No Action Needed } else if (screenShotType == SCREENSHOT_TYPE.IMAGE_ONLY) { //Un-Hide those images showTexts(allTexts, true); } else if (screenShotType == SCREENSHOT_TYPE.TEXT_ONLY) { //Un-Hide those images showImages(allImages, true); } //Un-hide the orginal Canvas we want to take Picture of showCanvas(canvasPanel, true); if (createNewInstance) { //Destroy the duplicated GameObject Destroy(duplicatedTargetUI, 1f); } else { //Remove the Canvas as parent canvasPanel.transform.SetParent(null); } }
//takes Screenshot public void takeScreenShot(Canvas canvasPanel, SCREENSHOT_TYPE screenShotType = SCREENSHOT_TYPE.IMAGE_AND_TEXT, bool createNewInstance = true) { StartCoroutine(_takeScreenShot(canvasPanel, screenShotType, createNewInstance)); }