Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

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

Discussion in 'Editor & General Support' started by xefhs, Nov 21, 2012.

  1. xefhs

    xefhs

    Joined:
    Jul 16, 2011
    Posts:
    34
    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 ?
     
    TechRoxx5 likes this.
  2. foxter888

    foxter888

    Joined:
    May 3, 2010
    Posts:
    530
    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
     
  3. thebarryman

    thebarryman

    Joined:
    Nov 22, 2012
    Posts:
    130
    Hmmm, I guess you could break off the Game tab and just drag it off of the screen? That does seem pretty annoying.
     
  4. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    There is a way and you already figured it out pretty much
     
  5. xefhs

    xefhs

    Joined:
    Jul 16, 2011
    Posts:
    34
    As I feared, we can't "correctly" remove the switching...

    Anyway, thanks for answering.
     
  6. pivotraze

    pivotraze

    Joined:
    Feb 4, 2012
    Posts:
    593
  7. Benjiko99

    Benjiko99

    Joined:
    May 15, 2013
    Posts:
    1
    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.
     
  8. Tenderz

    Tenderz

    Joined:
    May 23, 2014
    Posts:
    1
    You can use this piece of code on in a Start() function:
    Code (csharp):
    1. UnityEditor.SceneView.FocusWindowIfItsOpen(typeof(UnityEditor.SceneView));
     
    Novack, toonsend, VotaVader and 7 others like this.
  9. Mabenan

    Mabenan

    Joined:
    Feb 7, 2014
    Posts:
    132
    remember to check before this function if you are in the editor otherwise you will have problems in the build version of the game
     
    ow3n likes this.
  10. ToneSlaveMusic

    ToneSlaveMusic

    Joined:
    Oct 16, 2015
    Posts:
    4
    How do you by code check if you are in the editor?
     
  11. syscrusher

    syscrusher

    Joined:
    Jul 4, 2015
    Posts:
    1,104
    Try this:

    #if UNITY_EDITOR
    UnityEditor.SceneView.FocusWindowIfItsOpen(typeof(UnityEditor.SceneView));
    #endif
     
    Napoloo, VotaVader, jojue and 6 others like this.
  12. JDelekto

    JDelekto

    Joined:
    Jan 7, 2017
    Posts:
    49
    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:

    Code (CSharp):
    1. #if UNITY_EDITOR
    2.  
    3. using UnityEngine;
    4.  
    5. public class DebugManager : MonoBehaviour
    6. {
    7.     public bool useSceneView = false;
    8.  
    9.     // Start is called before the first frame update
    10.     private void Awake()
    11.     {
    12.         if (useSceneView)
    13.         {
    14.             UnityEditor.SceneView.FocusWindowIfItsOpen(typeof(UnityEditor.SceneView));
    15.         }
    16.     }
    17. }
    18. #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. :)
     
  13. syscrusher

    syscrusher

    Joined:
    Jul 4, 2015
    Posts:
    1,104
    Nice idea, making it more accessible than my single-line solution. :)
     
  14. JDelekto

    JDelekto

    Joined:
    Jan 7, 2017
    Posts:
    49
    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!
     
  15. phobos2077

    phobos2077

    Joined:
    Feb 10, 2018
    Posts:
    350
    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
     
  16. syscrusher

    syscrusher

    Joined:
    Jul 4, 2015
    Posts:
    1,104
    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!
     
  17. JDelekto

    JDelekto

    Joined:
    Jan 7, 2017
    Posts:
    49
    That's a good idea. Now I know how some editor extensions get started. :)
     
  18. racarate

    racarate

    Joined:
    Jul 14, 2012
    Posts:
    62
    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!
     
    JDelekto likes this.
  19. varikor

    varikor

    Joined:
    Jan 8, 2019
    Posts:
    1
    Thanks :) 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!
     
  20. n-gist

    n-gist

    Joined:
    Mar 7, 2016
    Posts:
    41
    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.
    Code (CSharp):
    1. #if TRUE && UNITY_EDITOR
    2. using UnityEngine;
    3. using UnityEditor;
    4.  
    5. namespace twicebetter {
    6.     [InitializeOnLoad]
    7.     internal static class TB_KeepSceneFocused {
    8.         private const  bool          forceFocusSceneOnPlay = true;
    9.         private static SceneView     sceneWindow;
    10.         private static EditorWindow  gameWindow;
    11.         private static bool          sceneNeedFocus;
    12.         private static bool          oneFrameSkipped;
    13.         private static System.Action focusFunc;
    14.         static TB_KeepSceneFocused() {
    15.             FindSceneWindow();
    16.             FindGameWindow();
    17.             EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
    18.         }
    19.         private static void FindSceneWindow() {
    20.             if (sceneWindow != null) return;
    21.             var sceneWindows = Resources.FindObjectsOfTypeAll<SceneView>();
    22.             if (sceneWindows != null && sceneWindows.Length > 0) sceneWindow = sceneWindows[0];
    23.         }
    24.         private static readonly System.Type gameWindowType = typeof(UnityEditor.EditorWindow).Assembly.GetType("UnityEditor.PlayModeView");
    25.         private static void FindGameWindow() {
    26.             if (gameWindow != null) return;
    27.             var gameWindows = Resources.FindObjectsOfTypeAll(gameWindowType);
    28.             if (gameWindows != null && gameWindows.Length > 0) gameWindow = (EditorWindow)gameWindows[0];
    29.         }
    30.         private static void StoreSceneNeedFocus() {
    31.             FindSceneWindow();
    32.             if (sceneWindow != null) sceneNeedFocus = sceneWindow.hasFocus;
    33.             else sceneNeedFocus = false;
    34.         }
    35.         private static void OnPlayModeStateChanged(PlayModeStateChange stateChange) {
    36.             switch (stateChange) {
    37.                 case PlayModeStateChange.ExitingEditMode:
    38.                     StoreSceneNeedFocus();
    39.                     break;
    40.                 case PlayModeStateChange.EnteredPlayMode:
    41.                     EditorApplication.pauseStateChanged += OnPauseStateChanged;
    42.                     if (EditorSettings.enterPlayModeOptionsEnabled &&
    43.                         EditorSettings.enterPlayModeOptions.HasFlag(EnterPlayModeOptions.DisableDomainReload)) {
    44.                         if (sceneNeedFocus) FocusSceneWindow();
    45.                     } else {
    46.                         if (forceFocusSceneOnPlay) FocusSceneWindow();
    47.                     }
    48.                     break;
    49.                 case PlayModeStateChange.ExitingPlayMode:
    50.                     StoreSceneNeedFocus();
    51.                     EditorApplication.pauseStateChanged -= OnPauseStateChanged;
    52.                     break;
    53.                 case PlayModeStateChange.EnteredEditMode:
    54.                     FindSceneWindow();
    55.                     if (sceneWindow != null) {
    56.                         if (sceneNeedFocus) {
    57.                             if (!sceneWindow.hasFocus) FocusSceneWindow();
    58.                         } else {
    59.                             FindGameWindow();
    60.                             if (gameWindow != null) FocusGameWindow();
    61.                         }
    62.                        
    63.                     }
    64.                     break;
    65.             }
    66.         }
    67.         private static void OnPauseStateChanged(PauseState pauseState) {
    68.             switch (pauseState) {
    69.                 case PauseState.Paused:
    70.                     StoreSceneNeedFocus();
    71.                     if (!sceneNeedFocus) {
    72.                         FindGameWindow();
    73.                         if (gameWindow != null && gameWindow.hasFocus) FocusOnUpdate(FocusGameWindow);
    74.                     } else FocusOnUpdate(FocusSceneWindow);
    75.                     break;
    76.                 case PauseState.Unpaused:
    77.                     if (sceneNeedFocus) FocusOnUpdate(FocusSceneWindow);
    78.                     break;
    79.             }
    80.         }
    81.         private static void FocusOnUpdate(System.Action focusFunc) {
    82.             TB_KeepSceneFocused.focusFunc = focusFunc;
    83.             oneFrameSkipped = false;
    84.             EditorApplication.update += OnUpdateFocusFunc;
    85.         }
    86.         private static void OnUpdateFocusFunc() {
    87.             if (oneFrameSkipped) {
    88.                 EditorApplication.update -= OnUpdateFocusFunc;
    89.                 focusFunc();
    90.             }
    91.             oneFrameSkipped = true;
    92.         }
    93.         private static void FocusSceneWindow() {
    94.             FindSceneWindow();
    95.             if (sceneWindow != null) sceneWindow.Focus();
    96.         }
    97.         private static void FocusGameWindow() {
    98.             FindGameWindow();
    99.             if (gameWindow != null) gameWindow.Focus();
    100.         }
    101.     }
    102. }
    103. #endif
     

    Attached Files:

    wpetillo, mo-unity-00 and dimmduh1 like this.
  21. wpetillo

    wpetillo

    Joined:
    May 23, 2017
    Posts:
    24
    To add to n-gist's answer, to change forceFocusSceneOnPlay in editor: make it public and then add this alongside:

    Code (CSharp):
    1. using UnityEngine;
    2. using twicebetter;
    3.  
    4. namespace Playcraft
    5. {
    6.     public class FocusedSceneSOInterface : ScriptableObject, IRelayBool
    7.     {
    8.         public virtual void Relay(bool value) { }
    9.     }
    10.  
    11.     [CreateAssetMenu(menuName = "Editor/Set Focused Scene")]
    12.     public class SetStartFocusedSceneSO : FocusedSceneSOInterface
    13.     {
    14.         public bool startInScene;
    15.    
    16.         void OnValidate() { Relay(startInScene); }
    17.    
    18.         public override void Relay(bool newValue)
    19.         {
    20.             startInScene = newValue;
    21.             KeepSceneFocused.forceFocusSceneOnPlay = newValue;
    22.         }
    23.     }
    24. }
    and create the scriptable object. Ticking the checkbox in the scriptable object will change the static value. To go a step further, add this script, not in an editor folder:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. namespace Playcraft
    4. {
    5.     public class SetStartFocusedSceneMono : MonoBehaviour
    6.     {
    7.         [SerializeField] bool startInScene;
    8.         [SerializeField] ScriptableObject so;
    9.    
    10.         void OnValidate()
    11.         {
    12.             if (!so) return;
    13.             var relay = (IRelayBool)so;
    14.             relay.Relay(startInScene);
    15.         }
    16.     }
    17. }
    Oh, and this interface will need to be somewhere in your project:

    public interface IRelayBool { void Relay(bool value); }


    then attach the MonoBehaviour to any object in your scene and assign the scriptable object. Ticking the box on/off in the MonoBehaviour will now also set the static value.

    A bit indirect, but this is one way to access an editor script value from a MonoBehaviour. And with that, the Editor is one step closer to working the way it should have been designed in the first place.
     
  22. n-gist

    n-gist

    Joined:
    Mar 7, 2016
    Posts:
    41
    That is nice updates to the script to make it more handy! Except that it doesn't turn off it completely, only if you using fast enter play mode (forceFocusSceneOnPlay takes its action only in that case).

    However, Editor now has built in option that makes just that and it works better. It is in Game window, called Play Unfocused
    Screenshot_1.png
     
    erenaydin, Novack, vozcn and 14 others like this.
  23. azeem_ehsan

    azeem_ehsan

    Joined:
    Mar 5, 2022
    Posts:
    5
    while you are in Play Mode and you have both SCENE and GAME tab Splitted , then the total FPS of your system will be divided into both SCENE TAB and GAME TAB. So you'll get half fps in Game
     
  24. Napoloo

    Napoloo

    Joined:
    Nov 30, 2021
    Posts:
    5
    Here's my attempt as well:
    Code (CSharp):
    1. #if UNITY_EDITOR
    2. using UnityEditor;
    3. using UnityEngine;
    4.  
    5.  
    6. internal sealed class SceneViewFocus : MonoBehaviour
    7. {
    8.     private const string GAMEOBJECT_NAME = nameof(SceneViewFocus);
    9.  
    10.     private static event System.Action OnAwakeEvent;
    11.  
    12.     private static new GameObject gameObject;
    13.  
    14.  
    15.     [MenuItem("SceneView/Disable Focus", true)]
    16.     private static bool DisableFocusValidate()
    17.     {
    18.         gameObject = GameObject.Find(GAMEOBJECT_NAME);
    19.  
    20.         return gameObject;
    21.     }
    22.  
    23.  
    24.     [MenuItem("SceneView/Disable Focus")]
    25.     private static void DisableFocus() => DestroyImmediate(gameObject);
    26.  
    27.  
    28.     [MenuItem("SceneView/Enable Focus", true)]
    29.     private static bool EnableFocusValidate() => !DisableFocusValidate();
    30.  
    31.  
    32.     [MenuItem("SceneView/Enable Focus")]
    33.     private static void EnableFocus() => gameObject = new GameObject(GAMEOBJECT_NAME, typeof(SceneViewFocus))
    34.     {
    35.         hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector | HideFlags.NotEditable
    36.     };
    37.  
    38.  
    39.     [InitializeOnLoadMethod]
    40.     private static void RegisterEventHandlerOnStartup() => OnAwakeEvent += OnAwakeEventHandler;
    41.  
    42.  
    43.     private static void OnAwakeEventHandler()
    44.     {
    45.         if (EditorWindow.HasOpenInstances<SceneView>())
    46.         {
    47.             SceneView.lastActiveSceneView.Focus();
    48.         }
    49.         else
    50.         {
    51.             var gameView = typeof(Editor).Assembly.GetType("UnityEditor.GameView");
    52.  
    53.             _ = EditorWindow.GetWindow<SceneView>(new[] { gameView });
    54.         }
    55.     }
    56.  
    57.  
    58.     private void Awake() => OnAwakeEvent?.Invoke();
    59. }
    60. #endif
    Then you'll be able to disable or enable it from here:
     
    Last edited: Mar 5, 2023
  25. WCPeaches

    WCPeaches

    Joined:
    Oct 14, 2022
    Posts:
    2
    I was getting a migraine at a lack of development options on the run button. After clicking back to scene view a couple thousand times, or not having the eyesight to split the screen and see the scene in a teeny tiny view while Unity jumps to the game tab, I tired of forced game tab free aspect ratio.

    Unwilling to re-code the editor itself, like y'all are doing, I checked a few forums here and then went back to my running sever. A solution is possible.

    I don't code for free. C# is as hard as Chinese. You all deserve to get paid.

    However, there is a non code option, for all those artists who do nothing but write enigma subconsciously.

    Nubz is on it up there at the top of this thread. Nice one.

    Pull Game tab out and make it its own window.

    Minimize its edges till it is as small as possible, and just let it run in the corner.

    And here is the secret. . .

    Ignore it.

    Development takes place in scene tab. Playtesting takes place in game tab.

    I wonder which one the ole Birdie is doing? Unless your using a toy from someone else, owing original game engine in the scene tab is the only way to design. And the server allows me to scroll and move while the thing is runnin'.....nice one Unity. Even with that little minimal game window crankin'. As for fps....Up your towers.

    Unity, thank you all for your excellent product. When I make my billions I will come visit ya and take everyone out to lunch, bar tab included of course.

    *shink* The pink katana draws blood on the field of game development again.

    Birdie.

    p.s. takes samurai to know samurai, may all your projects be as successful as you all are.
     
  26. heronww

    heronww

    Joined:
    Sep 8, 2019
    Posts:
    30
    it seems advanced scripts are needed for basic things like editor key remappings and preventing game view focus when starting the game. These are basic things that should be available in preferences. Is there any reason Unity devs don't actually implement what users want? Just curious.
     
  27. But they do. The Play Unfocused does exactly what you're asking for.
    Capture.PNG
     
    radiantboy and heronww like this.
  28. heronww

    heronww

    Joined:
    Sep 8, 2019
    Posts:
    30
    Very cool. I unequivocally apologize
     
  29. It's cool, we're always learning and Unity Editor is a giant software with a lot of functionality.
    This is also tackled a long time ago. It's in the
    Edit
    menu.
    Capture.PNG
     
    heronww likes this.
  30. heronww

    heronww

    Joined:
    Sep 8, 2019
    Posts:
    30
    Wow, amazing. You're a savior. A lot has changed since I last used this engine years ago, I'm genuinely impressed. No idea how I didn't come across all this.