- Home /
Game view scale on compilation/play
Something that has been going on for some time in Unity since they added the Scaling of the game view is that sometimes after compilation and/or when pressing the play button the scale change for no reason (zoom in the game view). Restarting unity doesn't fix this. It seems to always go to 0.55 in scale.
Am I the only one that have this problem ? Did anyone managed to understand why it happens and how to solve this ?
Thanks for the help
I get it as well. It seems to vary depending on which resolution you choose. It seems to scale such that the resolution matches your PCs resolution, or something along those lines. So if you choose a high resolution (e.g. 1125x2436) it zooms in so you can only see some of the screen, but if you choose a low resolution (e.g. 800x480) it zooms out so the camera view only takes up a fraction of the game window.
but somehow doesn't happen all the time ... even when I setup the editor and prefered resolution the same way across projects/computers.
fun fact : it also does it when applying "free aspect" which,to me, doesn't make sense at all.
Thanks for the insight.
Answer by ABerlemont · Dec 19, 2018 at 02:06 PM
It seems that changing the layout through the "saved layout" menu (top right of the editor) temporary fix this issue.
@ABerlemont can you please tell me how you solve it ? I went to layout button in the top right corner and saved the layout. But it is still zoo$$anonymous$$g in on play mode.
I meant: change the layout selecting any other than the one you are using will refresh the editor and fix the issue
You can, for example, save your current layout then switch to another one and then come back to it using the layout menu
This used to work for me, but recently I couldn't get it to fix it. As I don't need a fixed resolution at the moment, I've just set it to a fixed scale ins$$anonymous$$d and then the problem doesn't happen.
Answer by svendkiloo · Mar 14, 2019 at 07:34 AM
EDIT: This seems to now have been fixed in 2019.1.8:
Editor: Fixed game view scale in play mode for mobile platforms. (1140742, 1151179)
https://unity3d.com/unity/whats-new/2019.1.8
Also fixed or planned fixed for some other major Unity versions: https://issuetracker.unity3d.com/issues/game-tab-window-rescales-when-entering-the-play-mode?_ga=2.6018931.1353564765.1561964524-1323957048.1539005405
An alternative solution, if you don't need to have the game view set to a certain resolution, is to set it instead to a certain aspect ratio. Then the scaling problem doesn't seem to occur.
The alternative solution of using an aspect ratio did the trick for me. Thanks!
Answer by bmulla · Mar 14, 2019 at 07:40 PM
Just use this code snippet http://answers.unity.com/answers/1612575/view.html
#if UNITY_EDITOR
void Awake()
{
SetGameViewScale();
}
void SetGameViewScale()
{
System.Reflection.Assembly assembly = typeof(UnityEditor.EditorWindow).Assembly;
System.Type type = assembly.GetType("UnityEditor.GameView");
UnityEditor.EditorWindow v = UnityEditor.EditorWindow.GetWindow(type);
var defScaleField = type.GetField("m_defaultScale", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
//whatever scale you want when you click on play
float defaultScale = 0.1f;
var areaField = type.GetField("m_ZoomArea", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
var areaObj = areaField.GetValue(v);
var scaleField = areaObj.GetType().GetField("m_Scale", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
scaleField.SetValue(areaObj, new Vector2(defaultScale, defaultScale));
}
#endif
Answer by Flexford · Apr 20, 2019 at 10:28 PM
Improved code:
using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
[InitializeOnLoad]
public static class FixResolutionScale
{
static FixResolutionScale()
{
EditorApplication.playModeStateChanged += OnPlayStateChanged;
SetGameViewScale();
}
private static void OnPlayStateChanged(PlayModeStateChange playModeStateChange)
{
SetGameViewScale();
}
private static void SetGameViewScale()
{
Type gameViewType = GetGameViewType();
EditorWindow gameViewWindow = GetGameViewWindow(gameViewType);
if (gameViewWindow == null)
{
Debug.LogError("GameView is null!");
return;
}
var defScaleField = gameViewType.GetField("m_defaultScale", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
//whatever scale you want when you click on play
float defaultScale = 0.1f;
var areaField = gameViewType.GetField("m_ZoomArea", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
var areaObj = areaField.GetValue(gameViewWindow);
var scaleField = areaObj.GetType().GetField("m_Scale", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
scaleField.SetValue(areaObj, new Vector2(defaultScale, defaultScale));
}
private static Type GetGameViewType()
{
Assembly unityEditorAssembly = typeof(EditorWindow).Assembly;
Type gameViewType = unityEditorAssembly.GetType("UnityEditor.GameView");
return gameViewType;
}
private static EditorWindow GetGameViewWindow(Type gameViewType)
{
Object[] obj = Resources.FindObjectsOfTypeAll(gameViewType);
if (obj.Length > 0)
{
return obj[0] as EditorWindow;
}
return null;
}
}
Answer by hojjat-reyhane · Jun 19, 2019 at 02:27 AM
A fix to previous solution. In my case if I edit the scripts, unity 2019.1.5 changes the scale of game window. So ... Just add this to your project.
using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
[InitializeOnLoad]
[ExecuteInEditMode]
public class FixGameWindowScaleBug
{
private static bool mUpdate = false;
static FixGameWindowScaleBug()
{
EditorApplication.update += update;
EditorApplication.delayCall += delayCall;
SetGameViewScale();
}
private static void delayCall()
{
mUpdate = true;
}
private static void update()
{
if (mUpdate)
{
SetGameViewScale();
mUpdate = false;
}
}
private static void SetGameViewScale()
{
Type gameViewType = GetGameViewType();
EditorWindow gameViewWindow = GetGameViewWindow(gameViewType);
if (gameViewWindow == null)
{
Debug.LogError("GameView is null!");
return;
}
var defScaleField = gameViewType.GetField("m_defaultScale", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
//whatever scale you want when you click on play
float defaultScale = 0.1f;
var areaField = gameViewType.GetField("m_ZoomArea", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
var areaObj = areaField.GetValue(gameViewWindow);
var scaleField = areaObj.GetType().GetField("m_Scale", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
scaleField.SetValue(areaObj, new Vector2(defaultScale, defaultScale));
}
private static Type GetGameViewType()
{
Assembly unityEditorAssembly = typeof(EditorWindow).Assembly;
Type gameViewType = unityEditorAssembly.GetType("UnityEditor.GameView");
return gameViewType;
}
private static EditorWindow GetGameViewWindow(Type gameViewType)
{
Object[] obj = Resources.FindObjectsOfTypeAll(gameViewType);
if (obj.Length > 0)
{
return obj[0] as EditorWindow;
}
return null;
}
}
Of course, if you can update to latest Unity, where it's fixed (see my answer), then you don't need to add it :)
Your answer
Follow this Question
Related Questions
Help with overhead view game 1 Answer
Unity Scaling for Android on 5.5. 0 Answers
Is there any way to get Debug.DrawLine() to display in the game view without other gizmos? 2 Answers
My game shows weird errors in game view 1 Answer
Scene is suddenly super small,Scene is suddenly super small on reload 1 Answer