Search code examples
unity-game-engine

Unity How do I capture GameView in my EditorWindow


I'm making a simple leveleditor window for my game and I want to capture GameView image to that leveleditor window. I'm using ScreenCapture.CaptureScreenshotAsTexture() but it gives the image of leveleditor window, I also try Texture2d.ReadPixels but it gives the same result.

enter image description here


Solution

  • You could have a look at my answer here. I wrote a script that can capture any window that is currently focused.

    You could easily adopt it to allways make the GameView the captured window. For example using reflection like this (source)

    public static EditorWindow GetMainGameView()
    {
        var assembly = typeof(EditorWindow).Assembly;
        var type = assembly.GetType("UnityEditor.GameView");
        var gameview = EditorWindow.GetWindow(type);
        return gameview;
    }
    

    and replacing

    var activeWindow = EditorWindow.focusedWindow;
    

    with

    var activeWindow = GetMainGameView();
    

    Note that this will open a new GameView window if none was there yet.