コード例 #1
0
        public void TakeScreenshot(object source, int frameNumber, int maximumWidth, int maximumHeight, Action <int, byte[]> callback)
        {
            ScreenshotOperation screenshotOperation = this.GetScreenshotOperation();

            screenshotOperation.FrameNumber   = frameNumber;
            screenshotOperation.MaximumWidth  = maximumWidth;
            screenshotOperation.MaximumHeight = maximumHeight;
            screenshotOperation.Source        = source;
            screenshotOperation.Callback      = callback;
        }
コード例 #2
0
        private void ScreenshotCallback(byte[] data, object state)
        {
            ScreenshotOperation screenshotOperation = state as ScreenshotOperation;

            if (screenshotOperation != null)
            {
                screenshotOperation.Data       = data;
                screenshotOperation.IsComplete = true;
            }
        }
コード例 #3
0
        /// <inheritdoc cref="IUserReportingPlatform"/>
        public void TakeScreenshot(int frameNumber, int maximumWidth, int maximumHeight, object source, Action <int, byte[]> callback)
        {
            ScreenshotOperation screenshotOperation = new ScreenshotOperation();

            screenshotOperation.FrameNumber   = frameNumber;
            screenshotOperation.MaximumWidth  = maximumWidth;
            screenshotOperation.MaximumHeight = maximumHeight;
            screenshotOperation.Source        = source;
            screenshotOperation.Callback      = callback;
            screenshotOperation.UnityFrame    = Time.frameCount;
            this.screenshotOperations.Add(screenshotOperation);
        }
コード例 #4
0
        private void ScreenshotInternal(Texture source, int maximumWidth, int maximumHeight, ScreenshotType type, Action <byte[], object> callback, object state)
        {
            ScreenshotOperation operation = this.GetOperation();

            operation.Identifier    = ScreenshotRecorder.nextIdentifier++;
            operation.Source        = source;
            operation.MaximumWidth  = maximumWidth;
            operation.MaximumHeight = maximumHeight;
            operation.Type          = type;
            operation.Callback      = callback;
            operation.State         = state;
            AsyncGPUReadback.Request(source, 0, TextureFormat.RGBA32, operation.ScreenshotCallbackDelegate);
        }
コード例 #5
0
        private ScreenshotOperation GetOperation()
        {
            foreach (ScreenshotOperation operation in this.operationPool)
            {
                if (!operation.IsInUse)
                {
                    operation.IsInUse = true;
                    return(operation);
                }
            }
            ScreenshotOperation newOperation = new ScreenshotOperation();

            newOperation.IsInUse = true;
            this.operationPool.Add(newOperation);
            return(newOperation);
        }
コード例 #6
0
        private ScreenshotOperation GetScreenshotOperation()
        {
            foreach (ScreenshotOperation screenshotOperation in this.screenshotOperations)
            {
                if (!screenshotOperation.IsInUse)
                {
                    screenshotOperation.Use();
                    return(screenshotOperation);
                }
            }
            ScreenshotOperation newScreenshotOperation = new ScreenshotOperation();

            newScreenshotOperation.Use();
            this.screenshotOperations.Add(newScreenshotOperation);
            return(newScreenshotOperation);
        }
