Don't switch to game tab when I press Play button

Hi,

Sometimes the Scene view is more useful than the Game view, but when I press the “Play” button, Unity switch automatically to the Game tab.

The only solution I’ve found is to use a Layout where I see the scene AND the game view together… Any way to to tell Unity I don’t want to see the Game view when I press play ?

1 Like

you are correct, when you hit play it automatically goes to the game tab. it would be nice if they had a option for it to automatically only go on that certain tab or go to the desired window when running

Hmmm, I guess you could break off the Game tab and just drag it off of the screen? That does seem pretty annoying.

There is a way and you already figured it out pretty much

As I feared, we can’t “correctly” remove the switching…

Anyway, thanks for answering.

This is just how I set up mine :stuck_out_tongue:

http://screencast.com/t/mXtwRwxm

I have done mine like this http://i.imgur.com/Ecw2h3B.png. Both the scene view and Game view have exactly 16:9 aspect ratio on my 1920x1080 screen so i can see exactly what i will see in the final game, and the scene view is quite massive.

You can use this piece of code on in a Start() function:

UnityEditor.SceneView.FocusWindowIfItsOpen(typeof(UnityEditor.SceneView));
10 Likes

remember to check before this function if you are in the editor otherwise you will have problems in the build version of the game

1 Like

How do you by code check if you are in the editor?

Try this:

#if UNITY_EDITOR
UnityEditor.SceneView.FocusWindowIfItsOpen(typeof(UnityEditor.SceneView));
#endif

9 Likes

I ran across this thread when looking for a solution a long time ago, I apologize for re-awaking this necro thread, but I took the information here and came up with a helpful way to use this that’s easy to remove later.

Just create a new C# script called “DebugManager” (or your own name, season to taste) with the following code:

#if UNITY_EDITOR

using UnityEngine;

public class DebugManager : MonoBehaviour
{
    public bool useSceneView = false;

    // Start is called before the first frame update
    private void Awake()
    {
        if (useSceneView)
        {
            UnityEditor.SceneView.FocusWindowIfItsOpen(typeof(UnityEditor.SceneView));
        }
    }
}
#endif

Create an empty game object in your scene and attach this script to it so that it executes on the awake. Toggle the “Use Scene View” checkbox to start up in game mode or scene mode if you want to switch the two. If you forget to remove the game object, no harm, because it does have the #ifdef, but you can remove the object later without tainting any of your existing code.

As the community had a great answer for this, thought I’d share. :slight_smile:

9 Likes

Nice idea, making it more accessible than my single-line solution. :slight_smile:

Thank you for your original answer! --I was inspired to make it an optional thing because there are times I needed to change my perspective in scene view, but the game mode was handling mouse input for other purposes. I needed to be able to do both when debugging. Cheers!

It’s nice, but will it work when I pause and unpause the play mode? Start and/or awake won’t be called in that case. I guess your solution could be expanded using EditorApplication.pauseStateChanged

I’d be glad to test this, but unfortunately Microsoft’s latest update this morning has rendered my Unity development workstation nonbootable, so I’ll have to get back to you after I get that little mess resolved. {sigh} Thanks, Microsoft…for nothing!

That’s a good idea. Now I know how some editor extensions get started. :slight_smile:

Hey, this is awesome. I’m addicted to shading and lighting so keeping me away from that until the end of the project is a good idea… this script combined with WIREFRAME view mode for the SceneView is totally 90s LightWave chic!

1 Like

Thanks :slight_smile: works perfectly for me, just a note to new unity users, just remember to turn the bool to true or the script will do nothing!

Combined solutions and suggestions from previous posts, got the following script. In general, it tries to keep the current Scene or Game window active, preventing switching between them. Handles pause in play mode. Layout Scene and Game windows as adjacent tabs for the script to work properly. Works better when using Enter Play Mode Options enabled with Reload Domain disabled. If not, then the forceFocusSceneOnPlay value is used. Change TRUE to FALSE on the first line to disable script.

#if TRUE && UNITY_EDITOR
using UnityEngine;
using UnityEditor;

