Пример #1
0
    /// <summary>
    /// OnPostRender is called after a camera finished rendering the Scene.
    /// Accordingly, taking the picture is performed at the end of a frame.
    /// </summary>
    private void OnPostRender()
    {
        if (takePictureOnNextFrame)         // Proceed if the flag is set.
        {
            // Reset the flag.
            takePictureOnNextFrame = false;

            // Get the render texture.
            RenderTexture renderTexture = screenshotCamera.targetTexture;

            // Store the data of a rendered picture. Make it transparent by using ARGB32 format and do not use a bitmap.
            Texture2D renderPicture = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.ARGB32, false);
            // Create a rectangel to save the picture
            Rect rect = new Rect(0, 0, renderTexture.width, renderTexture.height);
            renderPicture.ReadPixels(rect, 0, 0);

            // Read bytes and encode to a PNG.
            byte[] byteArray = renderPicture.EncodeToPNG();
            string fileName  = GetPictureName(renderTexture.width, renderTexture.height);
            // Save the file.
            System.IO.File.WriteAllBytes(fileName, byteArray);
            Debug.Log("The picture has been taken!");

            // Release a temporary render texture allocated to the camera.
            RenderTexture.ReleaseTemporary(renderTexture);
            screenshotCamera.targetTexture = null;

            ProgramManager.DisplayTakingPhotoPanel(1f);

            // Refresh the folder to see the updated conent.
            UnityEditor.AssetDatabase.Refresh();
        }
    }