コード例 #7
0
        /// <inheritdoc cref="IUserReportingPlatform"/>
        public void OnEndOfFrame(UserReportingClient client)
        {
            // Screenshot Operations
            int screenshotOperationIndex = 0;

            while (screenshotOperationIndex < this.screenshotOperations.Count)
            {
                ScreenshotOperation screenshotOperation = this.screenshotOperations[screenshotOperationIndex];
                if (screenshotOperation.Stage == ScreenshotStage.Render && screenshotOperation.WaitFrames < 1)
                {
                    Camera cameraSource = screenshotOperation.Source as Camera;
                    if (cameraSource != null)
                    {
                        this.screenshotStopwatch.Reset();
                        this.screenshotStopwatch.Start();
                        RenderTexture renderTexture         = new RenderTexture(screenshotOperation.MaximumWidth, screenshotOperation.MaximumHeight, 24);
                        RenderTexture originalTargetTexture = cameraSource.targetTexture;
                        cameraSource.targetTexture = renderTexture;
                        cameraSource.Render();
                        cameraSource.targetTexture = originalTargetTexture;
                        this.screenshotStopwatch.Stop();
                        client.SampleClientMetric("Screenshot.Render", this.screenshotStopwatch.ElapsedMilliseconds);
                        screenshotOperation.Source     = renderTexture;
                        screenshotOperation.Stage      = ScreenshotStage.ReadPixels;
                        screenshotOperation.WaitFrames = 15;
                        screenshotOperationIndex++;
                        continue;
                    }
                    else
                    {
                        screenshotOperation.Stage = ScreenshotStage.ReadPixels;
                    }
                }
                if (screenshotOperation.Stage == ScreenshotStage.ReadPixels && screenshotOperation.WaitFrames < 1)
                {
                    this.screenshotStopwatch.Reset();
                    this.screenshotStopwatch.Start();
                    RenderTexture renderTextureSource = screenshotOperation.Source as RenderTexture;
                    if (renderTextureSource != null)
                    {
                        RenderTexture originalActiveTexture = RenderTexture.active;
                        RenderTexture.active        = renderTextureSource;
                        screenshotOperation.Texture = new Texture2D(renderTextureSource.width, renderTextureSource.height, TextureFormat.ARGB32, true);
                        screenshotOperation.Texture.ReadPixels(new Rect(0, 0, renderTextureSource.width, renderTextureSource.height), 0, 0);
                        screenshotOperation.Texture.Apply();
                        RenderTexture.active = originalActiveTexture;
                    }
                    else
                    {
                        screenshotOperation.Texture = new Texture2D(Screen.width, Screen.height, TextureFormat.ARGB32, true);
                        screenshotOperation.Texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
                        screenshotOperation.Texture.Apply();
                    }
                    this.screenshotStopwatch.Stop();
                    client.SampleClientMetric("Screenshot.ReadPixels", this.screenshotStopwatch.ElapsedMilliseconds);
                    screenshotOperation.Stage      = ScreenshotStage.GetPixels;
                    screenshotOperation.WaitFrames = 15;
                    screenshotOperationIndex++;
                    continue;
                }
                if (screenshotOperation.Stage == ScreenshotStage.GetPixels && screenshotOperation.WaitFrames < 1)
                {
                    this.screenshotStopwatch.Reset();
                    this.screenshotStopwatch.Start();
                    int maximumWidth  = screenshotOperation.MaximumWidth > 32 ? screenshotOperation.MaximumWidth : 32;
                    int maximumHeight = screenshotOperation.MaximumHeight > 32 ? screenshotOperation.MaximumHeight : 32;
                    int width         = screenshotOperation.Texture.width;
                    int height        = screenshotOperation.Texture.height;
                    int mipLevel      = 0;
                    while (width > maximumWidth || height > maximumHeight)
                    {
                        width  /= 2;
                        height /= 2;
                        mipLevel++;
                    }
                    screenshotOperation.TextureResized = new Texture2D(width, height);
                    screenshotOperation.TextureResized.SetPixels(screenshotOperation.Texture.GetPixels(mipLevel));
                    screenshotOperation.TextureResized.Apply();
                    this.screenshotStopwatch.Stop();
                    client.SampleClientMetric("Screenshot.GetPixels", this.screenshotStopwatch.ElapsedMilliseconds);
                    screenshotOperation.Stage      = ScreenshotStage.EncodeToPNG;
                    screenshotOperation.WaitFrames = 15;
                    screenshotOperationIndex++;
                    continue;
                }
                if (screenshotOperation.Stage == ScreenshotStage.EncodeToPNG && screenshotOperation.WaitFrames < 1)
                {
                    this.screenshotStopwatch.Reset();
                    this.screenshotStopwatch.Start();
                    screenshotOperation.PngData = screenshotOperation.TextureResized.EncodeToPNG();
                    this.screenshotStopwatch.Stop();
                    client.SampleClientMetric("Screenshot.EncodeToPNG", this.screenshotStopwatch.ElapsedMilliseconds);
                    screenshotOperation.Stage = ScreenshotStage.Done;
                    screenshotOperationIndex++;
                    continue;
                }
                if (screenshotOperation.Stage == ScreenshotStage.Done && screenshotOperation.WaitFrames < 1)
                {
                    screenshotOperation.Callback(screenshotOperation.FrameNumber, screenshotOperation.PngData);
                    UnityEngine.Object.Destroy(screenshotOperation.Texture);
                    UnityEngine.Object.Destroy(screenshotOperation.TextureResized);
                    this.screenshotOperations.Remove(screenshotOperation);
                }
                screenshotOperation.WaitFrames--;
            }
        }