namespace twicebetter {
    [InitializeOnLoad]
    internal static class TB_KeepSceneFocused {
        private const  bool          forceFocusSceneOnPlay = true;
        private static SceneView     sceneWindow;
        private static EditorWindow  gameWindow;
        private static bool          sceneNeedFocus;
        private static bool          oneFrameSkipped;
        private static System.Action focusFunc;
        static TB_KeepSceneFocused() {
            FindSceneWindow();
            FindGameWindow();
            EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
        }
        private static void FindSceneWindow() {
            if (sceneWindow != null) return;
            var sceneWindows = Resources.FindObjectsOfTypeAll<SceneView>();
            if (sceneWindows != null && sceneWindows.Length > 0) sceneWindow = sceneWindows[0];
        }
        private static readonly System.Type gameWindowType = typeof(UnityEditor.EditorWindow).Assembly.GetType("UnityEditor.PlayModeView");
        private static void FindGameWindow() {
            if (gameWindow != null) return;
            var gameWindows = Resources.FindObjectsOfTypeAll(gameWindowType);
            if (gameWindows != null && gameWindows.Length > 0) gameWindow = (EditorWindow)gameWindows[0];
        }
        private static void StoreSceneNeedFocus() {
            FindSceneWindow();
            if (sceneWindow != null) sceneNeedFocus = sceneWindow.hasFocus;
            else sceneNeedFocus = false;
        }
        private static void OnPlayModeStateChanged(PlayModeStateChange stateChange) {
            switch (stateChange) {
                case PlayModeStateChange.ExitingEditMode:
                    StoreSceneNeedFocus();
                    break;
                case PlayModeStateChange.EnteredPlayMode:
                    EditorApplication.pauseStateChanged += OnPauseStateChanged;
                    if (EditorSettings.enterPlayModeOptionsEnabled &&
                        EditorSettings.enterPlayModeOptions.HasFlag(EnterPlayModeOptions.DisableDomainReload)) {
                        if (sceneNeedFocus) FocusSceneWindow();
                    } else {
                        if (forceFocusSceneOnPlay) FocusSceneWindow();
                    }
                    break;
                case PlayModeStateChange.ExitingPlayMode:
                    StoreSceneNeedFocus();
                    EditorApplication.pauseStateChanged -= OnPauseStateChanged;
                    break;
                case PlayModeStateChange.EnteredEditMode:
                    FindSceneWindow();
                    if (sceneWindow != null) {
                        if (sceneNeedFocus) {
                            if (!sceneWindow.hasFocus) FocusSceneWindow();
                        } else {
                            FindGameWindow();
                            if (gameWindow != null) FocusGameWindow();
                        }
                       
                    }
                    break;
            }
        }
        private static void OnPauseStateChanged(PauseState pauseState) {
            switch (pauseState) {
                case PauseState.Paused:
                    StoreSceneNeedFocus();
                    if (!sceneNeedFocus) {
                        FindGameWindow();
                        if (gameWindow != null && gameWindow.hasFocus) FocusOnUpdate(FocusGameWindow);
                    } else FocusOnUpdate(FocusSceneWindow);
                    break;
                case PauseState.Unpaused:
                    if (sceneNeedFocus) FocusOnUpdate(FocusSceneWindow);
                    break;
            }
        }
        private static void FocusOnUpdate(System.Action focusFunc) {
            TB_KeepSceneFocused.focusFunc = focusFunc;
            oneFrameSkipped = false;
            EditorApplication.update += OnUpdateFocusFunc;
        }
        private static void OnUpdateFocusFunc() {
            if (oneFrameSkipped) {
                EditorApplication.update -= OnUpdateFocusFunc;
                focusFunc();
            }
            oneFrameSkipped = true;
        }
        private static void FocusSceneWindow() {
            FindSceneWindow();
            if (sceneWindow != null) sceneWindow.Focus();
        }
        private static void FocusGameWindow() {
            FindGameWindow();
            if (gameWindow != null) gameWindow.Focus();
        }
    }
}
#endif

6156381–672849–TB_KeepSceneFocused.cs (4.51 KB)

3 Likes