Beispiel #1
0
    IEnumerator TakeSnap()
    {
        yield return(new WaitForEndOfFrame());

        RenderTexture renderTexture = new RenderTexture(Screen.width, Screen.height, 24);

        mainCam.SetTargetTexture(renderTexture);
        Texture2D screenShot = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);

        mainCam.SecondaryRender();
        RenderTexture.active = renderTexture;

        var topLeftScreen     = mainCam.WorldToScreen(topLeftAnchor.position);
        var bottomRightScreen = mainCam.WorldToScreen(bottomRightAnchor.position);
        var width             = bottomRightScreen.x - topLeftScreen.x;
        var height            = topLeftScreen.y - bottomRightScreen.y;

        screenShot.ReadPixels(
            new Rect(topLeftScreen.x, Screen.height - topLeftScreen.y, width, height),
            0, 0);
        screenShot.Apply();

        // For saving the screenshot
        // Destroy(renderTexture);
        // byte[] bytes = screenShot.EncodeToPNG();
        // string filename = "newScreenshot.png";
        // System.IO.File.WriteAllBytes(filename, bytes);
        // Debug.Log(string.Format("Took screenshot to: {0}", filename));

        snapDisplay = screenShot;
        // snapDisplay.SetTexture("NewDisplay", snapDisplayTexture);
    }